affaan-m/ECC

cpp-testing

C++ テストの作成/更新/修正、GoogleTest/CTest の設定、失敗またはフレーキーなテストの診断、カバレッジ/サニタイザーの追加時にのみ使用します。

73CollectingNetwork accessWrites files
See how to use itView GitHub source
npx skills add https://github.com/affaan-m/ECC --skill "docs/ja-JP/skills/cpp-testing"
Automated source guide

Source checked Jul 28, 2026·Refresh due Oct 26, 2026

Reorganized from the pinned upstream SKILL.md

Turn cpp-testing's source instructions into a guide you can follow

According to the pinned SKILL.md from affaan-m/ECC: CMake/CTest を使用した GoogleTest/GoogleMock による最新の C++(C++17/20)向けのエージェント重視のテストワークフローです。

npx skills add https://github.com/affaan-m/ECC --skill "docs/ja-JP/skills/cpp-testing"
Check the pinned source

Best fit

  • C++ テストの作成/更新/修正、GoogleTest/CTest の設定、失敗またはフレーキーなテストの診断、カバレッジ/サニタイザーの追加時にのみ使用します。

Bring this context

  • A concrete task that matches the documented purpose of cpp-testing.
  • The files, examples, or context the task depends on.
  • Your constraints, target environment, and definition of done.

Expected outputs

  • A result that follows the pinned cpp-testing instructions.
  • A concise record of assumptions, inputs used, and unresolved questions.
  • A final check against the source workflow and relevant permission signals.

Key source sections

Read cpp-testing through these 5 source sections

Sections are extracted automatically from the pinned SKILL.md and link back to the source.

01

使用タイミング

新しい C++ テストの作成または既存のテストの修正

SKILL.md · 使用タイミング
新しい C++ テストの作成または既存のテストの修正C++ コンポーネントのユニット/統合テストカバレッジの設計テストカバレッジ、CI ゲーティング、リグレッション保護の追加
02

使用すべきでない場合

テスト変更を伴わない新しい製品機能の実装

SKILL.md · 使用すべきでない場合
テスト変更を伴わない新しい製品機能の実装テストカバレッジや失敗に関連しない大規模なリファクタリング検証するテストリグレッションのないパフォーマンスチューニング
03

コア概念

TDD ループ: red → green → refactor(テスト優先、最小限の修正、その後クリーンアップ)

SKILL.md · コア概念
TDD ループ: red → green → refactor(テスト優先、最小限の修正、その後クリーンアップ)分離: グローバル状態よりも依存性注入とフェイクを優先テストレイアウト: tests/unit、tests/integration、tests/testdata
04

TDD ワークフロー

RED → GREEN → REFACTOR ループに従います:

SKILL.md · TDD ワークフロー
RED: 新しい動作をキャプチャする失敗するテストを書くGREEN: 合格する最小限の変更を実装するREFACTOR: テストがグリーンのままクリーンアップする
05

コード例

Review the “コード例” section in the pinned source before continuing.

SKILL.md · コード例
Review and apply the “コード例” source section.

SkillSignal prompt templates

Provide the task, context, and acceptance criteria

These prompts were written by SkillSignal from the source structure; they are not upstream text.

Task-start prompt

Confirm source fit, inputs, and outputs before acting.

Use cpp-testing to help me with: [specific task]. Context: [files, data, or background]. Constraints: [environment, scope, and prohibited actions]. Before acting, check the pinned SKILL.md and explain which sections apply, what inputs are still missing, and what you will deliver.

Source-guided execution

Make the Agent explicitly follow the key extracted sections.

Apply the pinned cpp-testing source to [task]. Pay particular attention to these source sections: “使用タイミング”, “使用すべきでない場合”, “コア概念”, “TDD ワークフロー”, “コード例”. Preserve the important decision at each step. Mark facts not covered by the source as “needs confirmation” instead of inventing them. Then verify the result against my acceptance criteria: [criteria].

Result-review prompt

Check omissions, permissions, and source drift before delivery.

Review the current cpp-testing result: (1) does it satisfy the original task; (2) were any applicable steps or limits in the pinned SKILL.md missed; (3) did it perform any unauthorized file, command, network, or data action; and (4) which conclusions remain unverified? List issues first, then fix only what the source or user authorization supports.

Output checklist

Verify each item before delivery

The task matches the purpose documented in the SKILL.md.

The source section “使用タイミング” has been checked.

The source section “使用すべきでない場合” has been checked.

The source section “コア概念” has been checked.

The source section “TDD ワークフロー” has been checked.

Inputs, constraints, and acceptance criteria are explicit.

Unverified facts, compatibility, and outcome claims are clearly marked.

Any file, command, network, or data action has been reviewed.

Choose a different workflow

When another Skill is the better fit

FAQ

What does cpp-testing do?

CMake/CTest を使用した GoogleTest/GoogleMock による最新の C++(C++17/20)向けのエージェント重視のテストワークフローです。

How do I start using cpp-testing?

The catalog detected this source-specific install command: npx skills add https://github.com/affaan-m/ECC --skill "docs/ja-JP/skills/cpp-testing". Inspect the command and pinned source before running it.

Which Agent platforms does it declare?

No dedicated Agent platform is declared in the pinned source record.

Repository stars
234,327
Repository forks
35,711
Quality
73/100
Source repository last pushed

Quality breakdown

Based on traceable docs and repository signals; stars are not treated as quality.

73/100
Documentation27/30
Specificity18/25
Maintenance18/20
Trust signals10/25
View original Skill.mdThis page is parsed directly from the repository SKILL.md without editorial rewriting. Collected: Jul 28, 2026 · about 1 min

C++ Testing(エージェントスキル)

CMake/CTest を使用した GoogleTest/GoogleMock による最新の C++(C++17/20)向けのエージェント重視のテストワークフローです。

使用タイミング

  • 新しい C++ テストの作成または既存のテストの修正
  • C++ コンポーネントのユニット/統合テストカバレッジの設計
  • テストカバレッジ、CI ゲーティング、リグレッション保護の追加
  • 一貫した実行のための CMake/CTest ワークフローの設定
  • テスト失敗またはフレーキーな動作の調査
  • メモリ/レース診断のためのサニタイザーの有効化

使用すべきでない場合

  • テスト変更を伴わない新しい製品機能の実装
  • テストカバレッジや失敗に関連しない大規模なリファクタリング
  • 検証するテストリグレッションのないパフォーマンスチューニング
  • C++ 以外のプロジェクトまたはテスト以外のタスク

コア概念

  • TDD ループ: red → green → refactor(テスト優先、最小限の修正、その後クリーンアップ)
  • 分離: グローバル状態よりも依存性注入とフェイクを優先
  • テストレイアウト: tests/unittests/integrationtests/testdata
  • モック vs フェイク: 相互作用にはモック、ステートフルな動作にはフェイク
  • CTest ディスカバリー: 安定したテストディスカバリーのために gtest_discover_tests() を使用
  • CI シグナル: 最初にサブセットを実行し、次に --output-on-failure でフルスイートを実行

TDD ワークフロー

RED → GREEN → REFACTOR ループに従います:

  1. RED: 新しい動作をキャプチャする失敗するテストを書く
  2. GREEN: 合格する最小限の変更を実装する
  3. REFACTOR: テストがグリーンのままクリーンアップする
// tests/add_test.cpp
#include <gtest/gtest.h>

int Add(int a, int b); // プロダクションコードによって提供されます。

TEST(AddTest, AddsTwoNumbers) { // RED
  EXPECT_EQ(Add(2, 3), 5);
}

// src/add.cpp
int Add(int a, int b) { // GREEN
  return a + b;
}

// REFACTOR: テストが合格したら簡素化/名前変更

コード例

基本的なユニットテスト(gtest)

// tests/calculator_test.cpp
#include <gtest/gtest.h>

int Add(int a, int b); // プロダクションコードによって提供されます。

TEST(CalculatorTest, AddsTwoNumbers) {
    EXPECT_EQ(Add(2, 3), 5);
}

フィクスチャ(gtest)

// tests/user_store_test.cpp
// 擬似コードスタブ: UserStore/User をプロジェクトの型に置き換えてください。
#include <gtest/gtest.h>
#include <memory>
#include <optional>
#include <string>

struct User { std::string name; };
class UserStore {
public:
    explicit UserStore(std::string /*path*/) {}
    void Seed(std::initializer_list<User> /*users*/) {}
    std::optional<User> Find(const std::string &/*name*/) { return User{"alice"}; }
};

class UserStoreTest : public ::testing::Test {
protected:
    void SetUp() override {
        store = std::make_unique<UserStore>(":memory:");
        store->Seed({{"alice"}, {"bob"}});
    }

    std::unique_ptr<UserStore> store;
};

TEST_F(UserStoreTest, FindsExistingUser) {
    auto user = store->Find("alice");
    ASSERT_TRUE(user.has_value());
    EXPECT_EQ(user->name, "alice");
}

モック(gmock)

// tests/notifier_test.cpp
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>

class Notifier {
public:
    virtual ~Notifier() = default;
    virtual void Send(const std::string &message) = 0;
};

class MockNotifier : public Notifier {
public:
    MOCK_METHOD(void, Send, (const std::string &message), (override));
};

class Service {
public:
    explicit Service(Notifier &notifier) : notifier_(notifier) {}
    void Publish(const std::string &message) { notifier_.Send(message); }

private:
    Notifier &notifier_;
};

TEST(ServiceTest, SendsNotifications) {
    MockNotifier notifier;
    Service service(notifier);

    EXPECT_CALL(notifier, Send("hello")).Times(1);
    service.Publish("hello");
}

CMake/CTest クイックスタート

# CMakeLists.txt(抜粋)
cmake_minimum_required(VERSION 3.20)
project(example LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
# プロジェクトロックされたバージョンを優先します。タグを使用する場合は、プロジェクトポリシーに従って固定されたバージョンを使用します。
set(GTEST_VERSION v1.17.0) # プロジェクトポリシーに合わせて調整します。
FetchContent_Declare(
  googletest
  URL Google Test framework (official repository) https://github.com/google/googletest/archive/refs/tags/${GTEST_VERSION}.zip
)
FetchContent_MakeAvailable(googletest)

add_executable(example_tests
  tests/calculator_test.cpp
  src/calculator.cpp
)
target_link_libraries(example_tests GTest::gtest GTest::gmock GTest::gtest_main)

enable_testing()
include(GoogleTest)
gtest_discover_tests(example_tests)
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build -j
ctest --test-dir build --output-on-failure

テストの実行

ctest --test-dir build --output-on-failure
ctest --test-dir build -R ClampTest
ctest --test-dir build -R "UserStoreTest.*" --output-on-failure
./build/example_tests --gtest_filter=ClampTest.*
./build/example_tests --gtest_filter=UserStoreTest.FindsExistingUser

失敗のデバッグ

  1. gtest フィルタで単一の失敗したテストを再実行します。
  2. 失敗したアサーションの周りにスコープ付きログを追加します。
  3. サニタイザーを有効にして再実行します。
  4. 根本原因が修正されたら、フルスイートに拡張します。

カバレッジ

グローバルフラグではなく、ターゲットレベルの設定を優先します。

option(ENABLE_COVERAGE "Enable coverage flags" OFF)

if(ENABLE_COVERAGE)
  if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
    target_compile_options(example_tests PRIVATE --coverage)
    target_link_options(example_tests PRIVATE --coverage)
  elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    target_compile_options(example_tests PRIVATE -fprofile-instr-generate -fcoverage-mapping)
    target_link_options(example_tests PRIVATE -fprofile-instr-generate)
  endif()
endif()

GCC + gcov + lcov:

cmake -S . -B build-cov -DENABLE_COVERAGE=ON
cmake --build build-cov -j
ctest --test-dir build-cov
lcov --capture --directory build-cov --output-file coverage.info
lcov --remove coverage.info '/usr/*' --output-file coverage.info
genhtml coverage.info --output-directory coverage

Clang + llvm-cov:

cmake -S . -B build-llvm -DENABLE_COVERAGE=ON -DCMAKE_CXX_COMPILER=clang++
cmake --build build-llvm -j
LLVM_PROFILE_FILE="build-llvm/default.profraw" ctest --test-dir build-llvm
llvm-profdata merge -sparse build-llvm/default.profraw -o build-llvm/default.profdata
llvm-cov report build-llvm/example_tests -instr-profile=build-llvm/default.profdata

サニタイザー

option(ENABLE_ASAN "Enable AddressSanitizer" OFF)
option(ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer" OFF)
option(ENABLE_TSAN "Enable ThreadSanitizer" OFF)

if(ENABLE_ASAN)
  add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  add_link_options(-fsanitize=address)
endif()
if(ENABLE_UBSAN)
  add_compile_options(-fsanitize=undefined -fno-omit-frame-pointer)
  add_link_options(-fsanitize=undefined)
endif()
if(ENABLE_TSAN)
  add_compile_options(-fsanitize=thread)
  add_link_options(-fsanitize=thread)
endif()

フレーキーテストのガードレール

  • 同期に sleep を使用しないでください。条件変数またはラッチを使用してください。
  • 一時ディレクトリをテストごとに一意にし、常にクリーンアップしてください。
  • ユニットテストで実際の時間、ネットワーク、ファイルシステムの依存関係を避けてください。
  • ランダム化された入力には決定論的シードを使用してください。

ベストプラクティス

すべきこと

  • テストを決定論的かつ分離されたものに保つ
  • グローバル変数よりも依存性注入を優先する
  • 前提条件には ASSERT_* を使用し、複数のチェックには EXPECT_* を使用する
  • CTest ラベルまたはディレクトリでユニットテストと統合テストを分離する
  • メモリとレース検出のために CI でサニタイザーを実行する

すべきでないこと

  • ユニットテストで実際の時間やネットワークに依存しない
  • 条件変数を使用できる場合、同期としてスリープを使用しない
  • 単純な値オブジェクトをオーバーモックしない
  • 重要でないログに脆弱な文字列マッチングを使用しない

よくある落とし穴

  • 固定一時パスの使用 → テストごとに一意の一時ディレクトリを生成し、クリーンアップします。
  • ウォールクロック時間への依存 → クロックを注入するか、偽の時間ソースを使用します。
  • フレーキーな並行性テスト → 条件変数/ラッチと境界付き待機を使用します。
  • 隠れたグローバル状態 → フィクスチャでグローバル状態をリセットするか、グローバル変数を削除します。
  • オーバーモック → ステートフルな動作にはフェイクを優先し、相互作用のみをモックします。
  • サニタイザー実行の欠落 → CI に ASan/UBSan/TSan ビルドを追加します。
  • デバッグのみのビルドでのカバレッジ → カバレッジターゲットが一貫したフラグを使用することを確認します。

オプションの付録: ファジングとプロパティテスト

プロジェクトがすでに LLVM/libFuzzer またはプロパティテストライブラリをサポートしている場合にのみ使用してください。

  • libFuzzer: 最小限の I/O で純粋関数に最適です。
  • RapidCheck: 不変条件を検証するプロパティベースのテストです。

最小限の libFuzzer ハーネス(擬似コード: ParseConfig を置き換えてください):

#include <cstddef>
#include <cstdint>
#include <string>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
    std::string input(reinterpret_cast<const char *>(data), size);
    // ParseConfig(input); // プロジェクト関数
    return 0;
}

GoogleTest の代替

  • Catch2: ヘッダーオンリー、表現力豊かなマッチャー
  • doctest: 軽量、最小限のコンパイルオーバーヘッド
Source repo
affaan-m/ECC
Skill path
docs/ja-JP/skills/cpp-testing/SKILL.md
Commit SHA
4e973d3eaf92
Repository license
MIT
Data collected