#StackBounty: #c# #.net-core #nunit #app-config #nunit-3.0 Using an app.config file with NUnit3 in a .NET Core console app

Bounty: 300

The Environment:

I’ve got three projects in my solution currently:

  • A .NET Standard 2.0 library with some code I’d like to test.
  • A .NET Core 2.2 console app that references the library to make sure it works.
  • A .NET Core 2.2 console app created by using the “NUnit Test Project” template in VS.

The dependencies in my test project all are all from NuGet:

  • Moq Version=”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″

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 tried renaming app.config to my_test_project_name.dll.config
  • I set the config file “Copy to output directory” setting to “Copy always”
  • I tried every similar name I could think of (app.config, App.config, my_test_project_name.config, my_test_project_name.dll.config, etc)

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:PathToMyTestsmy_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.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.