Configuring Your Unit Testing Projects
In the first part of this tutorial about unit testing in C# with NUnit we described what is unit testing and which are the main benefits it can bring to you and to the projects you develop. We will now demonstrate how you can set up and organize your projects to work with unit tests on your own systems.
To illustrate the application of unit tests in C# with NUnit, we will create a simple calculator with the following basic features:
We believe this basic set of features is enough for you to understand how unit tests work. If desired, however, you can extend the functionality of this calculator with something else. This can be useful for you as a learning process, so that you may create your own unit tests and see first hand how things work, comparing your tests with those which we will present in the course of these tutorials and asking questions in the comments at the end of each of these articles we are writing about unit testing.
Creating the Projects for the Implementation and for the Tests
To better organize the code of implementation and testing of our system we will create two projects: a project to code our calculator (implementation code) and another project for unit tests that we will use to test our calculator (testing code). To create these projects follow these steps:
- Open the Visual Studio IDE.
- Select the menu option File > New Project.
- In the New Project dialog box, select the Class Library template and enter a name for your project, such as SimpleCalculator, for example, and click OK.
- Wait while Visual Studio generates the calculator project and click Save All (CTRL + SHIFT + S). Choose the location where you want to save your project files and click Save. In the Solution Explorer window right click on your solution file and select Add> New Project to add the unit testing project.
- Again the New Project window appears. Select the Class Library template and enter a name for the test project. If you named the calculator project as SimpleCalculator, we suggest you to name the test project as SimpleCalculatorTests, or some other name that makes sense to you and makes you remember that it is a testing project.
- In the calculator project, rename the file “Class1.cs” to “Calculator.cs” by selecting it and pressing F2. Visual Studio will ask if you also want to change the class name of the file you renamed. Click Yes and the Class1 class will be also renamed to Calculator.
- Do the same for the file Class1 in the SimpleCalculatorTests project, but this time rename the file to CalculatorTests.cs.
After following these steps, you should now have the following file structure in your solution, which you can check in the Solution Explorer window of Visual Studio:
Referencing the Project to be Tested
When we separate the testing code and the production code of our system, we need a way to allow the testing code to have access to the features defined in the production code. To accomplish this we must add, in the testing project, a reference to the implementation project, so that the testing code can exercise the functionality defined in the production code of our program. To add this reference follow these steps:
- In the Solution Explorer window, right-click the testing project root file and select Add Reference… menu.
- Select the Projects tab, then select the project SimpleCalculator that appears in the list and then click OK
- In the CalculatorTests.cs file, import the SimpleCalculator namespace adding a “using” statement immediately before the declaration of the namespace of the testing project, as shown below:
using SimpleCalculator; namespace SimpleCalculatorTests
Now we have a defined structure for how we will separate the production code and the testing code of our calculator, but we still haven’t written any code to implement or test our calculator. We will do this in the next parts of this tutorial. We assume that you already have at least a basic knowledge of how to program in C#, so we will not go into detail about how to create the calculator code. We will, however, provide a detailed analysis on how to create unit tests for C# programs.
First, however, we need to configure our testing project to allow us to implement unit tests. See how you can do this in the following section.
Adding the NUnit Framework to our Testing Project
To write unit tests in C# as well as in any other programming language, you will need a unit testing framework. The unit testing framework will allow you to create, execute and analyze the results of your unit tests, and also provides several features that simplify the process of working with unit tests. In this tutorial we will work with NUnit as the unit testing framework for our project.
NUnit is a unit testing framework that can be used with all dotNet languages, as described in the official website of the tool, www.nunit.com . To use it you need to download it and install it on your computer. This will give you access to the NUnit IDE and also for class libraries that make up the NUnit framework, which you need to reference in your testing project to make use of the features that the framework provides.
If you want to deepen your knowledge about the NUnit Framework (something that unfortunately can’t be done through a tutorial) the book Unit Testing in C# with NUnit can serve as an excellent starting point, giving you a full view of virtually all issues related to this framework for unit testing.
To get the NUnit Framework, visit the link http://www.nunit.com/index.php?p = download , download and install NUnit on your computer. After the installation is complete follow these steps to add the NUnit libraries as a reference for the testing project of your calculator:
- Right click on the SimpleCalculatorTests project and select the menu option Add Reference.
- In the Add Reference dialog box, select the Browse tab and navigate to the bin directory where NUnit was installed in your computer. This can vary depending on the version of your operating system and the version of NUnit, but you should find this folder somewhere like c:\Program Files (x86)\NUnit 2.6.3\bin.
- Still in the Add Reference dialog box and inside the bin folder of the installation directory of NUnit, select the nunit.framework.dll file and click OK. This is the class library that we use to allow us to create unit tests for our programs.
- The last step to be done before we can use the features of NUnit Framework in our project is to import this library in our code. We do this using the following line of code immediately before the namespace declaration in the CalculatorTests.cs source code file:
Conclusion of Part II
With the completion of the first two parts of this tutorial you now do understand better what is unit testing, what are its benefits, why it should be used, and you now also knows how to create and set up unit testing projects for your systems. We made some progress, but there are still some theoretical and practical concepts that need to be analyzed for you to master the essentials needed to produce unit tests professionally in your projects.
In the next part of this tutorial we continue our study of unit testing, analyzing in detail the main features that the NUnit Framework offers us for unit tests development. With that insight we will be better prepared to understand and to program unit tests.
Thank you for reading this article.