Geekus Con Livus

Malcolm Anderson's home for Geeks With Lives
posts - 63, comments - 51, trackbacks - 23

My Links

News

Archives

Post Categories

Unit Testing a Private Method

So on a lark, I threw my hat into the ring for Portland Code Camp 2.0, I didn't expect to get accepted, there were already a few sessions on unit testing and nunit but I figured, what the heck, let's gear it for beginners, and do it in VB.Net.  Now I'm in the process of getting ready and I figured I should blog about things and or techniques as they came up.

 

For my first trick, testing a private method.

First rule of thumb: Don't do this.

No really, Don't.

You are locking the internals of your code down with your unit test, if you are having to test a private method, then chances are that method should be public on a different class.

However, if you are sitting in a situation where that kind of refactoring is just not going to be allowed and you still really-really-need-to-get-this-under-test, then this simple process can help.

 

Given

Private Function Add2Integers(byval x as integer, byval y as integer) as integer

    return x + y

End Function

 

You want to get this under test, but you want this to be a refactor (which is NOT another word for maintenance, it's a word for code clean up, not feature changing) which means no changes as far as the users are concerned.

 

Try this, wrap the code in a compiler conditional, and then just call the function, and then pass the value back out.

Now your unit tests can access the private method.

#If DEBUG Then

Public Function public_Add2Integers(byval x as integer, byval y as integer) as integer

     return Add2Integers( x, y) as integer

End Function

 

 

One more time, really, don't do this, if you are not REALLY careful, this will come back and bite you hard.

Print | posted on Thursday, July 13, 2006 7:48 AM | Filed Under [ Agile Development Stupid Code Tricks ]

Feedback

Gravatar

# re: Unit Testing a Private Method

very close minded article. something about old dog and new tricks coming to mind
7/8/2008 12:14 PM | erebus
Gravatar

# re: Unit Testing a Private Method

Have you considered using Aspect Oriented Programming? http://www.khussein.com/wordpress/?p=38
9/26/2008 1:06 AM | Khaled Hussein
Gravatar

# re: Unit Testing a Private Method

I've used a private method to promote code re-use. Obviously this is preferable, as it reduces errors and code maintenance. This code isn't intended to be called directly (as it was part of a public method) so really this is definitely 100% the desired outcome.
4/8/2009 4:41 AM | Terry Wilson
Gravatar

# re: Unit Testing a Private Method

I agree that unit testing private methods is a bad idea to do.

If you absolutely have to do this, try using reflection !
http://www.codeproject.com/KB/cs/testnonpublicmembers.aspx

Cluttering the code with compiler conditionals just because of testing private methods seems a bit cumbersome to me.
8/28/2009 11:30 AM | XiaoP
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: