piotrwest
piotrwest

Reputation: 2166

NUnit appSettings file attribute (linked config file) is not seen by ConfigurationManager in test

I have NUnit test (version 2.6.4) test. It uses ConfigurationManager.AppSettings["foo"] to retrive a configuration setting from the app.config file (which is in the test project). This is my App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="bar.config">
    <add key="thisSettingIsVisible" value="yes, indeed"/>
  </appSettings>
</configuration>

and this is bar.config file:

<appSettings>
  <add key="foo" value="this setting isn't visible"/>
</appSettings>

I'm using ReSharper 10 test runner to execute the test. bar.config file is copied to the bin/Debug directory. In fact, that configuration was working some time ago, but stopped. Any clues what can be wrong?

Now, I've figured out a workaround, but I'm not happy with this solution:

private static void InitializeAppSettings()
{
    var exeAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    var assemblyName = exeAssembly.GetName().Name + ".dll";
    var testDllFolder = new Uri(System.IO.Path.GetDirectoryName(exeAssembly.CodeBase)).LocalPath;
    var openExeConfiguration = ConfigurationManager.OpenExeConfiguration(Path.Combine(testDllFolder, assemblyName));
    foreach (var setting in openExeConfiguration.AppSettings.Settings.AllKeys)
    {
        ConfigurationManager.AppSettings[setting] = openExeConfiguration.AppSettings.Settings[setting].Value;
    }
}

BTW. I can't abstract away ConfigurationManager usage form existing, legacy code.

Upvotes: 2

Views: 468

Answers (2)

Alexander Kurakin
Alexander Kurakin

Reputation: 13523

If you use R# 10.0.0 or R# 10.0.1 - it is a known issue for such builds and it has been fixed in R# 10.0.2 build.

Upvotes: 1

chief7
chief7

Reputation: 14383

I replicated your use case and found that my additional config worked in the context of an ASP.NET site but the additional appSetting was null in a test project until I changed the Copy to Output Directory property to Copy Always

enter image description here

Upvotes: 2

Related Questions