你如何用"dotnet测试"过滤特征的xunit测试?

nat*_*ter 18 xunit .net-core

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

[Fact]
[Trait("Color", "Blue")]
public void TestBlue()
{
}
Run Code Online (Sandbox Code Playgroud)

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

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

我正在尝试使用dotnet test --filter $something,但无论我使用什么东西,我看到这个错误:

错误:[xUnit.net 00:00:00.7800155] E2ETests:异常过滤测试:没有测试与过滤器匹配,因为它包含一个或多个无效的属性($ something).指定包含有效属性(DisplayName,FullyQualifiedName)的过滤器表达式,然后重试.

nat*_*ter 29

我找到了答案:

dotnet test --filter TraitName=TraitValue
Run Code Online (Sandbox Code Playgroud)

或者,您可以通过具有特征值进行过滤

dotnet test --filter TraitName!=TraitValue
Run Code Online (Sandbox Code Playgroud)

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

dotnet test --filter Color=Blue
Run Code Online (Sandbox Code Playgroud)

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

  • 以下工作:`dotnet test --filter TraitName!= TraitValue`但是只要在测试程序集中有一些用`[Trait("TraitName","somevalue")]`装饰的测试...如果有的话然后没有测试运行:-(.可能是dotnet测试中的一个错误? (5认同)

and*_*ian 5

在 csproj 中

<PropertyGroup>
  <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
  <PackageReference Include="xunit" Version="2.3.0" />
  <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

命令行

dotnet xunit -trait "Color=Blue"
Run Code Online (Sandbox Code Playgroud)