在 .NET Core 控制台应用程序中使用带有 NUnit3 的 app.config 文件 [英] Using an app.config file with NUnit3 in a .NET Core console app

查看:29
本文介绍了在 .NET Core 控制台应用程序中使用带有 NUnit3 的 app.config 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:

目前我的解决方案中有三个项目:

  • 一个 .NET Standard 2.0 库,其中包含一些我想测试的代码.
  • 引用库以确保其正常工作的 .NET Core 2.2 控制台应用程序.
  • 使用 VS 中的NUnit 测试项目"模板创建的 .NET Core 2.2 控制台应用.

我的测试项目中的依赖全部来自NuGet:

  • Moq 版本="4.10.1"
  • nunit" 版本="3.11.0"
  • NUnit.ConsoleRunner" Version="3.10.0"
  • NUnit3TestAdapter" 版本="3.13.0"
  • Microsoft.NET.Test.Sdk" Version="16.0.1"
<小时>

问题:

.NET 标准库依赖于任何使用它的应用程序中存在的 app.config 文件.它使用 ConfigurationSectionConfigurationElement 属性将值映射到一个类,非常类似于这个答案:带有嵌套集合的自定义配置部分

.NET Core 控制台应用程序中有一个 app.config 文件,该库能够很好地解析其中的值并使用它们.耶.

另一方面,NUnit 控制台应用程序中包含相同的 app.config 文件,但库似乎无法看到它.一旦它尝试使用 ConfigurationManager.GetSection("...") 读取值,它就会返回 null.

有没有人得到一个 app.config 文件在这样的环境中与 NUnit3 一起工作?

<小时>

我的尝试:

似乎它支持配置文件,但我不确定这些文档是指一些特殊的 NUnit 配置文件还是 app.config 文件.

  • 我尝试将 app.config 重命名为 my_test_project_name.dll.config
  • 我将配置文件复制到输出目录"设置为始终复制"
  • 我尝试了所有我能想到的相似名称​​(app.config、App.config、my_test_project_name.config、my_test_project_name.dll.config 等)

我还在迄今为止编写的一个测试中尝试了一些东西,尝试以某种方式设置配置文件,例如建议使用 AppDomain.CurrentDomain.SetData() (没有用,可能是因为 NUnit3 不支持 AppDomain):

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:PathToMyTestsmy_test_project_name.dll.config");

尽管 NUnit 存储库中的测试似乎表明 可以在 NUnit3 中使用配置文件,该特定测试文件仅在 .NET 4.5 演示项目,而不是 .NET Core 演示项目.

解决方案

当您在单元测试中执行以下行并检查其结果时,您可能会注意到 NUnit 项目查找名为 testhost 的配置文件.dll.config.

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;

<块引用>

路径缩短:ClassLibrary1NUnitTestProject1inDebug etcoreapp2.2 esthost.dll.config

因此,我创建了一个示例,说明如何在 ASP.NET Core 2.2 和 NUnit 测试项目模板中使用配置文件.另外,确保配置文件的Copy to Output Directory设置为Copy always.

UnitTest.cs

公共类UnitTest{私有只读字符串 _configValue = ConfigurationManager.AppSettings["test"];[测试]公共无效测试(){Assert.AreEqual("testValue", _configValue);}}

testhost.dll.config

The Environment:

I've got three projects in my solution currently:

  • A .NET Standard 2.0 library with some code I'd like to test.
  • A .NET Core 2.2 console app that references the library to make sure it works.
  • A .NET Core 2.2 console app created by using the "NUnit Test Project" template in VS.

The dependencies in my test project all are all from NuGet:

  • Moq Version="4.10.1"
  • nunit" Version="3.11.0"
  • NUnit.ConsoleRunner" Version="3.10.0"
  • NUnit3TestAdapter" Version="3.13.0"
  • Microsoft.NET.Test.Sdk" Version="16.0.1"

The Problem:

The .NET standard library relies on an app.config file being present in any application that uses it. It uses ConfigurationSection and ConfigurationElement attributes to map the values to a class, very similar to this answer: A custom config section with nested collections

The .NET Core console app has an app.config file in it, and the library is able to parse values out of it just fine and use them. Yay.

The NUnit console app, on the other hand, has the same app.config file in it, but the library can't seem to see it. As soon as it tries to read a value using ConfigurationManager.GetSection("...") it returns null.

Has anyone gotten an app.config file to work with NUnit3 in an environment like this?


What I've Tried:

It seems like it supports config files, but I'm not sure if the docs are referring to some special NUnit config file or an app.config file.

  • I tried renaming app.config to my_test_project_name.dll.config
  • I set the config file "Copy to output directory" setting to "Copy always"
  • I tried every similar name I could think of (app.config, App.config, my_test_project_name.config, my_test_project_name.dll.config, etc)

I also tried a few things inside the one test I've written so far, to attempt to set the config file somehow, such as a suggestion to use AppDomain.CurrentDomain.SetData() (didn't work, possibly because NUnit3 doesn't support AppDomain):

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:PathToMyTestsmy_test_project_name.dll.config");

Although there are tests in the NUnit repo that seem to suggest using a configuration file in NUnit3 is possible, that particular test file is only referenced in the .NET 4.5 demo project, not the .NET Core demo project.

解决方案

When you execute the following line within a unit test and inspect its result, you may notice that the NUnit project looks for a configuration file called testhost.dll.config.

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;

Path shortened: ClassLibrary1NUnitTestProject1inDebug etcoreapp2.2 esthost.dll.config

Thereby, I have created an example of how to use a configuration file with ASP.NET Core 2.2 and the NUnit Test Project template. Also, make sure that the Copy to Output Directory setting for the configuration file is set to Copy always.

UnitTest.cs

public class UnitTest
{
    private readonly string _configValue = ConfigurationManager.AppSettings["test"];

    [Test]
    public void Test()
    {
        Assert.AreEqual("testValue", _configValue);
    }
}

testhost.dll.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="test" value="testValue" />
  </appSettings>
</configuration>

这篇关于在 .NET Core 控制台应用程序中使用带有 NUnit3 的 app.config 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆