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:

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:

public class TestSomething
    public void FailingTest()
        Assert.Fail(“Fail description.”);
    public void PassingTest()
    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.


You’re now ready for Red Green Refactor.


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.

4 thoughts on “Getting Started Unit Testing with nUnit

  1. You can test .NET 4.0 assemblies targeting AnyCPU or x64 with NUnit, but you need to manually edit NUnit’s application configuration files.

    Find the files nunit.exe.config and (for console tests) nunit-console.exe.config in subdirectory bin\net-2.0 of your NUnit installation folder, and add the following line after the tag in each file:

    That’s how I’m testing. In fact I’m surprised that x86 assemblies work without this edit!

  2. @Chris: I must have added the requiredRuntime element–which makes it work for x86 assemblies, but that didn’t work for x64 assemblies…

Leave a Reply

Your email address will not be published. Required fields are marked *