Using .config files with NUnit and TestDriven.NET

I just emailed Jamie Cansdale at TestDriven.NET but in the meantime, this might come in useful to someone out there using TestDriven.NET...

As discussed in the FAQ you can use access appSettings values from a .config file.

I discovered a way of making this just a little bit easier to manage:
  • Add an App.Config file to your Class Library root folder along with all your tests, and rename it to match the name of the .dll of the unit test class library (e.g. MyProject.Tests.dll.config)
  • Add your config settings (i.e. appSettings and so on) in the normal way
It this point, whentrying to run a test, TestDriven (or possibly NUnit itself?) will not be able to findyour config settings because the config file should really have beenplaced in the bin folder but we'd then need to manage 2 copies (one foreach of the Release and Debug folders), and anyway, it's easier for you to seeand manage the config file if it's in the project root folder along with all your other classes ratherthan having to move it to the bin folder (because we'd then have tomess about doing 'show all files' and 'include in project')

To get round this problem without having to move the config file:
  • Add a pre-build command to the unit test project:
    (i.e. under the Project Properties dialog (Build Events | Pre-Build Command Line))
  • enter the following command:
    xcopy $(ProjectDir)$(TargetFileName).config /YR

this will copy the [tests.dll].config file to the (Release or Debug) bin folder

Assumptions:
  • This advice applies to VS2003, it is probably very similar for VS2005but I've not tried it (in fact, if I remember correctly, VS2005 has amuch more intuitive method for copying files into the bin directory atbuild time which won't need an xcopy command...)
  • Your unit tests reside in their own Class Library project
UPDATE: Jamie Cansdale has also now posted on this topic