The (Unofficial) ITWorx Technical Architecture Blog

Testing Private Methods

Posted by archworx on June 21, 2007

Test Driven Development a practice that is becoming very popular due to the quality assurance that it adds. While it is easy to test public methods, How do I test private methods?

There are 2 ways:

  1. Using Helper Class discussed on The Code Project
  2. Using Microsoft’s build in Testing Tools


using Microsoft.VisualStudio.TestTools.UnitTesting;
PrivateObject po = new PrivateObject(new TestClass());
po.Invoke('PrivateMethodName'); /*This will invoke the private method*/
po.GetField('PrivateFieldName'); /*This will get the value of a private variable*/


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: