单元测试程序集的app.config:如何使appsettings'文件'属性工作?

jer*_*enh 6 .net mstest assemblies app-config

我需要在单元测试中从appsettings部分(在app.config中定义)中读取设置.我们在这个项目中使用mstest.

说这是app.config:

<configuration>
 <appSettings>
   <add key="MyAppSetting" value="MyAppSettingValue"/>
 </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这是相应的测试,它通过了这个设置:

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

现在,当我尝试将appSettings部分移动到custom.config文件时,此测试失败.

这就是我的app.config文件现在的样子:

<configuration>
 <appSettings file='Custom.config' />
</configuration>
Run Code Online (Sandbox Code Playgroud)

我将Custom.config文件添加到我的项目中(使用构建操作'始终复制'):

 <appSettings>
   <add key="MyAppSetting" value="MyAppSettingValue"/>
 </appSettings>
Run Code Online (Sandbox Code Playgroud)

在控制台应用程序中执行相同操作时,这是有效的.有没有办法在单元测试装配中使这个工作?

jer*_*enh 8

我找到了答案.使用mstest,我需要将'Custom.config'文件标记为'localtestrun.testrunco​​nfig'文件中的部署项.