0
votes

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
There are two types of filters, runtime and coverage, according to Set up Coverage Filters It seems, that you've applied only first onePavel Anikhouski
@PavelAnikhouski: Good find, but that's not it: TestMethod2 is already not shown in the coverage window (probably because a runtime filter exists), so I can't add a coverage filter for it.Heinzi

1 Answers

0
votes

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