我有一个简单的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也没有在它的测试资源管理器中显示测试。


当前回答

通过安装xunit.runner.visualstudio修复。

其他回答

遇到此错误,根本原因是测试达到了Windows路径的最大长度(MAX_PATH),其定义为260个字符。

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

就我而言,问题在于我的目标是。net Core 2.0,而切换到。net Core 2.1就解决了这个问题。但是我使用的是Microsoft.NET.Test.SDK v16.4.0而不是15.9.0。

我下载了。net . test。SDK和. testplatform。testthost通过Nuget到我的项目,但测试一直不工作。Xunit.runner.visualstudio成功了。

在调试单元测试时,遇到此错误。以下是我尝试过的步骤。

步骤1:安装microsoft . testplatform . testhost并尝试运行测试,但运气不好。 步骤2:将目标框架从。net Core 2.0更改为2.1,并尝试运行测试,但运气不好。 步骤3:关闭并打开VS2017并尝试运行。

耶! !它起作用了:-) 千万不要错过最后一步;-)希望这能帮助到像我这样的人。