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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With