Data driven unit testing (Part 1)

Now that I have installed Whidbey Beta 2. I thought that I should implement the tests I designed in my recent post on using cyclomatic complexity to design tests.

To start, I have created a class library called Samples, with a shell for single static CheckTriangle method. I have added the return null statement so we can compile the class.

The easiest way to setup our test project is to right click on the method name and select create tests.

In the resulting dialog, ensure that I select C# tests in the output project drop-down. For some reason the default is VB, a opposed to the language of the library that you are creating the tests for. I am going to call the project Teknologika.Samples.Tests. Visual Studio will now create a new test project, which includes 2 tests: a UnitTest1.cs class and a ManualTest1.mht manual test.

I am not going to use the manual test, so I will simply delete it. I want to pause here for a second to reflect on what we have already done. In around 30 seconds we have created a new test project and have a test shell ready to run. We have already saved a heap of time that we would normally have to spend creating a new class library and setting up nunit just to get to this point. Cool.

The generated code shell will be the starting point for my tests. To keep things organised I have copied my tests into a text document and added that to the test project. I am also going to rename the UnitTest1.cs file to Samples.Tests.cs so it corresponds 1:1 with my class’ implementation. So after the tidy up, this is what my solution now looks like.

The next thing we need to do is to create a default test script that we will data drive. I have changed the generated test into the following:

Once the test finished, and the solution is finished, we should jump in to the Test Manager, by selecting Manager and Execute Tests on Visual Studio’s new test menu, then select and run out test which should pass.

In the next post, we will complete the CheckTriangle method and change this test into a data driven one.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>