June 03, 2004

Tip for Using App.Config files with NUnit from Visual Studio

At some point you may very well need to call code like the following from your test class library project:

string myValue = ConfigurationSettings.AppSettings["Data.Key"];

Subsequently, when running NUnit you may find that no value is returned from the expression above. This is because NUnit is looking for an xml configuration file with the same name as the assembly + .config in the directory the loaded assembly is running from.
Example: Suppose your assembly is located at C:\Code\My Project\MyAssembly.dll For the ConfigurationSettings.AppSettings call to work properly, you need to have a file at c:\Code\My Project\MyAssembly.dll.config.

Well, an easy way to automate the creation of this file from Visual Studio is to right-click on the project and choose properties from the context menu. Then, under common properties -> build events, add set the following value for the Post-Build Event Command Line:

copy $(ProjectDir)App.config $(TargetPath).config

This will make Visual Studio copy the App.Config file into the appropriate bin\Debug or bin\Release directory (depending on the current configuration) so NUnit will find the file. Happy Unit Testing!

[Listening to: Headstrong - Trapt - Trapt (4:46)]
Posted by Christian at June 3, 2004 02:49 PM |
Comments
Post a comment