Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app.config for unit test assembly: how to make the appsettings 'file' attribute work?

I need to read a setting from the appsettings section (defined in app.config) in a unit test. We're using mstest in this project.

Say this is the app.config:

<configuration>
 <appSettings>
   <add key="MyAppSetting" value="MyAppSettingValue"/>
 </appSettings>
</configuration>

Here's the corresponding test, which passes in this setup:

[TestClass]
public class ConfigurationTests
{
    [TestMethod]
    public void can_read_appsettings()
    {
      string value = ConfigurationManager.AppSettings.Get("MyAppSetting");
      Assert.AreEqual("MyAppSettingValue", value);
    }
}

Now when I try to move the appSettings section to a custom.config file, this test fails.

This is what my app.config file looks like now:

<configuration>
 <appSettings file='Custom.config' />
</configuration>

I added the Custom.config file to my project (with build action 'copy always'):

 <appSettings>
   <add key="MyAppSetting" value="MyAppSettingValue"/>
 </appSettings>

When doing the same in a console application, this works. Is there a way to make this work in a unit test assembly as well?

like image 628
jeroenh Avatar asked Sep 15 '09 05:09

jeroenh


1 Answers

I found the answer. Using mstest, I needed to mark the 'Custom.config' file as a deployment item in the 'localtestrun.testrunconfig' file.

like image 82
jeroenh Avatar answered Oct 22 '22 04:10

jeroenh