Unable to find testhost.dll. Please publish your test project and retry

C#Unit Testing.Net Corexunit.net

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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

Solution 6 - C#

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.

Solution 7 - C#

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.

Solution 8 - C#

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.

Solution 9 - C#

Fixed it by installing xunit.runner.visualstudio.

Solution 10 - C#

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.

Solution 11 - C#

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

Solution 12 - C#

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

Solution 13 - C#

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" />

Solution 14 - C#

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.

Solution 15 - C#

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

Solution 16 - C#

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.

Solution 17 - C#

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.

Solution 18 - C#

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.

Solution 19 - C#

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.

Solution 20 - C#

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.

Solution 21 - C#

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

Solution 22 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt WView Question on Stackoverflow
Solution 1 - C#R15View Answer on Stackoverflow
Solution 2 - C#AndreasView Answer on Stackoverflow
Solution 3 - C#Matt WView Answer on Stackoverflow
Solution 4 - C#Willy Van den DriesscheView Answer on Stackoverflow
Solution 5 - C#Maximiliano RiosView Answer on Stackoverflow
Solution 6 - C#Dev LeaderView Answer on Stackoverflow
Solution 7 - C#SteveHansenView Answer on Stackoverflow
Solution 8 - C#Raj kumarView Answer on Stackoverflow
Solution 9 - C#Eugene IhnatsyeuView Answer on Stackoverflow
Solution 10 - C#Michael ArmitageView Answer on Stackoverflow
Solution 11 - C#ÄkwavView Answer on Stackoverflow
Solution 12 - C#HakakouView Answer on Stackoverflow
Solution 13 - C#JpsyView Answer on Stackoverflow
Solution 14 - C#BliscoView Answer on Stackoverflow
Solution 15 - C#SaifAli SanadiView Answer on Stackoverflow
Solution 16 - C#S1r-LanzelotView Answer on Stackoverflow
Solution 17 - C#reimView Answer on Stackoverflow
Solution 18 - C#VenuView Answer on Stackoverflow
Solution 19 - C#ihebihebView Answer on Stackoverflow
Solution 20 - C#Shawon BaruaView Answer on Stackoverflow
Solution 21 - C#Aislan Gelatti RochaView Answer on Stackoverflow
Solution 22 - C#tmndunguView Answer on Stackoverflow