0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C++】Visual Studioを用いてGoogleTestでコードカバレッジを確認する

Last updated at Posted at 2025-03-25

1. 概要

GoogleTestを用いて単体試験など行った際に、コードカバレッジがどれくらい取れているか確認する方法を紹介します。

2. 手順

①Visual StudioでGoogleTestのプロジェクトを起動します。

②テスト対象のコードを記述
今回は以下のような、入力によって分岐するだけの処理を使います。

bool Coverage(int val) {
	switch (val) {
	case 0: {
		puts("0");
	}break;
	case 1: {
		puts("1");
	}break;
	case 2: {
		puts("2");
	}break;
	case 3: {
		puts("3");
	}break;
	case 4: {
		puts("4");
	}break;
	case 5: {
		puts("5");
	}break;
	case 6: {
		puts("6");
	}break;
	case 7: {
		puts("7");
	}break;
	case 8: {
		puts("8");
	}break;
	case 9: {
		puts("9");
	}break;
	default: {
		return false;
	}break;
	}

	return true;
}

③テストコードの記述
以下のテストコードで、カバレッジを確認してみます。

TEST(coverage, sample) {
	EXPECT_TRUE(Coverage(0));
	EXPECT_TRUE(Coverage(2));
	EXPECT_TRUE(Coverage(3));
	EXPECT_TRUE(Coverage(8));
}

④コードカバレッジの確認
「メニューバー->テスト->すべてのテストのコードカバレッジを分析」を選択します。
スクリーンショット 2025-03-25 161408.png

出力タブに結果が表示されます。検索バーで対象の関数を検索すると、カバー済みの行数や未カバーの行数を確認できます。
スクリーンショット 2025-03-25 162602.png

⑤色分け表示
出力タブにて「コードカバレッジの色分け表示」を選択します。
スクリーンショット 2025-03-25 162653.png

カバーされている行を目視で確認できます。
スクリーンショット 2025-03-25 162748.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?