Technology with opinion

Wednesday, July 27, 2005

Run NUnit Tests on Compilation

Test driven development is an approach to programming where you write your unit testing code to test for the functionality & requirements first. Then code is written to write the unit of code (class, etc).

For .Net programmers, the tool of choice for unit testing is NUnit. For more information about NUnit, see toolkit below.

I recommend creating a project for your unit testing, .UnitTesting will suffice. To automate the process of testing, it is helpful to have unit testing performed upon compilation, thus every time your code changes you can rerun your unit tests and not have to manually rerun NUnit.

To do this, you need to run NUnit's commandline utility, located in NUnit's Program Files folder \bin.

Create your testing class and methods:

[TestFixture]
Class1
{
[Test]
public void Test1()
{
Project1.MyUnit1 minute = new Project1.MyUnit1();
Assert.AreEqual(myUnit.Add(1,2), 3); // assert an exception if 1 + 2 <> 3
}
}

Next, go to the project properties for your NUnit project, click Build Events on the left-hand side. In the Post-build Event Command Line field paste the following:

"%programfiles%\NUnit 2.2\bin\nunit-console" "$(SolutionDir)\Project1.nunit" > "$(SolutionDir)\Project1.nunit.log"

This assumes your .nunit file is in the root of your solution. Variables above might need to be changed to work with your project. Once you build this logic into your solutions, unit testing can become seamless.

No comments: