Search code examples
c#.netunit-testingapp-config

Can a unit test project load the target application's app.config file?


I am unit testing a .NET application (.exe) that uses an app.config file to load configuration properties. The unit test application itself does not have an app.config file.

When I try to unit test a method that utilizes any of the configuration properties, they return null. I'm assuming this is because the unit test application is not going to load in the target application's app.config.

Is there a way to override this or do I have to write a script to copy the contents of the target app.config to a local app.config?

This post kind-of asks this question but the author is really looking at it from a different angle than I am.

EDIT: I should mention that I'm using VS08 Team System for my unit tests.


Solution

  • The simplest way to do this is to add the .config file in the deployment section on your unit test.

    To do so, open the .testrunconfig file from your Solution Items. In the Deployment section, add the output .config files from your project's build directory (presumably bin\Debug).

    Anything listed in the deployment section will be copied into the test project's working folder before the tests are run, so your config-dependent code will run fine.

    Edit: I forgot to add, this will not work in all situations, so you may need to include a startup script that renames the output .config to match the unit test's name.