Unable to find testhost.dll. Please publish your t

2020-05-24 19:27发布

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.

11条回答
Rolldiameter
2楼-- · 2020-05-24 19:40

Fixed it by installing xunit.runner.visualstudio.

查看更多
够拽才男人
3楼-- · 2020-05-24 19:41

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.

查看更多
Ridiculous、
4楼-- · 2020-05-24 19:44

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.

查看更多
做个烂人
5楼-- · 2020-05-24 19:50

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.

查看更多
狗以群分
6楼-- · 2020-05-24 19:55

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.

查看更多
登录 后发表回答