Set up coverage filters
Sometimes, you may need to limit the scope of coverage analysis. For example, this makes sense if you participate in developing a large application with multiple projects and thousands of tests. In this case, you don't need to analyze coverage of all projects (types, type members) in the solution but only those related to the code you're working on. Another example is "reducing the noise" of a coverage tree by excluding the nodes (namespaces, classes, or methods) you're not currently interested in. In all such cases, you should apply coverage filters.
Filter types:
- Runtime coverage filters
JetBrains Rider applies these filters during a coverage session. Use these filters to exclude unnecessary code from analysis and reduce coverage session time.
You can specify runtime coverage filters using JetBrains Rider settings or by adding comments to the source code.
- Filters of coverage results
JetBrains Rider applies these filters to coverage results (coverage tree) after the coverage session is over. Use them to exclude nodes you don't want to see at the moment. Learn more
Specify runtime coverage filters in JetBrains Rider settings
Using the JetBrains Rider settings, you can specify the runtime coverage filters for a specific namespace, project, type, type member, or attribute.
As filters are applied in runtime, JetBrains Rider does not include the filtered items into the coverage snapshot and does not show them in the Unit Tests Coverage window.
Add a runtime coverage filter
Open the filter settings page in Rider settings:
.In Runtime coverage filters, click Add ... next to the required filter rule:
Analyze code only in: Specify an assembly, a class, and/or a method to be included in the coverage analysis. All other code items will be excluded. Code items are filtered independently on one another. For example, you can specify a method and leave asterisks
*
for assembly and class to include only methods with the specified name from all assemblies and all classes.Do not analyze code in: Specify a fully qualified name of an assembly, a class, and/or a method to be excluded from the coverage analysis. Code items are filtered independently on one another.
Do not analyze code marked with attributes: Specify a fully qualified attribute name of a class that will be used to exclude symbols from the coverage analysis. For example,
System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute
or*.ExcludeFromCodeCoverageAttribute
.
You can use asterisk wildcards when specifying code item names in a filter:
*
(asterisk) represents zero or more characters.For example, you can use the
*.Tests
wildcard in Do not analyze code in to exclude all test projects from code analysis.Click Save to save the filter settings and apply your modifications.
Specify runtime coverage filters in source code
Another way to specify a runtime coverage filter is by adding comments to the source code. This lets you exclude a particular parts of the code from coverage analysis.
Exclude particular code from coverage analysis
In your project, add a reference to the JetBrains.dotCover.MSBuild NuGet package. This is a development-only dependency that will not be included as a runtime dependency in your project.
Comment the code you want to exclude from analysis. The following comments are supported:
// dotcover disable var a = 1; var b = 10; // dotcover enable /* dotcover disable */ var c = 1; /* dotcover enable */ var d = 1; // dotCover disable this line // dotcover disable next line var e = 100;
Specify filters of coverage results
JetBrains Rider applies these filters to coverage results after a coverage session is over. Use these filters to:
Exclude nodes (namespaces, classes, and so on) you're currently not interested in from the Unit Tests Coverage window.
Exclude an entire file from the Unit Tests Coverage window. For example, this may be helpful if you want to exclude some auto-generated files (that is, classes and methods declared in these files) from the results.
Exclude a node (namespace, class, class member) from coverage results
Open the Unit Tests Coverage window.
Select a node and in the context menu choose:
Exclude from Coverage Results to exclude the node from the current results.
Exclude and Create Runtime Coverage Filter to exclude the node from the results and create a corresponding runtime filter that will prevent collecting coverage data for this node in future coverage sessions.
Exclude All But This to leave only the selected node in the coverage tree.
Show the excluded nodes back
Click the Show all nodes link on top of the Unit Tests Coverage window.
Exclude a file from coverage results
In Rider settings, open
.Find the Exclude file masks list.
Click to add a new exclude filter for specific file masks.
You can use Ant-style wildcards in file masks:
?
to match a single character excluding directory separators*
to match zero or more characters excluding directory separators**
to match any number of characters including directory separators/
or\
to match directory separators regardless of the OS path format
For example a pattern
**Test?\**.*
will match the following files:C:\Projects\MyTestX\data\file_one.txt
/home/projects/TestY/file_two.xml
But not:
C:\Projects\Test\data\file_one.txt
/home/projects/TestY/file_two