我有一个简单的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 NuGet包。

我应该做的是使用以下命令创建一个XUnit项目:dotnet new XUnit -n TestProject

我找到了这个有用的页面。


从nuget包管理器安装Microsoft.NET.Test.Sdk包解决了我的问题。


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


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文件中。然后问题就解决了。


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


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


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


如果您正在使用xUnit,请确保您的项目类型不是netstandd。由于xUnit不支持netstandard,请将其更改为coreapp2.0或其他。


我正在构建一个netcoreapp2.2测试项目,然后试图从bin文件夹中运行dotnet vstest。我注意到微软测试dll来自:

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

没有输出到我的bin文件夹。而不是只是构建,我运行了一个发布,而不是在输出文件夹中包含必要的dll,然后我就可以从那里运行dotnet vstest。


如果你的目标是netstandard2.0,这是行不通的。如果你使用。net Core。确保.csproj包含以下行:

<TargetFramework>网芯应用3.0</TargetFramework>

还包含Microsoft.NET.Test.Sdk包


我在Nunit (.net core 3.1)项目中也遇到了同样的问题。 我使用的是Microsoft.NET.Test.SDK v16.6.1,我将版本降级到15.9.0。 它开始工作了


我发现了一个非常有趣的版本兼容性问题。我确实按照惯例升级了我的代码,并切换到xUnit.runner.visualstudio 2.4.2。它不再适用于。net Core 3.1。我不得不降级到2.4.1,它又开始工作了。

附加信息后,我的意见之一。

xunit.runner.visualstudio versions <= 2.4.1包包含了对Microsoft.NET.Test.Sdk的引用。后来的版本没有,所以需要将引用添加到项目中。

参见https://stackoverflow.com/a/63786758/3248302


如果您正在通过克隆运行项目,那么解决方案是安装Microsoft.NET.Test.Sdk。 如何: 工具>Nuget包管理>管理解决方案的Nuget包…搜索Microsoft.NET.Test.Sdk并安装您的测试项目。


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

类库->针对。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”

这时一切都恢复了正常。


这也可能是由于无意中试图运行一个非测试项目造成的,这通常发生在你的测试文件过滤器太宽的时候。


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

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

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


如果将xunit.runner.visualstudio升级到高于2.4.1的版本,则可能发生此错误。包括2.4.1在内的版本包含了对Microsoft.NET.Test.Sdk的引用,但之后的版本没有,所以您需要在自己的项目中包含该引用。

有趣的是,我发现NCrunch仍然在没有额外引用的情况下运行我的测试,即使我不能通过CLI运行它们。


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


在我的例子中,有必要包含对MsTest的引用。TestAdapter模块使用nuget。

MSTest的一个新项目。TestFramework和Microsoft.Net.Test.Sdk不足以运行单个单元测试。

注意,在我的例子中,我使用的是一个针对。net框架4.8而不是。net核心的测试项目。尽管如此,我坚信这一修正也适用于该平台


我发现了5个至关重要的因素。 (其中3个问题以混合变化的形式分布在这里的其他答案中。)

1 ~ 4: 在你的。net Core版本中添加以下Nuget包:

xunit Microsoft.NET.Test.Sdk xunit.runner.visualstudio Microsoft.AspNetCore.Mvc.Testing

5: 现在确保Microsoft.NET.Test.Sdk设置为NOT ExcludeAssets。要做到这一点,可以使用以下方法之一:

在项目资源管理器中,进入Dependencies - > NuGet,找到Sdk包。右键点击它,选择“属性”。将“All”从字段ExcludeAssets中移除。 或者编辑你的.csproj文件,并从Sdk包条目中删除ExcludeAssets="All",如果存在的话:

<!-- Bad: -->
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" ExcludeAssets="All" />
<!-- Good: -->
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />

对我来说,这是个愚蠢的错误。我也在我的源项目中引用xunit(不仅仅是在我的测试项目中)

删除源项目中的xunit依赖项解决了这个问题。


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


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


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


重新启动Visual Studio并重新构建。在切换分支后,Visual Studio有时似乎会感到困惑。