[英]Using an app.config file with NUnit3 in a .NET Core console app
The Environment:环境:
I've got three projects in my solution currently:目前我的解决方案中有三个项目:
The dependencies in my test project all are all from NuGet:我的测试项目中的依赖全部来自NuGet:
The Problem:问题:
The .NET standard library relies on an app.config file being present in any application that uses it. .NET 标准库依赖于任何使用它的应用程序中存在的 app.config 文件。 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它使用ConfigurationSection和ConfigurationElement属性将值映射到一个类,非常类似于这个答案: 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. .NET Core 控制台应用程序中有一个 app.config 文件,该库能够很好地解析其中的值并使用它们。 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.另一方面,NUnit 控制台应用程序中包含相同的 app.config 文件,但库似乎无法看到它。 As soon as it tries to read a value using ConfigurationManager.GetSection("...")
it returns null
.一旦它尝试使用ConfigurationManager.GetSection("...")
读取值,它就会返回null
。
Has anyone gotten an app.config file to work with NUnit3 in an environment like this?有没有人得到一个 app.config 文件在这样的环境中与 NUnit3 一起工作?
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.它似乎支持 config files ,但我不确定文档是指一些特殊的 NUnit 配置文件还是 app.config 文件。
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()
的建议(不起作用,可能是因为 NUnit3 不t 支持 AppDomain):
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Path\To\My\Tests\my_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 .尽管 NUnit 存储库中的一些测试似乎表明可以在 NUnit3 中使用配置文件,但该特定测试文件仅在.NET 4.5 演示项目中引用,而不是在.NET Core 演示项目中引用。
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
.当您在单元测试中执行以下行并检查其结果时,您可能会注意到 NUnit 项目查找名为testhost.dll.config
的配置文件。
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
Path shortened :
ClassLibrary1\\NUnitTestProject1\\bin\\Debug\\netcoreapp2.2\\testhost.dll.config
路径缩短:ClassLibrary1\\NUnitTestProject1\\bin\\Debug\\netcoreapp2.2\\testhost.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.因此,我创建了一个示例,说明如何将配置文件与 ASP.NET Core 2.2 和NUnit 测试项目模板一起使用。 Also, make sure that the Copy to Output Directory setting for the configuration file is set to Copy always
.此外,请确保将配置文件的复制到输出目录设置设置为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测试主机.dll.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test" value="testValue" />
</appSettings>
</configuration>
For one project testhost.dll.config
is working well.对于一个项目testhost.dll.config
运行良好。
For another project I had to use testhost.x86.dll.config
对于另一个项目,我不得不使用testhost.x86.dll.config
The solution from (prd) was very helpfull for verifying the real path being used (prd) 的解决方案对于验证正在使用的真实路径非常有帮助
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
https://github.com/dotnet/corefx/issues/22101 https://github.com/dotnet/corefx/issues/22101
Copy app.config with correct name使用正确的名称复制 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>
This was another interesting solution这是另一个有趣的解决方案
<None Update="App.config">
<Link>testhost.x86.dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Rider version > 2020.2 and up looks for ReSharperTestRunner64.dll.config
. Rider 版本 > 2020.2 及更高版本查找ReSharperTestRunner64.dll.config
。 So to extend Nathan Smith's answer, have an App.config
and copy it:所以要扩展内森史密斯的答案,有一个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>
I hit this post because of an issue running NUnit tests from Resharper in a Net.Core test project due to a faulty app.config.由于 app.config 错误,在 Net.Core 测试项目中从 Resharper 运行 NUnit 测试时出现问题,我点击了这篇文章。
Setting Resharper>Options>Unit Testing>General>Log entries severity to Trace doesn't give much useful info.将 Resharper>Options>Unit Testing>General>Log entryseverity 设置为 Trace 并没有提供太多有用的信息。 I installed Microsoft.NET.Test.Sdk and NUnit3TestAdapter to run the tests from VS Test Explorer and it told me exactly what was the error in the config file.我安装了 Microsoft.NET.Test.Sdk 和 NUnit3TestAdapter 来运行来自 VS 测试资源管理器的测试,它准确地告诉我配置文件中的错误是什么。
The app.config file content is automatically added to yourTestProjectName.dll.config in the output directory if : app.config文件的内容会自动添加到yourTestProjectName.dll.config输出目录,如果:
the file is named app.config该文件名为 app.config
or there is an <appConfig> tag in the csproj或者 csproj 中有一个 <appConfig> 标签
or there is a link to a config file eg:或者有一个指向配置文件的链接,例如:
<ItemGroup> <None Include="..\\someOtherProject\\app.config" Link="myTestApp.config"> <CopyToOutputDirectory>Never</CopyToOutputDirectory> </None> </ItemGroup> <ItemGroup> <None Include="..\\someOtherProject\\app.config" Link="myTestApp.config"> <CopyToOutputDirectory>从不</CopyToOutputDirectory> </None> </ItemGroup>
There is no need to set the output of config files to copy if newer in properties or csproj.如果在属性或 csproj 中更新,则无需将配置文件的输出设置为复制。
If there are issues with the content of the config.file, the test ends up being inconclusive with this error:如果 config.file 的内容存在问题,则测试最终会出现以下错误:
ReSharper Ultimate – System.NullReferenceException: Object reference not set to an instance of an object.
at NUnit.Engine.Internal.ServerBase.Start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.