Efficiently testing with NUnit and log4net


I have used Nunit for testing .NET based software (libraries) for quite some time now but I have not been entirely happy due to three reasons:

  • The only way to execute the tests was through nunit-gui or nunit-console. Both of these programs read the passed assembly via reflection and call the tests on the contained classes and methods. The problem with this approach is that there is no natural place to perform a single initialization and shutdown procedure that is contained in the component under test.
  • In particular it was not easily possible to configure log4net.
  • Finally, nunit-console and nunit-gui load the component under test in a separate AppDomain and this makes interactive debugging quite slow.

Recently, I have been so annoyed by these restrictions that I took some time to find a solution. On the NUnit Homepage I read that the new version 2.4 now allows more control over the creation of a dedicated AppDomain for the component under test (see the release notes).:

The new /domain option controls of the creation of AppDomains for running tests. The following values are recognized:

No domain is created – the tests are run in the primary domain. This normally requires copying the NUnit assemblies into the same directory as your tests.
A test domain is created – this is how NUnit worked prior to version 2.4
A separate test domain is created for each assembly

This sounded as if it could solve the problem of slow debugging when running under NUnit.
Still, the problem remained that there is no natural single point of initialization. What I wanted was:

  • Create a .NET console application
  • Add classes and methods and tag them as usual as test classes/methods with attributes.
  • Add one time initialization and cleanup code in the applications Main() method.
  • Run the executable in the Visual Studio debugger with breakpoints and automatically execute the tests as defined by the attributes.

I found a posting on Charlie Pooles NUnit blog that gave me the missing hint. All I had to do was to call the desired method in the relevant NUnit assembly:
class TestClass
static void Main(string[] args)

And that’s it!

Here is a summary:

  • Use NUnit 2.4 or newer
  • Create a C# or VB.NET Console application
  • Add required references including log4net and NUnit
  • In your applications Main method add all required initialization (and shutdown if necessary) code.
  • Set the compiled executable as target for debugging.
  • Add (at least) these arguments in the ‘Properties’ dialog of the project: /domain=none <your executable>
  • Add a suitable (standard log4net) App.Config file to your project

Console output of NUnit run

[tags]NUnit,log4net,Visual Studio,Debugging[/tags]

Leave a Reply