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.
Testing a positive case This method is decorated with the Fact attribute, which tells xUnit that this is a test.
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.
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.
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).
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"
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.
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
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.
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
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With