4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#でMSTestのカバレッジ計測をしてみる

Posted at

1. はじめに

  • C#で作成した単体テストのカバレッジを計測したい
  • Visual Studio EnterpriseだとGUIでカバレッジが取得できるが、高価になるのでフリーのツールで代替したい

2. 開発環境

  • Widnows 11
  • Visual Studio 2022 Community
  • MSTEST
  • OpenCover (Nuget)
  • ReportGenerator (Nuget)

3. 開発環境準備

  • NuGetよりインストールする

3.1. OpenCoverのインストール

image.png

3.2. ReportGeneratorのインストール

image.png

4. サンプルプログラム

4.1. Calculatorクラス (テスト対象)

  • 新規クラスを追加する
    image.png

  • 足し算をする簡単なメソッドを追加

Calculator.cs
    public class Calculator
    {
        public Calculator()
        {
            // Do Nothing
        }

        /// <summary>
        /// 足し算をする
        /// </summary>
        /// <param name="x">値1</param>
        /// <param name="y">値2</param>
        /// <returns>値1 + 値2</returns>
        public int Add(int x , int y)
        {
            return x + y;
        }
  • 念のため動作確認をする
        private void Form1_Load(object sender, EventArgs e)
        {
            // 足し算する
            var calculator = new Calculator();
            var result = calculator.Add(1, 2);

            // デバッグ用
            Debug.Print(result.ToString());
        }

4.2. テストクラス作成

  • テスト対象のメソッドを選択して、右クリックから単体テストの作成を選択する
    image.png

  • OKボタンをクリックする
    image.png

  • 足し算を確認する処理を追加する

    [TestClass()]
    public class CalculatorTests
    {
        [TestMethod()]
        [TestCategory("UnitTest")]
        public void AddTest()
        {
            // 期待値
            int expected = 3;

            // 実行結果
            var calculator = new Calculator();
            int actual = calculator.Add(1, 2);

            // テスト検証
            Assert.AreEqual(expected, actual);
        }
    }
  • 単体テストを実行して成功になることを確認する
    image.png

5. カバレッジ取得

  • OpenCover, ReportGeneratorを使用する場合、GUI上では実施できないためバッチファイルを作成する
  • 実行結果はHTMLへ出力してブラウザ上で確認できるようにする

5.1. バッチファイル作成

  • 今回ソリューションフォルダの直下に作成した
@echo off
rem OpenCoverのインストール先
SET OPEN_COVER=%USERPROFILE%\.nuget\packages\opencover\4.7.1221\tools

rem Report生成ツールのインストール先
SET REPORT_GEN=%USERPROFILE%\.nuget\packages\reportgenerator\5.1.18\tools\net6.0

rem テストフレームワークのインストール先
SET MS_TEST=%ProgramFiles%\Microsoft Visual Studio\2022\Community

rem ターゲットアセンブリ (テストクラスがあるDLLファイル) 
SET TARGET_TEST=WinFormsApp2Tests.dll

rem ターゲットアセンブリの格納先 (テストクラスがある場所)
SET TARGET_TEST_DIR=C:\Users\user\source\repos\WinFormsApp2\WinFormsApp2Tests\bin\Debug\net6.0-windows

REM カバレッジ計測対象 (テスト対象クラスのNAMESPACE)
SET FILTERS="+[WinFormsApp2]*"

REM パスの設定
SET PATH=%PATH%;%OPEN_COVER%;%MS_TEST%;%REPORT_GEN%

REM OpenCoverを実行 (./CoverageReportフォルダへ結果出力)
OpenCover.Console -register:user -target:"%MS_TEST%\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" -targetargs:"%TARGET_TEST%" -targetdir:"%TARGET_TEST_DIR%" -filter:"%FILTERS%" -output:".\CoverageReport\result.xml"

REM ReportGeneratorrを実行 (./CoverageReportフォルダへ結果出力)
ReportGenerator -reports:".\CoverageReport\result.xml" -reporttypes:Html -targetdir:".\CoverageReport"

pause

5.2. バッチファイル実行

  • Visual Studioでビルドした後にバッチファイルを実行する
  • 初回は原因不明のエラーが出たが、2回目からは正常終了した
    image.png

5.3. ReportGeneratorのレポート確認

  • ブラウザでカバレッジを確認して想定通り出力されていることを確認した
  • staticのクラスの確認方法はまだ対応方法を検討中
    image.png

image.png

6. 参考文献

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?