Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 743
natemcmaster Avatar asked Feb 16 '17 00:02

natemcmaster


People also ask

Which attribute is used to mark a method as test method in xUnit?

Testing a positive case This method is decorated with the Fact attribute, which tells xUnit that this is a test.

What dotnet command executes xUnit tests?

The dotnet test command builds the solution and runs a test host application for each test project in the solution. The test host executes tests in the given project using a test framework, for example: MSTest, NUnit, or xUnit, and reports the success or failure of each test.

Does xUnit work with .NET framework?

xUnit.Net is an open source unit testing tool for the . Net Framework that provides an easy way to work with data driven unit tests.

What test runners can be used to test xUnit.net tests?

If you have Visual Studio Community (or a paid-for version of Visual Studio), you can run your xUnit.net tests within Visual Studio's built-in test runner (named Test Explorer).

How to run tests by traits in DotNet?

However, nowadays, most people will run tests on .NET Core using the `dotnet test` command. In those cases, syntax to filter by traits is: dotnet test --filter "Category=Bug&Category!=Integration"

What is DotNet test-filter XYZ?

For example, dotnet test --filter xyz is the same as dotnet test --filter FullyQualifiedName~xyz. Property is an attribute of the Test Case. For example, the following properties are supported by popular unit test frameworks. Value is a string. All the lookups are case insensitive.

How do I filter a dotnet test?

With the dotnet version 1.0.0, you have to use the --filter option: You can filter by DisplayName, FullyQualifiedName and Traits. Ex: dotnet test --filter "FullyQualifiedName=YourNamespace.TestClass1.Test1" Also, these operators are allowed: =, != and ~ (contains). More info here: docs Share Follow answered Mar 14 '17 at 13:42

What is @xUnit trait in SpecFlow?

xUnit: Trait (similar functionality, SpecFlow will insert a Trait attribute with Category name) This category can be used to filter the test execution in your build pipeline. Note that the incorrect filter can lead to no test getting executed. You don’t have to include the @ prefix in the filter expression.


2 Answers

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

like image 120
natemcmaster Avatar answered Oct 23 '22 18:10

natemcmaster


In the csproj

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

command line

dotnet xunit -trait "Color=Blue" 
like image 33
andrei.ciprian Avatar answered Oct 23 '22 17:10

andrei.ciprian