2

I have a C# test project compiling under .NET6, using System.Configuration.ConfigurationManager 7.0.0 (so I can use configuration files instead of JSON files, for legacy reasons).

The code looks like this:

[Test]
public void TestConfiguration()
{
   var filePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
   Console.Error.WriteLine($"Configuration filepath is {filePath}");

   var testValue = ConfigurationManager.AppSettings["TestValue"];
   Assert.IsTrue(testValue == "testing");
}

My .nUnit file (mytest.nunit) looks like this:

<NUnitProject>
  <Config name="Debug" binpathtype="Auto" runtimeFramework="6.0" configfile="./mytest.config">
    <assembly path="PrimeService.Tests\bin\Debug\net6.0\PrimeService.Tests.dll" />
  </Config>
</NUnitProject>

My .config file (mytest.config) looks like this:

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

Directory structure looks like this:

D:\src2\csharp\coretest\
  - mytest.nunit
  - mytest.config
D:\src2\csharp\coretest\nUnit
D:\src2\csharp\coretest\PrimeService.Tests

And finally, my command line and results look like this:

D:\src2\csharp\coretest>nunit\console\nunit3-console.exe mytest.nunit --configfile=mytest.config
NUnit Console 3.16.1 (Release)
Copyright (c) 2022 Charlie Poole, Rob Prouse
Wednesday, January 18, 2023 5:04:08 PM

Runtime Environment
   OS Version: Microsoft Windows NT 6.2.9200.0
   Runtime: .NET Framework CLR v4.0.30319.42000

Test Files
    mytest.nunit

Configuration filepath is D:\src2\csharp\coretest\nunit\console\agents\net6.0\nunit-agent.dll.config

[ ... rest of output removed for brevity ...]

Note that the actual configuration file being used is not what is specified.

This may be related to https://github.com/nunit/nunit-project-loader/issues/44 but that was years ago and I'm running the latest release.

Note: I used --trace=Verbose, but nothing in the log files mentioned how it's obtaining the config filename.

Does someone know something I don't to get this to recognize my configuration file? The same configuration using the same body of code for .NET Framework 4.8 works as expected.

Ketchup201
  • 129
  • 1
  • 9
  • Hello, did you were able to fix that problem ? I have exactly the same case with .Net 7.0 projects using old XML ConfigurationManager for safety reasons. It works well in Visual Studio R# but not with the NUnit Console runner. Thank you – AFract Jul 17 '23 at 16:53
  • No, unfortunately not. Still an issue :-( – Ketchup201 Jul 18 '23 at 21:22
  • 1
    Ok, Since yesterday I dug on it, so I shared my own research on the topic, and what I finally did to definitely fix the problem. – AFract Jul 19 '23 at 13:57

1 Answers1

1

I had the exact same problem, I spend a lot on time on it so I would like to share what didn't work and how I finally fixed the problem (spoiler : it's not exactly a fix, but I hope it will be helpful).

I was also in need to make it work for TeamCity continuous integration with NUnit runner. Before to migrate to recent .Net frameworks, it was ok, but after it was broken, the configuration file is never loaded.

Under Visual Studio, tests are ok. If I run them with Resharper Test Runner, it's also OK, BUT it requires a configuration file with the proper name. This can be generated with :     

 <Copy SourceFiles="app.config" DestinationFiles="$(OutDir)\ReSharperTestRunner.dll.config" Condition="Exists('app.config')" />

The same trick applies for other file name (so it can also be used for NUnit)

Apparently, the configuration file used could be nunit-agent.dll.config. However, it's already existing in NUnit agent folder, so even if you put a such configuration file in your project folder, it does nothing because NUnit picks its own file.

The "configfile" node in NUnit configuration file seems to do nothing. I have also played with other parameters of NUnit project files, but nothing works. Here's the documentation : https://docs.nunit.org/articles/nunit/technical-notes/usage/NUnit-Project-XML-Format.html

So after a few hours of unsuccesful tries, my solution was to completely drop the NUnit runner for recent .Net projects with old XML configuration files managed with ConfigurationManager.

Instead, I just used "dotnet.exe test" command, which looks like this :

"C:\Program Files\dotnet\dotnet.exe" test T:\Your.sln --configuration Release --no-build @T:\output.rsp

My tests are running fine now. If it's an option for you to don't use NUnit runner anymore, I suggest you to consider it.

AFract
  • 8,868
  • 6
  • 48
  • 70
  • Unfortunately, I need the nUnit outout to feed other pipelines, but this is excellent, thank you for sharing it! – Ketchup201 Jul 24 '23 at 23:41