在.NET Core控制台应用程序中将app.config文件与NUnit3一起使用

Gra*_*ICA 14 c# nunit app-config nunit-3.0 .net-core

环境:

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

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

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

  • Moq版本=“ 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“

问题:

.NET标准库依赖于任何使用它的应用程序中存在的app.config文件。它使用ConfigurationSection和ConfigurationElement属性将值映射到类,与以下答案非常相似:具有嵌套集合的自定义配置节

.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:\Path\To\My\Tests\my_test_project_name.dll.config");
Run Code Online (Sandbox Code Playgroud)

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

prd*_*prd 12

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

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
Run Code Online (Sandbox Code Playgroud)

路径缩短:ClassLibrary1\NUnitTestProject1\bin\Debug\netcoreapp2.2\testhost.dll.config

因此,我创建了一个示例,说明如何在ASP.NET Core 2.2和NUnit Test Project模板中使用配置文件。另外,请确保将配置文件的“ 复制到输出目录”设置设置为Copy always。

UnitTest.cs

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

    [Test]
    public void Test()
    {
        Assert.AreEqual("testValue", _configValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

testhost.dll.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="test" value="testValue" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)


Nat*_*ith 6

因为一个项目testhost.dll.config运作良好。
对于我不得不使用的另一个项目testhost.x86.dll.config

(prd) 的解决方案对于验证正在使用的真实路径非常有帮助

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
Run Code Online (Sandbox Code Playgroud)

https://github.com/dotnet/corefx/issues/22101

使用正确的名称复制 app.config

<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
    <!-- Command Line (dotnet test) -->
    <Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
    <!-- Visual Studio Test Explorer -->
    <Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\testhost.x86.dll.config" />
</Target>
Run Code Online (Sandbox Code Playgroud)

这是另一个有趣的解决方案

<None Update="App.config">
  <Link>testhost.x86.dll.config</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Run Code Online (Sandbox Code Playgroud)