0

I'm trying to execute my SpecFlow tests through nunit3-console runner but I'm receiving the following error:

System.IO.FileNotFoundException : The configuration file 'appsettings.json' was not found and is not optional. The expected physical path was 'C:\Users\...\.nuget\packages\nunit.consolerunner\3.15.0\tools\agents\net6.0\appsettings.json'.

Obviously the runner tries to find the configuration file in the nunit3-console package directory instead of the active/current directory where the file has been copied. Is there any way to specify the correct file directory?

The command I used is the following (or see the image below):

[nunit3-console runner package path] [test .dll path] --trace=off

Maybe it's visible but the nunit3-console runner package I'm using is version 3.15.0 and the version of the framework is .net 6.

Here is the detailed error:

Stack Trace

eclipse06
  • 11
  • 2
  • Have you verified the appsettings.json file is copied to the build output directory? Right-click on appsettings.json in Visual Studio, then choose "Properties". The "Copy To Output" property should be set to "Always". If not, change the value and rebuild. I bet it works. – Greg Burghardt Mar 28 '22 at 11:40
  • 1
    Yes, I did. This was the first thing I checked but the property was set to "Always" and I can see the file in the directory. I probably should have mentioned that there is no problem when I'm running the tests through VS. – eclipse06 Mar 28 '22 at 12:37
  • Can you [edit] your question to include the code that loads AppSettings.json? Or is this something intrinsic to NUnit? – Greg Burghardt Mar 28 '22 at 13:17
  • You correctly assumed where the problem may be and it turned out that `AppDomain.CurrentDomain.BaseDirectory` had to be replaced with `Directory.GetCurrentDirectory()` into the config setup in order to work. – eclipse06 Mar 28 '22 at 14:10

1 Answers1

1

Update:

The problem turned out to be the way the configuration was built.

Before:

Configuration = new ConfigurationBuilder()
    .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .Build();

After:

Configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .Build();

And the difference between those two looks like that:

AppDomain.CurrentDomain.BaseDirectory

"C:\\Users\\...\\source\\repos\\qa-automation\\...\\bin\\Debug\\net6.0\\"

Directory.GetCurrentDirectory()

"C:\\Users\\...\\source\\repos\\qa-automation\\...\\bin\\Debug\\net6.0"
eclipse06
  • 11
  • 2
  • HI @eclipse06, welcome. Nice that you found it. [Here are some more ways to get application paths.](https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) – Rubenisme Mar 28 '22 at 14:38