Posts Tagged ‘dirty trick’

Unit testing private methods

March 25th, 2014

Read here instead.

I have a dirty but simple way to unit test private methods.

I create a wrapper method that I prefix with UT_ and write a comment that is only used for unit testing.
To be honest I haven’t done this on a public API but I wouldn’t hesitate for a public-within-a-company method.

1
2
3
4
5
6
7
8
9
10
11
12
</del>

<del>private int MyMethod( int myParameter ){</del>
<del> ComplexBusinessLogic...</del>
<del> }</del>

<del>///&lt;summary&gt;This helper method is only used for unit testing. Do Not call it in normal operations.&lt;/summary&gt;</del>
<del> internal int UT_MyMethod( int myParameter ){</del>
<del> return MyMethod( myParameter );</del>
<del> }</del>

<del>

I use InternalsVisibleTo to make internals visible to my unit testing project.

One can play with protected methods and inheritance too.