Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nunit3-console doesn't see nugets from Microsoft.AspNetCore.App #1392

Open
Harmyder opened this issue Mar 6, 2024 · 2 comments
Open

nunit3-console doesn't see nugets from Microsoft.AspNetCore.App #1392

Harmyder opened this issue Mar 6, 2024 · 2 comments

Comments

@Harmyder
Copy link

Harmyder commented Mar 6, 2024

I have the following project and test

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
    <PackageReference Include="NUnit" Version="3.13.3" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
  </ItemGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>
  
</Project>
[Test]
public void Test1()
{
    Assembly.Load("Microsoft.AspNetCore, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60");
}

I can run fine this project from Visual Studio but then I run it from command line with nunit3-console nunittests.dll I see the following error

System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
at System.Reflection.Assembly.Load(String assemblyString)
at NUnitTests.Tests.Test1() in D:\Yola\Test22\NUnitTests\UnitTest1.cs:line 16
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

If I use Assembly.LoadFrom with the full path it is able to load the DLL.

@OsirisTerje
Copy link
Member

OsirisTerje commented Mar 6, 2024

Hi!

We do have issues with this kind of loading behavior, and we do have multiple attempts at finding the reason.
I made small repro solution, I got a different error from what you have, but I believe it might be about the same bug. The repro is here: https://github.com/nunit/nunit.issues/tree/main/Issue4654 . Feel free to change it.

I do get this error:
image

We do have multiple similar errors in the console repo, see https://github.com/nunit/nunit-console/issues

I'll move this issue over to that repo.

You can have a look at #1383 which is a similar reported issue.

See this comment:

The reason is that the nunit-agent only references the Microsoft.NETCore.App runtime in its runtimeconfig.json. Therefore a workaround is to manually add the missing runtimes (Microsoft.WindowsDesktop.App and/or Microsoft.AspNetCore.App) to the runtimeconfig.json.

If you use the latest test.sdk, you should also get the runtimeconfig.json correct, and then you should get the same error as I have above. See the repro for details.

The failure with loading Microsoft.Extensions.Logging.Abstractions is a bit weird. It is a framework reference, and you see it in the dependencies list in Visual Studio, but the console runner doesn't find it.

We have to dig a bit deeper to see what this can be.

Is there a reason you can't use dotnet test instead of the console?

@bthharper
Copy link

Using "nunit-console3" to run unit tests for .NET 8.0 assemblies that require the "Microsoft.AspNetCore.App" framework fails with the following error:

Results (nunit3) saved as TestResult.xml
PS C:\git\common> C:\Platform\nunit.console\net8.0\nunit3-console.exe C:/git/common/Source/Framework.Test/bin/x64/Debug/Framework.Test.dll
NUnit Console 3.17.0+685c5b542b5e9ba632c905f0bd514a773d9758af (Release)
Copyright (c) 2022 Charlie Poole, Rob Prouse
17 May 2024 23:56:17

Runtime Environment
   OS Version: Microsoft Windows 10.0.22631
  Runtime: .NET 8.0.5

Test Files
    C:/git/common/Source/Framework.Test/bin/x64/Debug/Framework.Test.dll


Errors, Failures and Warnings

1) Invalid : C:/git/common/Source/Framework.Test/bin/x64/Debug/Framework.Test.dll
Unable to load one or more of the requested types.
Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
  ----> Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.Microsoft.AspNetCore.App

The workaround was to change the nunit3-comsole.runtimeconfig.json in the net8.0 folder to:

{
  "runtimeOptions": {
    "tfm": "net8.0",
    "frameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "8.0.0"
      },
      {
        "name": "Microsoft.WindowsDesktop.App",
        "version": "8.0.0"
      },
      {
        "name": "Microsoft.AspNetCore.App",
        "version": "8.0.0"
      }
    ],
    "configProperties": {
      "System.Reflection.NullabilityInfoContext.IsSupported": true,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
    }
  }
}

I would rather not have to modify the distribution of nunit3-console to make this work if possible, is there another solution I have missed for this?

As an aside, I am unable to use dotnet test as this does not work when the solution contains C++/CLI vcxproj - which is something Microsoft seem reluctant to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants