Jose's Blog

Musings on Software Engineering and Architecture

Loading .config files in NUnit

Loading .config files in NUnit

  • Comments 8

Last week I gave a presentation at Microsoft DevDays 2004 in Lisbon on Test-Driven Development. A day later I got this interesting question:

"(...) if I'm testing a DLL assembly that uses a .config file to read in data, when I run the tests, NUnit loads it's own .config file and all my tests fail. How can I solve this ?"

I also had this same problem a while back. There are two separate answers to this question as far as I could tell:

  1. If we're using the NUnit GUI than it's just a question to specify which .config file to load (Project -> Edit)
  2. If we're running NUnit from the console, copy the config file to the directory where the test assembly is located (tipically bin\Debug) and rename the config file to <test assembly>.config (ex: UniTests.dll.config)

This will cause NUnit to load the required .config file.

  • One other interesting thing I have found with nunit is that if you are testing multiple assemblies utilizing the .nunit project file, you can add all of your configuration information in to a file called .nunit.config and it will then load the configuration information from that file for the tests.
  • I have also created .nunit files and run them from the command line. This makes it easy to use the same nunit file in an automated build process. re : http://blog.iceglue.com/archive/2004/05/12/168.aspx
  • If I enable VS support, hence I can load a csproj file instead of the assembly, then my app fails to find the config file, even if I configured it through "Project | Edit".
    Which config file is read?

    Thanks!
  • With VS Support enabled you also have to specify the config file in the NUnit GUI (Project -> Edit). Place the .config file in the same dir as the assembly, set the Application Base to the same directory and specify the .config filename correctly. That should do the trick.
  • That's what I did, but it does not seem to load the config file or look in the right directory.
    If I load the assembly, it works, if I load the project it refues to load the config, and I put a config file in almost every directory that possible could be read, and named the file in al it's variations:
    [test-assembly].dll.config
    [test-assembly].config
    app.config.

  • Ok, if found out which config file is loaded if you enable VS.NET support and load your project instead of the assembly.
    Query the following property in your code:

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

    The result is: "d:\apps\myproj\myproj.csproj.config".

    And yes, copying "d:\apps\myproj\bin\degug\myproj.dll.config" to the above location and name does the trick.
    Hence, there is some bug in NUnit that does not set the ConfigurationFile property to a value you would expect...
  • Glad you managed to solve your problem, though I have to admit I wasn't able to repro it. With my setup everything just worked by specifying the application base and correct .config filename in the NUnit GUI.
    Thanks for posting your findings. :-)
  • Just to reiterate what Hans said,
    I too had a problem with NUnit 2.2 reading my config files. I was using a .nunit project file. I queried the value of

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile =

    @"C:\Dev\Cellar\CellarSolution\Cellar.Data.Tests\bin\Debug\Cellar.Data.Tests.nunit.config"

    So in my case myproj reads myproj.nunit.config in the \bin\Debug folder

    So, if you're having a problem query the value in your test assembly

    Thanks Hans!
Page 1 of 1 (8 items)