如何使用 "dotnet test" 按特征过滤 xunit 测试?

How do you filter xunit tests by trait with "dotnet test"?

我有一个使用 Xunit 2.2 的 .NET Core 测试项目。我的一些测试标有特征。

[Fact]
[Trait("Color", "Blue")]
public void TestBlue()
{
}

"dotnet test" 仅 运行 测试特征 Color == Blue 的正确命令行语法是什么?

我正在使用 .NET Core CLI 1.0.0-rc4,它使用 csproj,而不是 project.json。

我正在尝试使用 dotnet test --filter $something,但无论我将其用于 $something,我都会看到此错误:

Error: [xUnit.net 00:00:00.7800155] E2ETests: Exception filtering tests: No tests matched the filter because it contains one or more properties that are not valid ($something). Specify filter expression containing valid properties (DisplayName, FullyQualifiedName) and try again.

我找到了答案(只有 个归因于 [Trait("TraitName", "TraitValue")] 的测试被执行:

dotnet test --filter TraitName=TraitValue

或者,您可以通过 而不是 具有特征值进行过滤(归因于 [Trait("TraitName", "TraitValue")] 的测试被排除在 运行 之外)

dotnet test --filter TraitName!=TraitValue

在我上面的例子中,这意味着我可以 运行:

dotnet test --filter Color=Blue

这里有更多文档:https://github.com/Microsoft/vstest-docs/blob/master/docs/filter.md

在csproj

<PropertyGroup>
  <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
  <PackageReference Include="xunit" Version="2.3.0" />
  <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0" />
</ItemGroup>

命令行

dotnet xunit -trait "Color=Blue"