Garry Pilkington


Application Developer
Liverpool, UK

How do you test private methods?

This morning I read a post by Davy Brion who was explaining a technique to test private methods. Although the post was interesting, it was a comment by Rafferty Uy that got me thinking. He suggests that you make your method protected instead of private and have the testing class inherit from this class. There is much debate as to whether you should be testing private methods at all, and as I am fairly new to unit testing, I have only ever tested my public methods.

From Microsoft:-

protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.

(http://msdn.microsoft.com/en-us/library/ms173121.aspx)

So that is fine if your tests live in the same project as the code being tested. You just inherit from the class. If however you have all your tests in a separate project, then you simply need to have a reference to the .dll or .exe of your main project with the methods that need testing. Add the namespace to your using list and inherit the class in your test class.

   1: using NUnit.Framework;
   2: using Spike.PrivateMethodTest;
   3:  
   4: namespace Spike.UnitTests
   5: {
   6:     [TestFixture()]
   7:     public class Class1 : Spike.PrivateMethodTest.Program
   8:     {
   9:         [Test()]
  10:         public void TestPublicMethod()
  11:         {
  12:             string expectedValue = "this is public";
  13:             string actualValue = Program.MyPublicMethod();
  14:             Assert.AreEqual(expectedValue, actualValue);
  15:         }
  16:  
  17:         [Test()]
  18:         public void TestPrivateMethod()
  19:         {
  20:             string expectedValue = "this is private";
  21:             string actualValue = Program.MyPrivateMethod();
  22:             Assert.AreEqual(expectedValue, actualValue);
  23:         }
  24:     }
  25: }

This technique seems by far the simplest I have come across to allow you to test private methods.

Posted: Nov 21 2008, 04:42 PM by capgpilk | with 5 comment(s)
Filed under: , ,

Comments

Chris said:

There's always Reflection for Privates as well.

# November 21, 2008 1:05 PM

Rachit said:

The Visual Studio 2008 (version above Professional only , I believe) testing framework provides a way to test the Private method. It creates some Shadow class and you can test that way. Just right click on your private method and create unit test for it. It works very well!

# November 21, 2008 2:57 PM

Rob said:

# November 21, 2008 3:07 PM

capgpilk said:

Thanks guys, great input. That's 3 other ways to test private methods. I think I will have to look deeper into these methods and see how they can be integrated into our CI environment. Enough content for a post I think. Watch this space.

# November 24, 2008 4:42 AM

bet365 said:

Good day I was luck to discover your Topics in digg

your post is impressive

I get a lot in your Topics really thanks very much

btw the theme of you website is really fabulous

where can find it

# December 1, 2010 8:43 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)