233

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

TestLib.csproj:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.SDK" Version="15.9.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.console" Version="2.4.1">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="xunit.runners" Version="2.0.0" />
  </ItemGroup>

</Project>

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.

What do I need to change to get the test to run?

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

Matt W
  • 11,753
  • 25
  • 118
  • 215
  • In my case, it was that you can't actually run tests against netstandard2.0, since that's an API definition, not a runtime. If you switch the TFM to net472, things work just fine. Alternatively, you can multi-target to netcore+net472 for example and run against both. – kzu Sep 20 '20 at 18:58
  • worked for me : https://github.com/microsoft/vstest/issues/1870#issuecomment-452120663 – Suchith May 19 '22 at 07:55

27 Answers27

388

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

R15
  • 13,982
  • 14
  • 97
  • 173
  • That was already included in my post - but you're right: there will be big problems in running unit tests with dotnet core without it. – Matt W Apr 02 '19 at 07:54
  • 3
    "Microsoft.NET.Test.Sdk" was the missing piece when you add a class library project and turn that into a test project. Probably the best thing to do would be to add a new test project then add needed nuget packages like Rhino or Moq etc... – Yawar Murtaza Apr 23 '19 at 10:59
  • 4
    Created .NET Standard 2.0 lib, added `xunit`, `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk` to the project, still the same result. I think there is yet another factor at play... – Manfred May 18 '19 at 01:12
  • 14
    The problem in my case was caused by creating a `netstandard2.0` project instead of a `netcoreapp2.2` project. As soon as I switched to the latter, it worked. The only nuget packages I needed were `xunit`, `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`. – Manfred May 18 '19 at 01:18
  • This doesn't work for me. Microsoft.NET.Test.Sdk is installed. – Tom W Aug 27 '19 at 13:01
  • Fixed my issue after making a class library and using nUnit as well. – Rory Oct 03 '19 at 12:35
  • Merely updating to the latest package version of this fixed it for me. – slasky Oct 18 '19 at 16:43
  • 2
    Installing Microsoft.NET.Test.Sdk didn't work for me either, UNTIL I did `dotnet clean` – IGx89 Nov 06 '19 at 17:45
  • There is another factor at play indeed. My testrunner was confused after I changed the `TargetFramework` from `netstandard2.0` to `net462`. If you go to the output window and change it show output from `Tests`, you may see that it is unable to find testhost.dll, but that it is looking in the `netstandard2.0` folder. I was unable to fix the testrunner, so I created a new project and just copied the test files. – Alexander Høst Dec 19 '19 at 16:26
  • Thanks for this hint. I added "Microsoft.NET.Test.Sdk" but also required reference to "xunit.runner.visualstudio" Note: Mine is on VS 2019 and .NET 5 – RSF Apr 19 '21 at 03:49
41

In my case, the problem was that I was targeting .NET Core 2.0 and switching to .NET Core 2.1 solved the problem. However I was using Microsoft.NET.Test.SDK v16.4.0 instead of 15.9.0.

Andreas
  • 6,447
  • 2
  • 34
  • 46
36

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 this helpful page.

Matt W
  • 11,753
  • 25
  • 118
  • 215
  • 3
    After executing this command you may want to update the nuget packages the new project references. – Manfred May 18 '19 at 01:19
  • Or install nuget xunit.runner.visualstudion to existing project ;) – Lukáš Kmoch Feb 20 '20 at 21:48
  • Is that a typo? I can't find that one. – Matt W Feb 21 '20 at 12:27
  • If you have an existing project, you can pass the name of that with `--force` to force it to rebuild the project as an xUnit test project. As per @Manfred's comment, you'll need to update / re-add any project references you had in that project. – Myles Jul 04 '20 at 16:21
  • 2
    @MattW Yes, that looks like a typo. I think @Lukas meant `xunit.runner.visualstudio` which you can find at https://www.nuget.org/packages/xunit.runner.visualstudio/ – Manfred Jul 07 '20 at 09:31
30

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 <IsTestProject>true</IsTestProject> 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

     <PropertyGroup>
...
        <IsTestProject>false</IsTestProject>
...
      </PropertyGroup>

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

21

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.

See https://stackoverflow.com/a/63786758/3248302

RonC
  • 31,330
  • 19
  • 94
  • 139
Maximiliano Rios
  • 2,196
  • 2
  • 20
  • 34
  • 2
    I had the same problem - for some of the test projects in my solution. The common factor for the test projects failing after update to 2.4.2 was that those projects were missing Microsoft.Net.Test.Sdk (never been a problem before). Added the 16.6.1 nuget and was back to working again. – bit0001 Jun 05 '20 at 14:46
  • Nice, I didn't know that. I downgraded to make it work – Maximiliano Rios Jun 05 '20 at 18:25
  • 1
    I've fixed it in the same way. Downgrade of xUnit.runner.visualstudio package to 2.4.1 was solve problem. – Jacek Labuda Jun 08 '20 at 14:10
  • 1
    I can confirm it is still an issue with version 2.4.3 of xunit.runner.visualstudio. Downgrade to 2.4.1 solves the problem. – bN_ Aug 19 '20 at 15:08
  • I had to downgrade a lot of projects because of this. I accidentally upgraded everything and it stopped working – Maximiliano Rios Aug 19 '20 at 16:10
  • 3
    `xunit.runner.visualstudio` versions <= 2.4.1 include a reference to `Microsoft.NET.Test.Sdk`. Later versions don't, so you need to add the reference to your project. See https://stackoverflow.com/a/63786758/3248302. – Blisco Nov 09 '20 at 15:42
  • @Blisco That seems exactly right. You should make this comment into an answer. This is exactly what I needed to know to fix my issue. – RonC Nov 24 '20 at 19:13
  • I will update my answer to reflect that. I am very happy it worked for you – Maximiliano Rios Nov 25 '20 at 14:59
15

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.

Dev Leader
  • 312
  • 3
  • 9
13

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.

Raj kumar
  • 1,275
  • 15
  • 20
  • 1
    This was my problem in particular. Doh! I should have caught that sooner. Thanks for your response as it put me on the right path :) – Dev Leader Mar 24 '20 at 20:03
  • 1
    Changing the test project to be a .Net Core app allowed the xunit.runner.visualstudio package to install correctly. Please note that you will probably need to close your solution and reload it in order for VisualStudio to sort out the changes. – Sheldon May 13 '20 at 12:12
12

Fixed it by installing xunit.runner.visualstudio.

Eugene Ihnatsyeu
  • 1,299
  • 13
  • 15
12

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

SteveHansen
  • 361
  • 3
  • 10
8

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

Äkwav
  • 81
  • 2
  • 2
  • 1
    It is nice that you are giving other people credit, but here on stackoverflow, we prefer that you also include enough information here. Just in-case the web link that you gave becomes broken/invalid someday. – tgolisch Dec 16 '20 at 21:54
  • Okay, I revisited the link, there is no information to add to my answer. Should I better remove it then? – Äkwav Dec 22 '20 at 21:13
  • 1
    Actually, your answer seems helpful. I'm not suggesting that you delete it. Community standards recommend that your answer contains the info instead of a link (paraphrase but not plagiarize), so the info is on S/O, without having to click-through. – tgolisch Dec 22 '20 at 23:50
7

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:
<!-- Bad: -->
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" ExcludeAssets="All" />
<!-- Good: -->
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
Jpsy
  • 20,077
  • 7
  • 118
  • 115
  • Simple installed Microsoft.NET.Test.Sdk xunit.runner.visualstudio and it works for me. Thanks! – smrsgv Feb 09 '22 at 14:35
6

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:

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />

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.

Michael Armitage
  • 1,502
  • 19
  • 17
6

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

<TargetFramework>netcoreapp3.0</TargetFramework>

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

Hakakou
  • 522
  • 3
  • 6
6

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.

Blisco
  • 601
  • 6
  • 14
2

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

2

As of today 18.05.2023 with VS 2022 you have to install the following in addition to xunit because otherwise VS will not be able to detect the tests.

  • Microsoft.NET.Test.Sdk - this will solve the "TestPlatformException: Could not find testhost"
  • xunit.runner.visualstudio - this will connect the tests visible to VS runner
Ognyan Dimitrov
  • 6,026
  • 1
  • 48
  • 70
1

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.

S1r-Lanzelot
  • 2,206
  • 3
  • 31
  • 45
1

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.

reim
  • 492
  • 5
  • 8
1

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.

Venu
  • 109
  • 1
  • 4
1

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.

ihebiheb
  • 3,673
  • 3
  • 46
  • 55
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.

Shawon Barua
  • 61
  • 10
0

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

0

I was using .NET Core Framework 6.0 I had to downgrade Microsoft.NET.Test.Sdk from Version 17.1.0 to 16.5.0 to resolve the issue.

tmndungu
  • 320
  • 4
  • 17
0

In my case Microsoft.NET.Test.Sdk was already installed, I had to reinstall it on Nuget Package Manager and the issue got fixed.

0

Restart Visual Studio and do a rebuild. Visual Studio seems to get confused sometimes after switching branches.

vmb100
  • 127
  • 1
  • 7
0

I was receiving the error due to multiple dlls being picked up by the filter instead of just the one dll which contained the tests.

Pellet
  • 2,254
  • 1
  • 28
  • 20
-1

I downloaded .Net.Test.SDK and .Testplatform.TestHost via Nuget to my project but test kept not working. xunit.runner.visualstudio did the trick.

Furki4_4
  • 11
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 24 '22 at 23:14