I have a simple dotnet core class library with a single XUnit test method:

TestLib.csproj:


  
    netstandard2.0
  

  
    
    
    
      runtime; build; native; contentfiles; analyzers
      all
    
    
      runtime; build; native; contentfiles; analyzers
      all
    
    
  



BasicTest.cs:
using Xunit;

namespace TestLib
{
    public class BasicTest
    {
        [Fact(DisplayName = "Basic unit test")]
        [Trait("Category", "unit")]
        public void TestStringHelper()
        {
            var sut = "sut";
            var verify = "sut";

            Assert.Equal(sut, verify);
        }
    }
}

If I enter the project on the CLI and type dotnet build the project builds. If I type dotnet test I get this:

C:\git\Testing\TestLib> dotnet test
C:\git\Testing\TestLib\TestLib.csproj : warning NU1701: Package 'xunit.runner.visualstudio 2.4.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.
Build started, please wait...
C:\git\Testing\TestLib\TestLib.csproj : warning NU1701: Package 'xunit.runner.visualstudio 2.4.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.
Build completed.

Test run for C:\git\Testing\TestLib\bin\Debug\netstandard2.0\TestLib.dll(.NETStandard,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 16.0.0-preview-20181205-02
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
Unable to find C:\git\Testing\TestLib\bin\Debug\netstandard2.0\testhost.dll. Please publish your test project and retry.

Test Run Aborted.

Что мне нужно изменить, чтобы запустить тест?

If it helps, VS Code is not displaying the tests in its test explorer, either.

Matt W

Ответы (21)

В моем случае проблема заключалась в том, что я ориентировался на .NET Core 2.0, и переход на .NET Core 2.1 решил проблему. Однако я использовал Microsoft.NET.Test.SDK v16.4.0 вместо 15.9.0.

If you are running a project by cloning than Solution is to install the Microsoft.NET.Test.Sdk. How to : Tools>Nuget Package Manager>Manage Nuget Packages For Solution...>Search for Microsoft.NET.Test.Sdk and install for your test project.

Installing Microsoft.NET.Test.Sdk package from nuget package manager solved my issue.

This happened to me after updating Microsoft.NET.Test.Sdk from v16.2.0 to v16.4.0 with netcoreapp2.0. Updating to netcoreapp3.0 resolved the issue for me.

Had to add Microsoft.TestPlatform.TestHost to get testhost.dll. I found that in this answer https://github.com/dotnet/sdk/issues/7171#issuecomment-261506546

Fixed it by installing xunit.runner.visualstudio.

This error can occur if you upgrade xunit.runner.visualstudio to a version greater than 2.4.1. Versions up to and including 2.4.1 include a reference to Microsoft.NET.Test.Sdk but later ones don't, so you need to include the reference in your own project.

Interestingly, I found that NCrunch still ran my tests without the additional reference, even though I couldn't run them via the CLI.

In my case the problem was that I have an extension project for xunit. There is also a test project to test the extensions. When I ran dotnet test on my solution, my extension project was also picked up as a unit test project (it took me some time to realize this). The reason for this is that it references some xunit packages. One of these xunit packages automatically sets the true property in you csprj file. This is actually a good thing since 99.99% of the projects that reference xunit are actually unit tests. I could finally solve this by explicitly setting

     
...
        false
...
      

Manually in my csproj file. Then the problem went away.

In my case, it was necessary to include a reference to MsTest.TestAdapter module using nuget.

A fresh project with MSTest.TestFramework and Microsoft.Net.Test.Sdk was not enough to run a single unit test.

Noticing in my case I was using a test project targeting .NET framework 4.8 and not .NET core. Although, I strongly believe this fix might apply to that platform as well

I had created a class library and tried to use the XUnit NuGet package in it.

What I should have done was created an XUnit project using this command: dotnet new xunit -n TestProject

Я нашел эту полезную страницу .

I found a very interesting compatibility issue with a version. I did upgrade just as a normal practice, my code, and I switched to xUnit.runner.visualstudio 2.4.2. It stopped working for .Net Core 3.1. I had to downgrade to 2.4.1 and it started working again.

Additional information after one of my comments.

The package xunit.runner.visualstudio versions <= 2.4.1 includes a reference to Microsoft.NET.Test.Sdk. Later versions don't, so you need to add the reference to your project.

См. Https://stackoverflow.com/a/63786758/3248302

I found 5 factors to be crucial.
(3 of whom were spread throughout the other answers here in mixed variations.)

1 to 4:
Add these Nuget Packages in versions that work for your .NET Core version:

  • xunit
  • Microsoft.NET.Test.Sdk
  • xunit.runner.visualstudio
  • Microsoft.AspNetCore.Mvc.Testing

5:
Now make sure that Microsoft.NET.Test.Sdk is set to NOT ExcludeAssets. To do so use one of these methods:

  1. In Project Explorer go to Dependencies –> NuGet and find the Sdk package. Right click it and select "Properties". Remove "All" from the field ExcludeAssets.
  2. Alternatively edit your .csproj file and remove ExcludeAssets="All" from the Sdk package entry, if present:




Ran into this error, the root cause was the tests were hitting the maximum length for a Windows path (MAX_PATH), which is defined as 260 characters.

If you are using xUnit, make sure your project type is not as netstanderd. As xUnit doesn't support netstanderd, change it to coreapp2.0 or others.

I was building a netcoreapp2.2 test project and then trying to run dotnet vstest from the bin folder. I noticed that the Microsoft Test DLLs from:


were not being output to my bin folder. Instead of just building, I ran a publish instead which did include the necessary DLLs in the output folder and I was then able to run dotnet vstest from there.

If you are targeting netstandard2.0 this will not work. If are using .NET Core. make sure the .csproj contains the following lines:

netcoreapp3.0

and also contains the package Microsoft.NET.Test.Sdk

This could also be caused by inadvertently trying to run a non-test project, this usually happens when your test files filter is too wide.

In my case it was a silly mistake. I was referencing xunit also in my source project (not only in my test project)

Deleting the xunit dependency in my source project fixed the problem.

Got this error, when trying to debug a unit test. Below are the steps I tried.

  • Step 1: Installed Microsof.TestPlatform.TestHost and tried to run the test but no luck.
  • Step 2: Changed Target framework from .NET Core 2.0 to 2.1 and tried to run the test but no luck.
  • Step 3: Closed and opened VS2017 and tried to run.

Yay!!! it worked :-) Never miss to try the last step ;-) Hope this helps someone like me.

same issue i faced for Nunit (.net core 3.1) project . I was using Microsoft.NET.Test.SDK v16.6.1, I downgraded the version to 15.9.0. And it start working

I've encountered this a couple of times and I always forget what's up. Most recently I had:

  • Class Library -> Targeting .NET Core 3.0
  • Test Project -> Targeting .NET Core 3.1

Packages for my test project:

  • Moq -> 4.14.1
  • xUnit -> 2.4.1
  • xUnit.Runner.VisualStudio -> 2.4.2

I was seeing:

Unable to find C:\PATH\bin\Debug\netstandard2.0\testhost.dll. Please publish your test project and retry.

And all that I needed to do was add to my test project the missing nuget package: "Microsoft.NET.Test.SDK"

Everything was back to normal at this point.

2022 WebDevInsider