在 .NET Core 控制台应用程序中使用带有 NUnit3 的 app.config 文件
Using an app.config file with NUnit3 in a .NET Core console app
环境:
我目前的解决方案中有三个项目:
- 一个 .NET Standard 2.0 库,其中包含一些我想测试的代码。
- 引用该库以确保其正常工作的 .NET Core 2.2 控制台应用程序。
- 使用 VS 中的 "NUnit Test Project" 模板创建的 .NET Core 2.2 控制台应用程序。
我的测试项目中的依赖全部来自NuGet:
- 起订量版本="4.10.1"
- nunit" 版本="3.11.0"
- NUnit.ConsoleRunner" 版本="3.10.0"
- NUnit3TestAdapter" Version="3.13.0"
- Microsoft.NET.Test.Sdk" 版本="16.0.1"
问题:
.NET 标准库依赖于 app.config 文件存在于任何使用它的应用程序中。它使用 ConfigurationSection and ConfigurationElement attributes to map the values to a class, very similar to this answer: A custom config section with nested collections
.NET Core 控制台应用程序中有一个 app.config 文件,库能够很好地解析其中的值并使用它们。耶。
另一方面,NUnit 控制台应用程序中有相同的 app.config 文件,但库似乎看不到它。一旦它尝试使用 ConfigurationManager.GetSection("...")
读取值,它就会 returns null
.
有没有人得到一个 app.config 文件来在这样的环境中使用 NUnit3?
我尝试过的:
它 似乎 像 it supports config files,但我不确定文档是指一些特殊的 NUnit 配置文件还是 app.config 文件。
- 我尝试将 app.config 重命名为 my_test_project_name.dll.config
- 我将配置文件 "Copy to output directory" 设置为 "Copy always"
- 我尝试了所有我能想到的相似名字 (app.config, App.config, my_test_project_name.config, my_test_project_name.dll.config, etc)
我还在到目前为止编写的一个测试中尝试了一些东西,试图以某种方式设置配置文件,例如使用 AppDomain.CurrentDomain.SetData()
的建议(没有用,可能是因为NUnit3 不支持 AppDomain):
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Path\To\My\Tests\my_test_project_name.dll.config");
尽管 NUnit 存储库中的测试似乎表明 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。
当您在单元测试中执行以下行并检查其结果时,您可能会注意到 NUnit 项目查找名为 testhost.dll.config
.
的配置文件
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
Path shortened: ClassLibrary1\NUnitTestProject1\bin\Debug\netcoreapp2.2\testhost.dll.config
因此,我创建了一个示例,说明如何将配置文件与 ASP.NET Core 2.2 和 NUnit 测试项目 模板一起使用。此外,确保配置文件的 复制到输出目录 设置设置为 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>
一个项目testhost.dll.config
运行良好。
对于另一个项目,我不得不使用 testhost.x86.dll.config
(prd) 的解决方案对于验证正在使用的真实路径非常有帮助
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
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>
这是另一个有趣的解决方案
<None Update="App.config">
<Link>testhost.x86.dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
我打这个 post 是因为 运行 在 Net.Core 测试项目中从 Resharper 进行 NUnit 测试时出现问题 app.config。
将 Resharper>选项>单元测试>常规>日志条目严重性设置为 Trace 不会提供太多有用的信息。
我安装了 Microsoft.NET.Test.Sdk 和 NUnit3TestAdapter 到 运行 来自 VS 测试资源管理器的测试,它告诉我配置文件中的错误是什么。
app.config文件内容自动添加到输出目录yourTestProjectName.dll.configif:
文件名为app.config
或者csproj中有标签
或者配置文件有 link,例如:
<项目组>
从不
如果属性或 csproj 中较新,则无需设置要复制的配置文件的输出。
如果 config.file 的内容有问题,测试最终会出现以下错误:
ReSharper Ultimate – System.NullReferenceException: Object reference not set to an instance of an object.
at NUnit.Engine.Internal.ServerBase.Start()
Rider 版本 > 2020.2 及更高版本查找 ReSharperTestRunner64.dll.config
。因此,要扩展 Nathan Smith 的答案,请使用 App.config
并复制它:
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<!-- Command Line (dotnet test) -->
<Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
<!-- Rider -->
<Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\ReSharperTestRunner64.dll.config"/>
</Target>
环境:
我目前的解决方案中有三个项目:
- 一个 .NET Standard 2.0 库,其中包含一些我想测试的代码。
- 引用该库以确保其正常工作的 .NET Core 2.2 控制台应用程序。
- 使用 VS 中的 "NUnit Test Project" 模板创建的 .NET Core 2.2 控制台应用程序。
我的测试项目中的依赖全部来自NuGet:
- 起订量版本="4.10.1"
- nunit" 版本="3.11.0"
- NUnit.ConsoleRunner" 版本="3.10.0"
- NUnit3TestAdapter" Version="3.13.0"
- Microsoft.NET.Test.Sdk" 版本="16.0.1"
问题:
.NET 标准库依赖于 app.config 文件存在于任何使用它的应用程序中。它使用 ConfigurationSection and ConfigurationElement attributes to map the values to a class, very similar to this answer: A custom config section with nested collections
.NET Core 控制台应用程序中有一个 app.config 文件,库能够很好地解析其中的值并使用它们。耶。
另一方面,NUnit 控制台应用程序中有相同的 app.config 文件,但库似乎看不到它。一旦它尝试使用 ConfigurationManager.GetSection("...")
读取值,它就会 returns null
.
有没有人得到一个 app.config 文件来在这样的环境中使用 NUnit3?
我尝试过的:
它 似乎 像 it supports config files,但我不确定文档是指一些特殊的 NUnit 配置文件还是 app.config 文件。
- 我尝试将 app.config 重命名为 my_test_project_name.dll.config
- 我将配置文件 "Copy to output directory" 设置为 "Copy always"
- 我尝试了所有我能想到的相似名字 (app.config, App.config, my_test_project_name.config, my_test_project_name.dll.config, etc)
我还在到目前为止编写的一个测试中尝试了一些东西,试图以某种方式设置配置文件,例如使用 AppDomain.CurrentDomain.SetData()
的建议(没有用,可能是因为NUnit3 不支持 AppDomain):
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Path\To\My\Tests\my_test_project_name.dll.config");
尽管 NUnit 存储库中的测试似乎表明 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。
当您在单元测试中执行以下行并检查其结果时,您可能会注意到 NUnit 项目查找名为 testhost.dll.config
.
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
Path shortened:
ClassLibrary1\NUnitTestProject1\bin\Debug\netcoreapp2.2\testhost.dll.config
因此,我创建了一个示例,说明如何将配置文件与 ASP.NET Core 2.2 和 NUnit 测试项目 模板一起使用。此外,确保配置文件的 复制到输出目录 设置设置为 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>
一个项目testhost.dll.config
运行良好。
对于另一个项目,我不得不使用 testhost.x86.dll.config
(prd) 的解决方案对于验证正在使用的真实路径非常有帮助
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
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>
这是另一个有趣的解决方案
<None Update="App.config">
<Link>testhost.x86.dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
我打这个 post 是因为 运行 在 Net.Core 测试项目中从 Resharper 进行 NUnit 测试时出现问题 app.config。
将 Resharper>选项>单元测试>常规>日志条目严重性设置为 Trace 不会提供太多有用的信息。 我安装了 Microsoft.NET.Test.Sdk 和 NUnit3TestAdapter 到 运行 来自 VS 测试资源管理器的测试,它告诉我配置文件中的错误是什么。
app.config文件内容自动添加到输出目录yourTestProjectName.dll.configif:
文件名为app.config
或者csproj中有
标签 或者配置文件有 link,例如:
<项目组>
从不
如果属性或 csproj 中较新,则无需设置要复制的配置文件的输出。
如果 config.file 的内容有问题,测试最终会出现以下错误:
ReSharper Ultimate – System.NullReferenceException: Object reference not set to an instance of an object.
at NUnit.Engine.Internal.ServerBase.Start()
Rider 版本 > 2020.2 及更高版本查找 ReSharperTestRunner64.dll.config
。因此,要扩展 Nathan Smith 的答案,请使用 App.config
并复制它:
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<!-- Command Line (dotnet test) -->
<Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
<!-- Rider -->
<Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\ReSharperTestRunner64.dll.config"/>
</Target>