2009-09-15 30 views
6

我需要從appsettings部分(在app.config中定義)中讀取單元測試中的設置。我們在這個項目中使用mstest。單元測試程序集的app.config:如何使appsettings的'文件'屬性有效?

之所以這樣說,是在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部分移動到自定義。配置文件,此測試失敗。

這是我的app.config文件看起來像現在:

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

我加入了Custom.config文件到我的項目(與構建行動 '複製總是'):

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

在控制檯應用程序中做同樣的事情時,這是有效的。有沒有一種方法可以在單元測試組件中完成這項工作?

回答

8

我找到了答案。使用mstest,我需要在'localtestrun.testrunco​​nfig'文件中將'Custom.config'文件標記爲部署項目。

+0

耶!我喜歡配置事物以達到我的配置! – 2013-06-25 13:41:58

相關問題