nunit-console icon indicating copy to clipboard operation
nunit-console copied to clipboard

AppDomain.CurrentDomain.BaseDirectory is directory nunit-agent.exe

Open MDcod opened this issue 2 years ago • 7 comments

Hello! I think you are aware of this behavior. But just in case, one more time.

When running a test from nunit3-console. The value of AppDomain.CurrentDomain.BaseDirectory in the test will be the path to the directory from where the console runner is essentially launched.

I have seen many people mention this feature in issues. But I didn't see any workaround for this. I can't use TestContext because the call to AppDomain.CurrentDomain.BaseDirectory is used in a third-party library.

About csproj:

  • net6.0
  • nuget Nunit v3.13.3
  • nuget NUnit3TestAdapter v4.3.1

Command C:\NUnit.Console-3.16.2\bin\nunit3-console.exe C:\src\solution\Ttest.Project\bin\Test.Project.dll --where id=1-1002 Value of AppDomain.CurrentDomain.BaseDirectory C:\NUnit.Console-3.16.2\bin\agents\net6.0\

Questions:

  • Please let me know if there is a workaround.
  • Are there any plans to fix this behavior?

MDcod avatar Feb 05 '23 12:02 MDcod

There are not workarounds?

MDcod avatar Feb 10 '23 12:02 MDcod

Can you explain a bit about what you are trying to accomplish by changing the base directory and what you've tried so far? We'll need a bit more info to better understand and help.

As a general piece of advice, there are some work arounds and code samples listed in here which may help for your use case: https://github.com/nunit/nunit/issues/1072

And there's some command line options which may help depending on what you're trying to do (for example, --work or --out) : https://docs.nunit.org/articles/nunit/running-tests/Console-Command-Line.html

stevenaw avatar Feb 10 '23 16:02 stevenaw

@stevenaw NUnit traditionally ran tests in a separate AppDomain, which it created. The .NET Framework agents still do that. You can find the key code in the DomainManager class. That class is only built for .NET Framework. The .NET Core agents all run tests in the primary AppDomain.

Historically, this is due to the fact that our first runners for .NET Core were dealing with versions 1.0 and 1.1, where almost none of the features of AppDomain were available. Research is needed to figure out a way to set a separate base directory for the tests run by a .NET Core agent and resolve this. It may be through AppDomain or possibly in some other way.

I'm going to create an identical issue to this one in the TestCentric Gui because the same problem exists there. I'll post or link here if I figure something out. If you take this on, feel free to consult me offline.

CharliePoole avatar Feb 10 '23 18:02 CharliePoole

I'm wondering why a .NET 6.0 test assembly, is expecting there to be more than one AppDomain in a process... i.e. the primary AppDomain. Perhaps this is not a bug at all.

Is this an x/y problem? Why do you need the ApplicationBase to match the current test assembly?

CharliePoole avatar Feb 11 '23 23:02 CharliePoole

We have switched to dotnet test where the behaviour is still correct.

ueli-werner avatar Sep 05 '23 13:09 ueli-werner

I'm pretty much convinced that this is not a bug.

  1. AppDomains are somewhat minimally supported in .NET Core. In particular, they cannot be created.
  2. As a result, all code runs in the primary AppDomain of the process hosting it.
  3. Consequently, no .NET Core test assembly being executed by a test runner should expect the ApplicationBase to be anything other than that of the primary AppDomain.
  4. In addition, any assembly referenced by the test assembly will also be a .NET Core or Standard assembly, so the same limitations apply to it.

@OsirisTerje I'm inclined to close this. Do you want any others on the team to review it first?

CharliePoole avatar Jun 05 '24 04:06 CharliePoole

Personally, I think it is fine to close this issue

mikkelbu avatar Jun 05 '24 13:06 mikkelbu