Getting Started Unit Testing with nUnit

Published on Sunday, April 18, 2010

Source

Getting Started Unit Testing with nUnit

Getting started with unit testing with nUnit is easy. First download and install the latest version of nUnit, which can be found here: http://www.nunit.org/index.php?p=download

Next, you need to decide where you want your unit tests will live. If you're developing a shrink-wrapped system that will be deployed to multiple customers or clients, you'll likely want to have an independent project to house your tests. If you're developing an enterprise system—and the software won't be deployed outside your enterprise—then having the tests within an existing project (usually the highest-level project) is a valid option.

Note: The only configuration I've been able to get to work with Visual Studio 2010 compiled assemblies is to have my assemblies compile to x86 and use nunit-x86.exe to run the tests.

Wherever you decide to keep your unit tests, you need to add a reference to the nUnit assembly. Add a reference to nunit.framework.dll.

Next, you can write some tests. For example:

[TestFixture]
public class TestSomething
{
[Test]
public void FailingTest()
{
Assert.Fail("Fail description.");
}
[Test]
public void PassingTest()
{
Assert.Pass();
}
[Test]
public void InconclusiveTest()
{
Assert.Inconclusive("Reason for lack of conclusion.");
}

}

Then, compile, and load your test assembly into nUnit. Clicking the Run button should result in something like the following in nUnit.

image

You're now ready for Red Green Refactor.

Yellow?

What about yellow? Red means a test failure, Green means a test pass. But, what about tests that don't have underlying code to be tested. Some may say that this is just another failing test. But, early in your testing efforts you may end up being overwhelmed by red. This could lead to missing a failed test actually is testing code. The alternative is to use the Assert.Inconclusive(). This actually gives you a purple, not yellow, result in nUnit, but you get the idea.

Where to from Here?

Now we've got an external test runner and the unit tests for the runner to execute and inform you of red, green, and yellow; you may want to automate the testing process. I would recommend looking at tools like TestDriven.NET, Resharper, and Continuous Integration tools like CruiseControl.NET

Of course, you've always got the option using the built-in automated testing tools in Visual Studio 2010 Professional, and the Continuous Integration abilities of Visual Studio Team System.

comments powered by Disqus