Can't get xUnit tests to run with .NET Core
Asked Answered
B

2

19

I'm porting a small library that I have on NuGet to .NET Core.

I've created .NET Standard 1.6 class libraries for the main project and the tests, and copied over the code. I changed the unit tests to use xUnit attributes and asserts rather than NUnit.

Other than that, I pretty much followed the instructions in the documentation, and thus I added the following NuGet packages:

  • Microsoft.NET.Test.Sdk
  • xunit
  • xunit.runner.visualstudio

Alas, (1) Test Explorer does not find my unit tests, and (2) when I run dotnet test, I get the following:

Starting test execution, please wait... Could not find testhost.dll for source '[...].Tests.dll'. Make sure test project has a nuget reference of package "microsoft.testplatform.testhost".

I have actually added the suggested Microsoft.TestPlatform.TestHost NuGet package, but that hasn't changed anything.

So what is the problem here?

I'm using Visual Studio 2017. Not that I think it makes a difference.

Update: changing the test project from Class Library (.NET Standard) to Class Library (.NET Core) fixed the problem. I still don't get why this is supposed to make a difference.

Burnedout answered 21/3, 2017 at 21:57 Comment(0)
P
15

Changing the test project from Class Library (.NET Standard) to Class Library (.NET Core) fixed the problem. I still don't get why this is supposed to make a difference.

Unit tests are applications that we run. To build such an application, we must specify a runtime and application model. When we target .NET Standard, the runtime and application model are ambiguous; MSBuild does not know whether to build against .NET Framework, .NET Core, Mono/Xamarin, or another .NET Standard compliant platform. Targeting .NET Core provides the required input to MSBuild, which now knows how to resolve all the referenced assemblies/projects and choose the appropriate framework version.

But that was never the case. In the past, you would always use a class library for unit tests; it was never considered an application that you could run, but rather a collection of classes and methods that you would feed into a test runner.

In the past we did not have .NET Standard, which is an ambiguous target. When MSBuild sees .NET Standard it needs more information. "Okay, which .NET Standard compliant runtime do you want to use to produce the runnable output?" If for instance we had targeted netstandard1.2, then MSBuild would not know whether to build against .NET Core 1.0, .NET Framework 4.5.1, Windows 8.1, or the several other netstandard1.2 compliant platforms.

enter image description here

Starting test execution, please wait... Could not find testhost.dll for source '[...].Tests.dll'. Make sure test project has a nuget reference of package "microsoft.testplatform.testhost".

If we do not specify netcoreapp, then MSBuild assumes that we are using the full framework. In that case, it expects the target assemblies, including testhost.dll, to be in the bin. If they are not (and they will not be if we built against .NET Standard), then we will receive the above error.

Photoengraving answered 22/3, 2017 at 4:8 Comment(6)
But that was never the case. In the past, you would always use a class library for unit tests; it was never considered an application that you could run, but rather a collection of classes and methods that you would feed into a test runner.Burnedout
Good question. I will look into that. I've assumed that the tests need the application model but it also might be that the tests need an implementation of .NET Standard (such as .NET Core) instead of only the set of interfaces that .NET Standard provides.Photoengraving
under the hood, the Microsoft.NET.Test.Sdk will turn the project from a class library to a console application when building for .net core so that all assets needed to "run" are generated.Fractious
Reading the answer I still don't know how to make xUnit tests work with NET Standard. Could you please indicate how? there are plenty of choices. Should we target .NET Core? Should we target .NET Core App? Which version? ... what about showing us a sample csproj (XML)?Icbm
@Icbm Target a framework that implements .NET Standard. That can be one of netcoreapp, net, uap, win, wpa, and wp.Photoengraving
Good explanation.Konya
A
4

My issue was the same, with message in Output > Test console:

Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException:
Unable to find [test-dir]\bin\Debug\netcoreapp3.1\testhost.dll.
Please publish your test project and retry.

I have xUnit test based on packages:

  • xunit (2.4.1)
  • xunit.runner.visualstudio (2.4.2)

What helps me:

  • remove package: Microsoft.NET.Test.Sdk
  • install package: Microsoft.TestPlatform.TestHost

Now works and even faster !

All xUnit based Test Project packages in VS.NET:

All xUnit base project packages in VS.NET

In NuGet Package Manager:

Package Microsoft.TestPlatform.TestHost in NuGet Package Manager

Amok answered 21/7, 2020 at 15:7 Comment(1)
Interesting; xunit.runner.visualstudio 2.4.1 referenced Microsoft.TestPlatform.TestHost through a reference to the Microsoft.NET.Test.Sdk package. I had to add these two packages back after upgrading xunit.runner.visualstudio to 2.4.3 for the tests to run. Unfortunately these changes didn't resolve the testhost.xml error.Dietetic

© 2022 - 2024 — McMap. All rights reserved.