单元测试程序集的app.config:如何使appsettings中的“file”属性生效?

6
我需要在单元测试中从app.config文件中的appsettings部分读取设置。我们在这个项目中使用mstest。
假设这是app.config文件:
<configuration>
 <appSettings>
   <add key="MyAppSetting" value="MyAppSettingValue"/>
 </appSettings>
</configuration>

这是相应的测试,它在这个设置中通过:
[TestClass]
public class ConfigurationTests
{
    [TestMethod]
    public void can_read_appsettings()
    {
      string value = ConfigurationManager.AppSettings.Get("MyAppSetting");
      Assert.AreEqual("MyAppSettingValue", value);
    }
}

现在当我尝试将appSettings部分移动到custom.config文件中时,这个测试失败了。
这是我的app.config文件现在的样子:
<configuration>
 <appSettings file='Custom.config' />
</configuration>

我将Custom.config文件添加到我的项目中(构建操作为“始终复制”):
 <appSettings>
   <add key="MyAppSetting" value="MyAppSettingValue"/>
 </appSettings>

在控制台应用程序中执行相同操作时,这是有效的。是否有办法使其在单元测试程序集中也能工作?
1个回答

8
我找到了答案。使用mstest,我需要在“localtestrun.testrunconfig”文件中将“Custom.config”文件标记为部署项。

耶!我喜欢配置东西来到达我的配置! - A.R.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接