microsoft / microsoft/codecoverage

Feature request: Code Coverage exclusion on line level

Open
#23 6 comments 2 reactions 1 assignee View on GitHub

@jakubch1 is already working on this.

Since Feb 11, 2022.

enhancement
Dominant language
C#
Stars
125
Forks
17
Avg merge
1h 17m
Merged PRs (30d)
2

Description

Description

This one is a tricky one to find the correct place to report, I have tried to look around in different repos that has issues that mentions ExcludeFromCodeCoverage (e.g. microsoft/vstest#2658 and dotnet/runtime#45374 ), but it seems that the responsibility for this one is a bit vague since the implementation (dotnet/runtime) and usage of this attribute (microsoft/vstest + 3rd party vendors) is assigned to different projects. But anyway, here is a description and you can choose how this should be taken further.

Today we have the ExcludeFromCodeCoverage attribute which is by Visual Studio Code Coverage (and other 3rd party vendors) used to exclude code on different levels of code like class, methods, properties and so on.

However, there is one situation where an attribute cannot be used, but I think that it should be some way to do this at that level as well. And that is on code line level.

The problem arises if you for example has a switch where the default statement is never hit (and it impossible to come to that location otherwise).

For example, you have a method that looks like this:

private static bool SomeFunc(AnEnum anEnum)
{
    switch(anEnum)
    {
        case AnEnum.Val1:
            // something
            return true;
        case AnEnum.Val2
            // something else
            return false;
        default:
            // This line is never hit since it is stopped by other code, it's only here because a default is needed
            // due to return statement in function
            throw new ArgumentOutOfRangeException(nameof(anEnum), anEnum, $"The value {anEnum} is not supported.");
    }
}

Since this is private method and the calling method already prohibits anEnum values (due to other logic), no tests can hit the default clause. So, it would be nice to exclude that from code coverage. It's simply there for the events when someone screws up something and causes havoc (and the return statement requires some kind of return that won't be hit otherwise a compile error is issued).

I am not sure how this could be solved. My first thought was preprocessor declarations like eg #skipcodecoverage enable #skipcodecoverage disable, but I'm not sure if code coverage can get these preprocessing directives. Other could be that we on ExcludeFromCodeCoverage could set attributes that excludes certain things e.g. [ExcludeFromCodeCoverage(IgnoreSwitchDefault = true)] or [ExcludefromCodeCoverage(IgnoreExceptions=typeof(ArgumentOutOfRangeException)]

Without this possibility it's difficult to get a clean code coverage and you always checks "why did not the code coverage returned % which I'm expecting"..

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.