JetBrains Rider 2022.1 Help

Set up coverage filters

Sometimes, you may need to limit the scope of coverage analysis. For example, this makes sense if you participate in development of 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 of those that are related to a particular feature. Another example is reducing the "noise" of a coverage tree by excluding the nodes (namespaces, classes, 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

  1. Open the filter settings page in Rider settings: Build, Execution, Deployment| dotCover | Filtering.

  2. In Runtime coverage filters, click Add filter 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 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 the fully qualified attribute class name 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.

  3. 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

  1. 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.

  2. 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 a node (namespace, class, class member) from coverage results

  1. Open the Unit Tests Coverage window.

  2. 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.

    JetBrains Rider: Exclude and create coverage filter

Show the excluded nodes back

Exclude a file from coverage results

  1. In Rider settings, open Build, Execution, Deployment | dotCover | Filtering.

  2. Find the Exclude file masks list.

  3. Click Add filter 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

Last modified: 22 July 2022