Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime

xunit integration tests
core dot net
asp net core test
net core unit test dependency injection
integration tests c#
solution root could not be located using application root
testserver 404
xunit postasync

When creating test projects or upgrading an application and tests to ASP.NET Core 2.1 / .NET Core 2.1, running tests fails with assembly load exceptions like

System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

When adding references to some other libraries there are also build warnings like

warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.Options" that could not be resolved. warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.Configuration.Abstractions" that could not be resolved. warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Hosting.Abstractions" that could not be resolved. warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.DependencyInjection.Abstractions" that could not be resolved. warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Http.Abstractions" that could not be resolved. warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Http.Features" that could not be resolved.

How can I make test projects work for testing ASP.NET Core 2.1 applications?

Update: This has been made easier with 2.2 Tooling. Make sure that your dotnet --version SDK version is at least 2.2.100, even when buidling 2.1 applications

Just add a versionless package reference to your project while keeping the Microsoft.NET.Sdk:

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

      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>

      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <!-- other references to xunit, test SDK etc. -->
      </ItemGroup>

      <ItemGroup>
        <ProjectReference Include="..\AspNetCoreAppToTest\AspNetCoreAppToTest.csproj" />
      </ItemGroup>

    </Project>

Original:

ASP.NET Core 2.1 uses a new "shared framework" to run ASP.NET Core applications on. Test projects need to be modified/updated to also use this shared framework using one of the following approaches:

  1. Change the test project's <Project> tag in the first line to use the web SDK (Microsoft.NET.Sdk.Web instead of Microsoft.NET.Sdk) and add a package reference to Microsoft.AspNetCore.App (or .All if you are using that inside the web project) without specifying a version

    The project file (.csproj) of the test project should now look like this:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <!-- other references to xunit, test SDK etc. -->
      </ItemGroup>
    
      <ItemGroup>
        <ProjectReference Include="..\AspNetCoreAppToTest\AspNetCoreAppToTest.csproj" />
      </ItemGroup>
    
    </Project>
    
  2. Alternative: Leave the Sdk as-is and add a PackageReference to the shared framework package but specify a version.

    This can be done by simply adding a NuGet reference to Microsoft.AspNetCore.App. However, this may cause issues since the SDK may choose to update the reference when a new patch release of the ASP.NET Core is released and the tooling is updated to reflect this. You will need update the NuGet reference for every patch release.

dotnet xunit fails for integration tests on ASP.NET Core 2.1-rc1 , Add reference from test project to api project Add simple integration test var server FileNotFoundException : Could not load file or assembly 'Microsoft. -​and-unit-tests-no-longer-work-on-asp-net-core-2-1-failing-to-find-as Somewhere xunit is reading a runtimeconfig.json or an assets file and has  Integration tests ensure that an app's components function correctly at a level that includes the app's supporting infrastructure, such as the database, file system, and network. ASP.NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server.

The question specifies ASP.NET Core 2.1, but for those running into the same problem with ASP.NET Core 2.2 it's enough to add the following to your test project file:

<PackageReference Include="Microsoft.AspNetCore.App" />

There is no need to set Sdk="Microsoft.NET.Sdk.Web" anymore and you should not add a version to the package reference above - that causes a warning.

Integration testing is hard to set up and may break on shared , TL;DR: Unit tests should have an option to run on the asp.net core shared known version of the web SDK and applying NuGet and assembly resolution accordingly. Mvc.Testing -v 2.1.0-rc1-final; add C# code -and-unit-tests-no-​longer-work-on-asp-net-core-2-1-failing-to-find-as/50401153#50401153  Repro: Download .NET Core 2.1 RC1 SDK. Create solution with two projects: dotnet new webapi, dotnet new xunit. Add reference from test project to api project Add simple integration test var server

I was having the same problem. Add this to Test Project solved the problem

 <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.1"/>

Test Explorer won't run tests: too particular about .NET Core runtime , Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime - testing. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. testing asp.net-core nunit integration

ASP.NET Core breaking changes, Testhost process exited with error: It was not possible to find any compatible framework version The framework 'Microsoft.NETCore.App', version '2.1.0' was not  Integration tests are slower and more involved than unit tests, so it's common for a project to have lots of small unit tests but only a handful of integration tests. In order to test the whole stack (including controller routing), integration tests typically make HTTP calls to your application just like a web browser would.

Breaking changes, version 2.2 to 3.1, packages that provide integration between ASP.NET NET Core 2.1 or later projects, replace the old Microsoft. For more information, see Support older browsers. Chrome 75 and earlier are reported to fail with the new None setting. No compatibility issues were observed when testing with Microsoft  This shows how ASP.NET Core is nicely factored for Unit Testing but it doesn't do a "real" HTTP GET or perform true end-to-end testing. These tests are testing if visiting URLs like /620 will automatically redirect to the correct full canonical path as they should.

Problems with .NET unit testing – JetBrains Rider Support, ASP.NET Core 3.1 has been updated to implement the new Chrome 75 and earlier are reported to fail with the new None No compatibility issues were observed when testing with Add the following JSON to a runtimeconfig.​template.json NET Core 2.1, 2.2, and 3.0; Microsoft. For test projects in 2.1 tools(!) (CLI 2.1.*), it is a bit different and tricky, see Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime

Comments
  • It seems fixed in 2.2 SDK github.com/dotnet/sdk/pull/2533 Test projects can have a versionless asp.net core reference that will match the referenced project
  • I was following your advice regarding the first alternative, and got the following compiler error on build. What am I doing wrong? Error CS5001 Program does not contain a static 'Main' method suitable for an entry point
  • That seems like a different problem because the test sdk turns the project into an executable but also emits a main method which somehow doesn't work with your project
  • Thanks for the clarification! Well, but this seems to happen also if you start over from scratch: I've made a new asp.net core MVC web app, chose 2.1 and MVC. Then I added a new .NET core class library, replaced the project file with the above snippet and got the compile error. The result is here: github.com/HermannGruber/AspNetCoreAppToTest
  • Update: I've noticed that the compiler error disappears if we change the project output type to "class library". Or equivalently, if we add <OutputType>Library</OutputType> to the first property group in the code snippet in your answer.
  • Using VS 2019, I had to also close and re-open VS + rebuild to remove the errors.