Prerequisites:
In order to follow along in this tutorial, you will need the following
- Visual C# 2010 Express (any version Visual Studio 2010 should do)
- NUnit (Please note that in order to get the NUnit client to run, you need to have .Net Framework 2.0 installed. It won’t matter if you are using a different version of the framework for your development.)
NUnit Installation:
To install NUnit click on the “.msi” file from the download page, link above, and double click the file and open the file once it’s been downloaded. The “Complete” setup type will be fine. Everything else is pretty much your standard installation. Once the installation is complete, the necessary DLLs will be on your computer and you will be ready to incorporate them into your test project.
Writing NUnit Tests:
Writing a test with NUnit is fairly straight forward. Lets start with the following method called “AddTwoNumbers”.
namespace NUnitTutorial { public class MathObject { public int AddTwoNumbers(int num1, int num2) { int result = num1 + num2; return result; } } }What you can see is that the method simply adds two numbers together and returns the result. Lets write an NUnit test to make sure this method is returning what we expect. We need to do the following to write a test:
- Add a new class library project to your solution. This new project should have the following naming conventions “ProjectUnderTest_Test” where “ProjectUnderTest” is the name of the project that contains the methods you are writing the tests against.
This project will contain all of your tests. Above you will notice that I have a project called “NUnitTutorial” that contains the “AddTwoNumbers” method and a project called “NUnitTutorial_Tests” that will contain the unit tests for that method.
- Next add the following DLL to the test project “nunit.framework”
- Write our test.
using NUnit.Framework; using NUnitTutorial;Now lets add our test to the MathObject_Tests.cs file. We first need to let NUnit know that this class contains tests that need to be ran. To do this we mark the class with the following attribute “[TestFixture]”.
using NUnit.Framework; using NUnitTutorial; namespace NUnitTutorial_Tests { [TestFixture] public class MathObject_Tests { } }Now, lets add the test method “AddTwoNumbers_returns_3_when_given_1_and_2”. (I always like to name my test methods this way so that I know what method they are testing as well as what the expected inputs and outputs are.) Once we add this method we need to add the attribute “[Test]” to it so that NUnit knows that this is a test to be ran. Note, that all test methods should be void methods, not return anything.
using NUnit.Framework; using NUnitTutorial; namespace NUnitTutorial_Tests { [TestFixture] public class MathObject_Tests { [Test] public void AddTwoNumbers_returns_3_when_given_1_and_2() { } } }All that’s left is to call the method with our two inputs and assert that the result from the method is what we expect.
using NUnit.Framework; using NUnitTutorial; namespace NUnitTutorial_Tests { [TestFixture] public class MathObject_Tests { [Test] public void AddTwoNumbers_returns_3_when_given_1_and_2() { MathObject mObject = new MathObject(); int calculatedResult = mObject.AddTwoNumbers(1, 2); int exptResult = 3;
Assert.AreEqual(exptResult, calculatedResult);
}
}
}
The above test simply calls the “AddTwoNumbers” method, giving it a 1 and a 2 and asserts that the result from the method is what we are expecting, which is a 3. Now, that we finished writing the test, we need to run it. Build your solution and generate those DLLs.
Running NUnit:
Start up the NUnit client by going to Program Files –> NUnit x.x.x –> NUnit. Once it is up and running, click on File->Open Project and navigate to where the DLLs generated from the test project are located. You want to load the test project DLL into NUnit.
Thanks!
ReplyDeleteIt's very clear explanation of NUnit testing for beginners.
.net developer
Alex
Cheers for the tutorial.
ReplyDeleteRegards.
Good example Jerrel, many thanks. I started off by creating NUnitTutorial as an executable not a class library. That failed during the NUnit test run, then I thought why am I testing against a .exe, I want to test little sections in a dll class library. My mistake was a good learning exercise.
ReplyDeleteThis is a great tutorial. I have used SharpDevelop and still was able to go through all the tutorial easily since it was written in a clear manner.
ReplyDeleteVery Useful and clear..Thanks to the Author...
ReplyDeleteNice tutorial. I did find a missing step, which should be done between steps 2 and 3: Add a reference to NUnitTutotial to NUnitTutorial_Tests.
ReplyDeleteNice article, it carries heaps of useful information about NUnit testing in .Net.
ReplyDeleteGreat article.
ReplyDeleteThank you.
Great article for beginners like me. Thanks.
ReplyDeleteIt was good , as reported by Another anonymous , that missing step should be added.
ReplyDeleteWe have to incorporate it only in test project template or any other template?
ReplyDelete