Tuesday, 15 January 2008

Setup configuration file for a unit test project

It's a common scenario when we need to have a configuration file when doing unit testing, especially for testing a database access object. If the project tested is a Windows application, the configuration in the project is app.config, for a web project it's web.config. The configuration file must be copied to the testing bin folder to read configuration data, with the file name AssemblyFile.dll.config. We could copy the config file to the bin\debug or bin\release folder in the testing project, but then we are maintaining multiple copies manually.

To avoid this, we can take advantage of the Post-build event of Visual Studio. First we need to copy app.config file from testing Windows application project to the root folder of the testing project, or web.config file from the Web project and rename it to app.config. Secondly in the testing project property window, click on Build Events tab, we can then enter the following into the Post-build event command line box:

copy /Y "$(ProjectDir)App.config" "$(TargetDir)$(TargetFileName).config"

This command will copy and rename the configuration file to the target folder automaticallty.

No comments: