Refactoring NUnit Unit Tests to VSUnit
If you have been using NUnit to write your unit tests, and you have access to Visual Studio 2005 Team System you'll want to consider upgrading your NUnit tests to the VS Unit Test Framework, which I'll call VSUnit for short. There is a walkthrough that you should read to get an idea of the VSUnit product features.
I recently upgraded some old unit tests to see what was involved, and it was really straightforward. First I'll make some assumptions. You have a VS 2003 solution that you've recently upgraded to VS 2005 and which contains some NUnit test projects. I'll also assume you are using C# as your development language.
Here's a rundown of what I did:
Add a new Test Project to your solution (Test menu -> New Test -> Unit Test). This is going to add a Solution Items folder containing .vsmdi and .testrunconfig files. It should look something like the image on the below in your Solution Explorer.
I usually delete the AuthoringTests.txt and ManualTest.mht files, the Additional Test Attributes #region, and all the // TODO: and /// comments.
Drag all your .cs files from your old NUnit project. This will copy them into the VSUnit project.
Back-up your old NUnit project somewhere, then remove the project from the solution and delete or rename the project directory on disk.
At this point you should also rename your new VSUnit project. If you right click Rename it, bear in mind that that it sadly won't update the directory name. You'll have to close the solution, rename the directory in Explorer and open the solution .sln file in Notepad and change the directory before TestProject1.csproj.
When you're done with the tidy-up you are ready to start converting the test code. The diagram below shows the basic conversions. Just follow the conversions, 1 through 7. li>When you're done with the tidy-up you are ready to start converting the test code. The diagram below shows the basic conversions. Just follow the conversions, 1 through 7.
Note that VSUnit also has support for assembly wide setup and cleanup which you might want to make use of. But just focus on the conversion for now. Note the change to a static method for the class setup and cleanup, and the addition of the TestContext parameter.When you're done with step 6, try to compile your project. If it compiles, you are ready to run it and see what happens! Select the test then Test -> Start Selected Test Project Without Debugger.
Note that VSUnit also has support for assembly wide setup and cleanup which you might want to make use of. But just focus on the conversion for now. Note the change to a static method for the class setup and cleanup, and the addition of the TestContext parameter.
When you're done with step 6, try to compile your project. If it compiles, you are ready to run it and see what happens! Select the test then Test -> Start Selected Test Project Without Debugger.
You're probably wondering about all those Asserts in your NUnit tests. The really good news is that the Assert class exists in VSUnit and the methods signatures and behavior are by in large the same. The compiler will tell you where there are problems.
Other things to note. You can debug your unit tests just like with NUnit by setting breakpoints in the unit test and then selecting Test -> Start Selected Test Project With Debugger. The TestContext parameter also lets you do some really cool stuff when setting up and configuring your tests.
You can take an existing set of unit tests and easily run the methods in specific orders by adding one or more Ordered Tests to your unit test project. The only way to do this in NUnit was to alphabetize your test method names!
There's lots of other fun stuff available too. My favorite feature by far is to turn on coverage profiling for the tests, by going to Test -> Edit Test Configurations then select Coverage and select the assemblies you want to check the coverage for. I think you'll soon agree that these two tools were made for each other.