我有一个简单的dotnet核心类库和一个XUnit测试方法:

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);
        }
    }
}

如果我在CLI中输入项目并输入dotnet build,项目就会生成。如果我输入dotnet test,我得到这个:

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.

我需要更改哪些内容才能运行测试?

如果有帮助的话,VS Code也没有在它的测试资源管理器中显示测试。


当前回答

必须添加Microsoft.TestPlatform.TestHost来获得testhost.dll。 我发现在这个答案https://github.com/dotnet/sdk/issues/7171#issuecomment-261506546

其他回答

我遇到过几次这种情况,我总是忘记发生了什么。最近我有:

类库->针对。net Core 3.0 测试项目->针对。net Core 3.1

我的测试项目包:

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

我看到:

无法找到C:\PATH\bin\Debug\netstandard2.0\ testthost .dll请发布测试项目并重试。

我所需要做的就是将缺失的nuget包添加到我的测试项目中: “Microsoft.NET.Test.SDK”

这时一切都恢复了正常。

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>

手动在我的csproj文件中。然后问题就解决了。

我使用的是。net核心框架6.0,为了解决这个问题,我不得不将Microsoft.NET.Test.Sdk从17.1.0版本降级到16.5.0版本。

这发生在我将Microsoft.NET.Test.Sdk从v16.2.0更新到v16.4.0后,使用<TargetFramework>netcoreapp2.0</TargetFramework>。更新到<TargetFramework>netcoreapp3.0</TargetFramework>解决了我的问题。

在我的情况下,Microsoft.NET.Test.Sdk已经安装,我必须在Nuget包管理器上重新安装它,问题得到了解决。