Welcome to MSDN Blogs Sign in | Join | Help

Jose's Blog

Musings on Software Engineering and Architecture
Loading .config files in NUnit

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.

Posted: Monday, May 31, 2004 4:46 PM by josealmeida
Filed under: ,

Comments

breaman@breaman.net (breaman) said:

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.
# June 1, 2004 5:10 AM

ajunod@NOSPAMMEsnet.net (Aaron Junod) said:

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
# June 1, 2004 1:21 PM

Hans Sissing (J.J.Sissing@[nospam]hetnet.nl said:

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!
# June 16, 2004 2:05 PM

José Almeida said:

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.
# June 16, 2004 2:30 PM

Hans Sissing (J.J.Sissing@[nospam]hetnet.nl said:

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.

# June 17, 2004 8:34 AM

Hans Sissing (J.J.Sissing@[nospam]hetnet.nl said:

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...
# June 19, 2004 10:20 AM

José Almeida said:

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. :-)
# June 20, 2004 1:44 AM

Mike Lorengo said:

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!
# June 30, 2004 5:06 PM
New Comments to this post are disabled
Page view tracker