首页 文章

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

提问于
浏览
6

我需要在单元测试中从appsettings部分(在app.config中定义)中读取设置 . 我们在这个项目中使用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,我需要将'Custom.config'文件标记为'localtestrun.testrunconfig'文件中的部署项 .

相关问题