用于单元测试程序集的 app.config:如何制作 appsettings “文件”;属性工作?
我需要在单元测试中从 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>
在控制台应用程序中执行相同操作时,这是有效的。有没有办法让这个在单元测试组件中也能工作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了答案。使用 mstest,我需要将“Custom.config”文件标记为“localtestrun.testrunconfig”文件中的部署项。
I found the answer. Using mstest, I needed to mark the 'Custom.config' file as a deployment item in the 'localtestrun.testrunconfig' file.