Monday, 26 April 2010

Unit Testing: Accessing AppSettings within the context of the test

I recently had an issue with my tests failing, I realised it was because when using my particular gtest suite, the debug files are not stored within bin\Debug, but rather somewhere else; and the configuration files are not being copied!

This was quite dramatic as my DB connection strings are stored as settings, and needed these to produce tests against the data. The simpliest way to solve this problem is to add a post-build event to the test project. This psot build event will copy the configuration file to the target destination. The testing suite will then pick up these settings as and when requested.

1. Right click and properties on the test project
2. Click Build Events Tab.
3. Add the following post build event...
copy /Y "$(ProjectDir)app.config" "$(TargetPath).config"

This will ensure your config file is copied to its destination, where your test suite (I.e. NUnit can pick it up).