Click here to Skip to main content
13,551,492 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 9 Jun 2011
Licenced CPOL

Changing App.Config on the fly for unit testing

, 10 Jun 2011
Rate this:
Please Sign up or sign in to vote.
Changing App.Config on the fly for unit testing
First of all, a short description and the problem which I've faced. Our team has a list of unit tests that are more likely to integration tests. These tests before starting deploy database make some changes in the current environment and after running drop database and return environment back to its initial state. Of course, all configuration such as database connection strings and other we put to App.Config. Each of us runs these tests on a local machine using ReSharper and everything was fine until we had decided to run tests on build-machine sing standard "nunit-console" application. We created a standalone NUnit solution where we put all references on all test projects. When we tried to run tests on build-machine, we found out that all NUnits have requirement that app.confing should have the same name as as NUnit project + . config, i.e., we had to unite our dozens of app.configs in one.

Our solution is to change app.config before each test will be run.

Here is the code example to do it:

public void SetUp()

SetUp of NUnit framework guarantees that method marked with this attribute will be invoked before any test. So we change app.config before running tests.

Here is the implementation of SetAssemblyConfig:
public static void SetAssemblyConfig(Assembly assembly)
	Configuration currentConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
	Configuration assemblyConfiguration = ConfigurationManager.OpenExeConfiguration(new Uri(assembly.CodeBase).LocalPath);
	if (assemblyConfiguration.HasFile && string.Compare(assemblyConfiguration.FilePath, currentConfiguration.FilePath, true) != 0)

I've posted this tip because I couldn't find anything about this topic. Hope it can help somebody and saves time on debugging and tracing unit tests.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

Generaluseful Pin
Shahriar Iqbal Chowdhury9-Jun-11 10:16
memberShahriar Iqbal Chowdhury9-Jun-11 10:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180515.1 | Last Updated 10 Jun 2011
Article Copyright 2011 by NeoPunk
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid