How do I ignore a unit test during dotCover coverage analysis?

Consider the following repro example (MS Test unit test code):

using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            SharedMethod();
        }

        [TestMethod, ExcludeFromCodeCoverage]
        public void TestMethod2()
        {
            SharedMethod();
            // This is a performance test which will fail during coverage analysis.
            Assert.Fail();
        }

        private void SharedMethod()
        {
            // Should be green
            if (true) ; // NOOP
        }
    }
}

TestMethod2 is a performance test method, which will succeed during "regular" unit test but fail during code coverage analysis (due to the additional overhead). That's fine. I just want that method to be ignored during code coverage analysis, so that my SharedMethod is shown as "green" (because it's successfully covered by TestMethod1.

According to JetBrains, coverage filters should do that, and, yes, there is a filter active for ExcludeFromCodeCoverageAttribute:

screenshot of coverage filters

However, running Extensions/ReSharper/Unit Tests/"Cover All Tests From Solution" still considers my excluded test when coloring my code:

screenshot of coverage test result

What did I do wrong? And how do I (easily) exclude my performance tests from Code Coverage analysis?

1 answer

  • answered 2020-03-31 11:45 Heinzi

    (This is a self-answer based on a workaround I found. Feel free to add your own answer, if you found an actual solution for this issue.)

    Apparently, dotCover's coverage filters only affect the coverage statistics shown in the Unit Test Coverage window (% of code covered), not the "code coloring".

    As a workaround, you can generally exclude tests of certain categories from ReSharper's test runner. To do that, you first add a category to all tests you want to ignore:

    [TestMethod, ExcludeFromCodeCoverage, TestCategory("Performance")]
    public void TestMethod2()
    {
        SharedMethod();
        // This is a performance test which will fail during coverage analysis.
        Assert.Fail();
    }
    

    and then you add that category in Extensions/ReSharper/Options/Tools/Unit Testing/General/"Skip tests from categories":

    screenshot

    Your test will still run when started via the regular Visual Studio test runner (e.g. Test/Run All Tests), but it will be ignored by ReSharper and its tools.