用于单元测试程序集的 app.config:如何制作 appsettings “文件”;属性工作?

发布于 2024-08-04 07:59:50 字数 1009 浏览 1 评论 0原文

我需要在单元测试中从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别再吹冷风 2024-08-11 07:59:50

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

I found the answer. Using mstest, I needed to mark the 'Custom.config' file as a deployment item in the 'localtestrun.testrunconfig' file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文