Posts Tagged ‘unit test’

Unit testing private methods

Mar 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.

Shortcuts when doing Visual studio unit test debugging

Feb 27th, 2013

Short story: http://msdn.microsoft.com/en-us/library/ms182470.aspx

I still miss a shortcut for “Debug the last unit test”.  There is “Repeat the last run” but that runs and doesn’t debug.  Strange since that is what I during times use the most.  I have resorted to marking the test and the alt-s-d-enter.

Also: Resharper interferes with the shortcuts but that is more a facet of life I think.

Unit tests, TDD, BDD

Nov 28th, 2011

Maybe we shouldn’t think of unit tests, TDD or BDD but more of go no go tests. Whatever you automatically test it has a Go/NoGo, Yes/No, Success/Fail result.

Visual studio test project fails compiling due to clashing test name

Jun 25th, 2011

In Visual studio 2010 I have a solution with some projects and a unit test project.  Besides the usual configurations Debug and Release I also have Debug_Test.  One sunny day when compiling the project …

… I  got a dialogue with

Duplicate Detected Loading Add from test.dll

In attempting to load Add form test\bin\debug\test.dll, it was determined that a test with the same id name Add in test\bin\debug_test\test.dll already exists in the catalog.

If you retry, Add from test\bin\debug\test.dll will have its id replaced with a new one.

when I tried to compile my solution with Debug_Test active. (or if it was the other way around)

When I pressed Retry I got

Error loading C:\Test\bin\Debug\Test.dll: The test ‘Add’ from ‘c:\test\bin\debug\test.dll’ that is loading has the same TestId {2704e894-0db5-60e4-53Ed-61c7e6951c9f} as the test ‘Add’ already loaded from ‘c:\test\bin\debug_test\test.dll’.

in the output console.

What was happening?
Check out the emphasis (I created). There are two compile configurations that clash.

I want Debug to compile the whole solution with the debug flag set (to know that everything compiles as it should).  Then I have a Debug_Test configuration that only compiles the parts of the solution that are needed for my automatic tests.  In the inner machinery of Visual studio 2010 there is something that tracks test methods and it looks like it regards Add from Debug configuration and Add from Debug_Test configuration as two different tests, albeit with the same name.
In reality they are the same unit test method code.

The whys and the hows are beyond my scope but I found a work around.
Go to the other configuration (i.e. Debug) and clean the output. Then go back to the one you want to work with (i.e. Debug_Test) and continue as usual.

One could possibly have created Debug_Test without checking Create new project configurations to avoid this too.  If anyone knows, please comment.