First, I would like to thank Charlie Pool, Jordon Terrell and Simone Busoli for blessing the general masses with the NUnit testing framework. I’m a proponent of automated unit testing. Incorporating automated unit testing in your code-build-deploy lifecycle will reduce (not necessarily eliminate) post production errors, assist in the early discovery of errors introduced from refactoring code, increase code confidence…
Unit tests give developers and testers a quick way to check for logic errors in the methods of classes in C#, Visual Basic .NET, and C++ projects. A unit test can be created one time and run every time that source code is changed to make sure that no bugs are introduced. – Thank you MSDN for this eloquent statement.
In this tutorial, I will overview the procedures to integrate the NUnit testing framework into a Visual Studio Solution and develop a unit test.
Visual Studio Setup
Writing a simple Test
NUnit is a unit-testing framework for all .Net languages. I’m by no means an expert on all the features of this unit-testing framework. The methods I know, I have made extensive use of and I am very satisfied with the ease of implementation. You can write test methods that are one offs (They test one static data set against one static algorithm) or you can write test methods that will consume test case sources (test data sets). Test methods consuming test case sources will iterate over the test data and evaluate them with one test method. I highly encourage you to research and employ MOQ and Fluent Assertions in concert with the NUnit framework.
Visual Studio Setup
Start by adding a new project to the solution.
Select the class library project and name it. I typically follow the naming convention Solution.Tests where Solution is the name of my solution
Once the class library project has been created, it is time to install NUnit using the Nuget Package Manager.
From the Nuget Package Manager, use the search text box to locate the NUnit package.
Select the Tests project
The final step to configure Visual Studio is the installation of the NUnit Test adapter from the Visual Extensions and Updates Manager. The NUnit Test adapter integrates your NUnit tests into Visual Studio to facilitate their utilization from the Test Solution explorer.
From the Tools menu, select the Extensions and Updates… command
From the Extensions and Updates Manager, download the NUnit Test Adapter.
Once the NUnit Test Adapter is installed, you will need to select the Restart Now command to restart Visual Studio completing the installation.
Visual Studio is configured, we can begin consuming NUnit to write unit tests.
Writing a simple Test
Lets begin with writing a simple test.
Feel free to copy and past the following code.
- using NUnit.Framework;
- namespace HighSpeedLowDrag.Tests
- public class ArticleServiceTest
- public void myFirstTest()
- int numberOne = 1;
- int numberTwo = 2;
- numberOne < numberTwo,
- “It must be the new math the kids keep talking about.”);
Once you have the code copied into your new Test Class, rebuild your solution. Once the build is complete, you should see the test “myFirstTest” in your Test explorer. From the Test explorer, right click the test and select the “Run selected test” command. The test will be annotated with a green check indicating the Test passed. Congratulations, you wrote your first successful unit test.
Now lets breakdown this example. For this class to be recognized as a class containing test methods it must be adorned with the TestFixture attribute. For the method myFirstTest() to be recognized as a test method in the Test explorer, the method must be adorned with the Test attribute. Within the test method, you can perform any algorithm to assess the validity of your code. The Assert class’s True() function assesses the supplied logic and determines whether the Test succeeds or fails.
NUnit Test Adapter
MSDN – Creating and Running Unit Tests for Managed Code
MSDN – Working with Unit Tests
Twelve Benefits of Writing Unit Tests First
Thoughtworks – Refactoring