Unit Testing with NUnit—Creating a Test
| Visual C# Tutorials |
| Visual Studio .NET Tutorials |
|
| © 2004 O'Reilly & Assoc., Inc. |
Creating a Test
The following steps outline how to create a solution that contains two projects—a project being tested and an NUnit test project:
1. Open the Visual Studio .NET IDE and create a new
blank solution called NPR.
2. Add a new Visual C# class library project called MyApp to
the solution. This is the project you’ll be running the
tests against.
3. Rename Class1 to MyMath. Change the class declaration to
read:
public class MyMath
4. Add a single method called Add that takes two integer arguments
and returns their sum. The completed class follows:
using System; namespace MyApp { public class MyMath { public int Add(int i, int j) { return i + j; } } }
5. Add a new Visual C# class library project called
MyAppTest to the solution. This is the project that will
contain the NUnit tests. It is a good idea to use a name
that allows the test project to be easily identified—for
example, use the name of the project being tested with
the suffix Test.
| WARNING |
| It is generally best to compile application code and test
code into different assemblies. This makes it easy to build a release executable that does not contain any unit tests. |
6. Add a reference to the NUnit framework to the test
project MyAppTest; select Project->Add Reference from
the Visual Studio .NET IDE menu. In the Add Reference
dialog, double-click on nunit.framework in the listbox on
the .NET tab and click OK. (See Figure 3.)

Figure 3. Add a reference to NUnit framework
7. Add a reference to the project being tested: MyApp to the test project MyAppTest. Select Project->Add Reference. In Figure 3. Add a reference to NUnit framework the Add Reference dialog, select the Projects tab. In the listbox, double-click on the project you are testing. Click OK. (See Figure 4.)

Figure 4. Add an NUnit project reference
8. The Solution Explorer now appears as shown in Figure 5.
9. Add the NUnit.Framework types to the test class Class1:
using NUnit.Framework;
10. Add the MyApp types to the test class Class1:
using MyApp;
11. Add the [TestFixture] attribute to the test class Class1 to indicate that the class contains test code. So far we have:
using System; using NUnit.Framework; using MyApp; namespace MyAppTest { [TestFixture] public class Class1 { } }

Figure 5. A Solution Explorer with references
12. Create test method MyAddTest in the test class. Remember
that this method must be both public and void, and
take no arguments. Identify the test method by decorating
it with the [Test] attribute:
[Test] public void MyAddTest( ) { }
13. Write the test. In this case, test the Add method in MyMath:
[Test] public void MyAddTest( ) { MyMath m = new MyMath( ); Assert.AreEqual(m.Add(2, 3), 5); }
The completed test class follows:
using System; using NUnit.Framework; using MyApp; namespace MyAppTest { [TestFixture] public class Class1 { [Test] public void MyAddTest( ) { MyMath m = new MyMath( ); Assert.AreEqual(m.Add(2, 3), 5); } } }
14. Compile your code.
15. Run the test as described in the next section "Running a Test" (open the test assembly MyAppTest.dll in the NPR\ MyAppTest\bin\debug directory from the test runner GUI). Figure 6 shows the test runner GUI after running the test.

Figure 6. An NUnit test runner GUI results
|

