Reputation: 66449
The Environment:
I've got three projects in my solution currently:
The dependencies in my test project all are all from NuGet:
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 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:\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.
Upvotes: 25
Views: 7982
Reputation: 303
Rider version > 2020.2 and up looks for ReSharperTestRunner64.dll.config
. So to extend Nathan Smith's answer, have an App.config
and copy it:
<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>
Upvotes: 2
Reputation: 2975
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.
Setting Resharper>Options>Unit Testing>General>Log entries severity to Trace doesn't give much useful info. 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.
The app.config file content is automatically added to yourTestProjectName.dll.config in the output directory if:
the file is named app.config
or there is an <appConfig> tag in the csproj
or there is a link to a config file e.g.:
<ItemGroup> <None Include="..\someOtherProject\app.config" Link="myTestApp.config"> <CopyToOutputDirectory>Never</CopyToOutputDirectory> </None> </ItemGroup>
There is no need to set the output of config files to copy if newer in properties or csproj.
If there are issues with the content of the config.file, the test ends up being inconclusive with this error:
ReSharper Ultimate – System.NullReferenceException: Object reference not set to an instance of an object.
at NUnit.Engine.Internal.ServerBase.Start()
Upvotes: 1
Reputation: 1813
For one project testhost.dll.config
is working well.
For another project I had to use testhost.x86.dll.config
The solution from (prd) was very helpfull for verifying the real path being used
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
https://github.com/dotnet/corefx/issues/22101
Copy app.config with correct name
<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>
Upvotes: 11
Reputation: 2311
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:
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. 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>
Upvotes: 27