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

natemcmaster picture natemcmaster · Feb 16, 2017 · Viewed 9.8k times · Source

I have a .NET Core test project that uses Xunit 2.2. Some of my tests are marked with traits.

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

What is the right command line syntax for "dotnet test" to only run tests where the trait Color == Blue ?

I'm using .NET Core CLI 1.0.0-rc4 which uses csproj, not project.json.

I'm trying to use dotnet test --filter $something, but whatever I use for $something, I see this error:

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.

Answer

natemcmaster picture natemcmaster · Feb 17, 2017

I found the answer (only tests attributed [Trait("TraitName", "TraitValue")] are executed):

dotnet test --filter TraitName=TraitValue

Alternatively, you can filter by not having a trait value (tests attributed [Trait("TraitName", "TraitValue")] are execluded from run)

dotnet test --filter TraitName!=TraitValue

In my example above, this means I can run:

dotnet test --filter Color=Blue

More docs here: https://github.com/Microsoft/vstest-docs/blob/master/docs/filter.md