Recently, I was doing some analysis on some tests to perform some optimizations. While performing some analysis, I wanted to run only subset of tests. And while doing this activity, I figured out an awesome option provided by
dotnet CLI to run subset of tests from a given test project. In this article, I will try to discuss that option with you all.
There are no specific prerequisites as such for this discussion. If you want to try the things mentioned in this article, you would just need a C# .NET based test project so that we should be able to use dotnet CLI.
Why do we need to run subset of tests ?
When we are writing new tests, we may want to run only the tests we have added. Also, sometimes, we may just want to run subset of tests for various reasons – e.g. we may want to run tests only from affected area.
If the tests are grouped in separate projects based on functional areas and we want to run all tests from a given functional area, then it may be easier. We just need to run all tests from a given test project in that case.
But if we want to run a subset of tests from a single test project, it may be a bit tricky.
What is the well-known approach?
For skipping the tests, the most widely used option is to modify the code and use
Ignore (in NUnit and MSTest projects) or
[Fact(Skip="reason")] (in xUnit test projects) attributes on tests as shown in the code snippet given below.
Why I did not want to go with the above mentioned approach ?
But I see there are two problems with this commonly used approach approach
- First problem is, when I want to run only a few of the many tests, I need to put these attributes at a lot of places. Thus, it is time killing activity and probably it may be faster to run all the tests than modifying the test files that we do not want to run.
- The source code files are modified. Hence, there is a chance that we may forget to remove this kind of temporary changes and it may lead to accidentally merging this code to the GIT repo.
What is my preferred approach?
Generally, I like to use dotnet CLI for running the tests. Somehow I feel that it is a bit quicker (as I can specify no build and no restore options in CLI). Also, it helps to keep my IDE available to me in case I want to browse through the same code or modify some code.
I recently discovered that
dotnet test command provides a switch
filter which can be used to run the subset of tests. In this switch we can specify the filters. Only the tests matching to specified filters are executed by the command. And that’s how simple it is. But wait…. now the question is – how can we specify the filters ?
How to specify filters in dotnet CLI ?
For specifying filters we need to of course, add the
filter switch, followed by the
property name , followed by some operator (
contains / does not contain / equals / does not equal ), which is followed by
The property name to be used in command depends on which properties are supported by the concerned test framework.
- If you are using
xUnit, then you can use FullyQualifiedName, DisplayName and Category properties in the filter followed by some operator and then value.
- If you are using
NUnit, then you can use FullyQualifiedName, Name, TestCategory and Priority.
- If you are using
MSTestframework, then you can use FullyQualifiedName, Name, ClassName, Priority, and TestCategory.
There are four different operators supported here:
|Not exact match|
The multiple conditions can be combined using logical operators (
| is an OR operator and
& is an AND operator).
For more details, I would suggest to refer the documentation.
The code snippet given below shows sample commands, which can be used.
As you can see, with this approach, we do not need to modify the code. So there is no question of accidentally committing temporary code modifications. Also, it is quicker way as well instead of modifying the source code.
I hope you find this information helpful. Have you tried this switch already? Did you find it helpful? Let me know your thoughts.