1

In my projet, I have an App.config where I store all the configuration. I have used several sections to have a clean and readable App.config file. Each section deals with a certain controller. Here is an example of what I have in my file :

<configuration>
  <configSections>
    <section name="applicationConfiguration" type="System.Configuration.NameValueSectionHandler" />
    <section name="databaseConfiguration" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>

  <ApplicationConfiguration/>

  <DatabaseConfiguration>
    <add key="UserID" value="user"/>
    <add key="Password" value="pwd"/>
  </DatabaseConfiguration>

</configuration>

It works perfectly in my project but the issue is with the project.test. I try to copy the app.config in the test project (and in properties select « always copy ») but it doesn’t change a thing. The App.config file exist in the Bin/Debug of the test projet but for the test, it doesn't exist.

So I tried to add elements in the App.config on the fly. It works well by adding new key to the AppSettings section by the Configuration Manager :

ConfigurationManager.AppSettings.Set("UserID ", " user ");

But I’m stuck when I want to use sections ; I can’t add section on the fly to the ConfigurationManager. Here is where I am :

System.Configuration.Configuration tempConfig = (System.Configuration.Configuration) ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                dbConfig = new NameValueCollection();
                dbConfig.Add("UserID ", "user");
                dbConfig.Add("Password ", "pwd");
                tempConfig.Sections.Add("DatabaseConfiguration ", (ConfigurationSection) dbConfig);

Do you guys have any ideas how to solve this ?

Thanks in advance !

SebDev43
  • 21
  • 3
  • I am not sure this is still accurate, but you used to be able to get this to work by naming changing the name of your app config file to match your test dll name. So if you dll was named some.tests.dll you would want to rename your app.config to some.tests.dll.config. Sorry. my nunit skills are rusty and I don't have access to a .Net environment to test. – Mike Two Mar 01 '19 at 03:35

0 Answers0