Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Feature request: Code Coverage exclusion on line level

オープン
#23 コメント 6 件 リアクション 2 件 担当者 1 名 GitHub で見る

@jakubch1 がすでに取り組んでいます。

2022年2月11日 から。

評価

この issue はまだ評価されていません。

説明

enhancement

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

主要言語
C#
スター
125
フォーク
17
平均マージ
1時間 15分
マージ済み PR(30日)
2

環境構築

このプロジェクトの環境構築ファイルはまだ確認していません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

microsoft/codecoverage のほかの issue

microsoft/codecoverage の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。