Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating the app.config file for unit tests

Tags:

c#

.net

nunit

I have isolated the NUnit tests for my C# app in an assembly called Tests.dll. The associated configuration file is called Tests.dll.config. This is what Nunit uses rather than my app's actual config file. It looks like this (only showing a couple of config options there are lots more):

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <appSettings>
    <add key="useHostsFile" value="true" />
    <add key="importFile" value="true" />

   </appSettings>
</configuration>

To ensure my app is thoroughly tested, I will need to change config options between tests. After I have run a couple of tests, I would like to add some new config values to the file and have these used by subsequent tests. What code would I need to add do this?

like image 496
FunLovinCoder Avatar asked Oct 01 '11 13:10

FunLovinCoder


1 Answers

I recommend to implement a interface IConfig with properties useHostsFile and importFile. Then i would remove all direct dependecies to this file except in the Class ConfigDefault which implements IConfig. In this implementation you load your normal config file. For each test you can implement another Class which also inherits from IConfig. I suggest to use a Dependecy Injection. Ninject is free and easy to use.

like image 139
NickD Avatar answered Oct 05 '22 23:10

NickD