Efficiently testing with NUnit and log4net

Hello!

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:

None
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.
Single
A test domain is created – this is how NUnit worked prior to version 2.4
Multiple
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
{
[STAThread]
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
NUnit.ConsoleRunner.ConsoleUi.Main(args);
Console.ReadLine();
}
}

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

Technorati Tags: , , ,

2 Responses to “Efficiently testing with NUnit and log4net”

  1. dieta da sopa Says:

    De forma, através da imposto que as notícias criminais imprimem
    sobre em direção a construção da realidade, formando essa opinião pública, através de meio da propaganda do sensacionalismo do crime,
    constatamos que essa mídia sítio fortalece e também dissemina os valores da criminologia positivista.

  2. Jina Zedian Says:

    Good question, James.The second one was actually promulgated by the Securities & Exchange Commission and the first by the Interior Department.I don’t know if Congress & President have plenary power to rescind each regulation (since the underlying law was passed in each case by the President & Congress, and has not since vested in some way with the people or the courts for some other reason).In any case the Republican Congress passed many such resolutions (e.g. repeal Obamacare, move the embassy to Jerusalem) but they all were certain to be vetoed by President Obama in the assurance that the veto would not be overridden by a 2/3 majority of both the Senate and the House.Now of course things are different.¶ But it may be that one or both of them was enacted by an Executive Order signed by Pres. Obama in the last six months.

Leave a Reply