cpp-testing

仅在创建/更新/修复 C++ 测试、配置 GoogleTest/CTest、诊断失败或不稳定的测试、以及添加覆盖率或消毒器时使用。

351 stars

Best use case

cpp-testing is best used when you need a repeatable AI agent workflow instead of a one-off prompt. It is especially useful for teams working in multi. 仅在创建/更新/修复 C++ 测试、配置 GoogleTest/CTest、诊断失败或不稳定的测试、以及添加覆盖率或消毒器时使用。

仅在创建/更新/修复 C++ 测试、配置 GoogleTest/CTest、诊断失败或不稳定的测试、以及添加覆盖率或消毒器时使用。

Users should expect a more consistent workflow output, faster repeated execution, and less time spent rewriting prompts from scratch.

Practical example

Example input

Use the "cpp-testing" skill to help with this workflow task. Context: 仅在创建/更新/修复 C++ 测试、配置 GoogleTest/CTest、诊断失败或不稳定的测试、以及添加覆盖率或消毒器时使用。

Example output

A structured workflow result with clearer steps, more consistent formatting, and an output that is easier to reuse in the next run.

When to use this skill

  • Use this skill when you want a reusable workflow rather than writing the same prompt again and again.

When not to use this skill

  • Do not use this when you only need a one-off answer and do not need a reusable workflow.
  • Do not use it if you cannot install or maintain the related files, repository context, or supporting tools.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/cpp-testing/SKILL.md --create-dirs "https://raw.githubusercontent.com/xu-xiang/everything-claude-code-zh/main/docs/ja-JP/skills/cpp-testing/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/cpp-testing/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How cpp-testing Compares

Feature / Agentcpp-testingStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

仅在创建/更新/修复 C++ 测试、配置 GoogleTest/CTest、诊断失败或不稳定的测试、以及添加覆盖率或消毒器时使用。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

# C++ Testing(智能体技能)

这是一个基于 CMake/CTest 和 GoogleTest/GoogleMock 的、面向智能体(Agent)的现代 C++(C++17/20)测试工作流(Workflow)。

## 使用场景

- 创建新的 C++ 测试或修改现有测试
- 为 C++ 组件设计单元测试/集成测试覆盖率
- 添加测试覆盖率、CI 门禁和回归保护
- 配置 CMake/CTest 工作流以实现一致的执行
- 调查测试失败或不稳定(Flaky)的行为
- 启用消毒器(Sanitizers)进行内存/竞态诊断

### 不建议使用的场景

- 实现不涉及测试更改的新产品功能
- 与测试覆盖率或失败无关的大规模重构
- 没有需要验证的测试回归的性能调优
- 非 C++ 项目或非测试任务

## 核心概念

- **TDD 循环**: 红(Red) → 绿(Green) → 重构(Refactor)(测试先行,最小化修复,然后清理代码)
- **解耦**: 优先使用依赖注入(Dependency Injection)和伪造对象(Fakes),而非全局状态
- **测试布局**: `tests/unit`、`tests/integration`、`tests/testdata`
- **模拟 (Mock) vs 伪造 (Fake)**: 交互验证使用 Mock,有状态的行为使用 Fake
- **CTest 发现**: 使用 `gtest_discover_tests()` 以实现稳定的测试发现
- **CI 信号**: 先运行子集,然后使用 `--output-on-failure` 运行全量测试套件

## TDD 工作流

遵循 RED → GREEN → REFACTOR 循环:

1. **RED**: 编写一个失败的测试来捕获新行为
2. **GREEN**: 实现最小化的更改使测试通过
3. **REFACTOR**: 在保持测试为绿色的前提下清理代码

```cpp
// 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)

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

int Add(int a, int b); // 由生产代码提供

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

### 测试固件 (Fixture - gtest)

```cpp
// 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");
}
```

### 模拟对象 (Mock - gmock)

```cpp
// 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 快速上手

```cmake
# 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 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)
```

```bash
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build -j
ctest --test-dir build --output-on-failure
```

## 运行测试

```bash
ctest --test-dir build --output-on-failure
ctest --test-dir build -R ClampTest
ctest --test-dir build -R "UserStoreTest.*" --output-on-failure
```

```bash
./build/example_tests --gtest_filter=ClampTest.*
./build/example_tests --gtest_filter=UserStoreTest.FindsExistingUser
```

## 调试失败

1. 使用 gtest 过滤器重新运行单个失败的测试。
2. 在失败的断言周围添加作用域日志(Scoped logging)。
3. 启用消毒器(Sanitizers)后重新运行。
4. 根本原因修复后,扩展到全量测试套件。

## 覆盖率 (Coverage)

优先使用目标级(Target-level)配置,而非全局标志。

```cmake
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:

```bash
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:

```bash
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
```

## 消毒器 (Sanitizers)

```cmake
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()
```

## 不稳定测试 (Flaky Test) 的防护栏

- 不要使用 `sleep` 进行同步,应使用条件变量(Condition variables)或门闩(Latches)。
- 确保临时目录对每个测试都是唯一的,并且始终在测试后清理。
- 在单元测试中避免依赖真实的时间、网络和文件系统。
- 为随机化输入使用确定的种子(Deterministic seeds)。

## 最佳实践

### 推荐做法

- 保持测试是确定性的且相互隔离的
- 优先使用依赖注入而非全局变量
- 前置条件使用 `ASSERT_*`,多个检查项使用 `EXPECT_*`
- 通过 CTest 标签(Labels)或目录结构分离单元测试和集成测试
- 在 CI 中运行 Sanitizer 以检测内存问题和竞态条件

### 禁忌做法

- 单元测试不要依赖真实的时间或网络
- 如果可以使用条件变量,不要使用 sleep 进行同步
- 不要对简单的值对象(Value objects)过度模拟(Over-mocking)
- 不要对非关键日志使用脆弱的字符串匹配

### 常见陷阱

- **使用固定的临时路径** → 为每个测试生成唯一的临时目录并进行清理。
- **依赖墙上时钟时间** → 注入时钟源或使用伪造的时间源。
- **不稳定的并发测试** → 使用条件变量/门闩和带超时的等待。
- **隐蔽的全局状态** → 在测试固件中重置全局状态,或移除全局变量。
- **过度模拟 (Over-mocking)** → 优先使用 Fake 处理有状态行为,仅对交互进行 Mock。
- **缺失 Sanitizer 运行** → 在 CI 中添加 ASan/UBSan/TSan 构建。
- **仅在 Debug 构建中统计覆盖率** → 确保覆盖率目标使用的是一致的标志。

## 可选附录:模糊测试与属性测试

仅当项目已经支持 LLVM/libFuzzer 或属性测试库时使用。

- **libFuzzer**: 最适合 I/O 极少的纯函数。
- **RapidCheck**: 验证不变性(Invariants)的基于属性的测试。

最小化 libFuzzer 挂钩示例(伪代码:请替换 ParseConfig):

```cpp
#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**: 仅头文件(Header-only),具有表现力的匹配器
- **doctest**: 轻量级,编译开销极小

Related Skills

swift-protocol-di-testing

351
from xu-xiang/everything-claude-code-zh

基于协议的依赖注入,用于可测试的Swift代码——使用聚焦协议和Swift Testing模拟文件系统、网络和外部API。

python-testing

351
from xu-xiang/everything-claude-code-zh

使用 pytest、TDD 方法论、固件(Fixtures)、模拟(Mocking)、参数化及覆盖率要求的 Python 测试策略。

golang-testing

351
from xu-xiang/everything-claude-code-zh

保证测试驱动开发与 Go 代码高质量的全面测试策略。

e2e-testing

351
from xu-xiang/everything-claude-code-zh

Playwright E2E 测试模式、页面对象模型(POM)、配置、CI/CD 集成、产物管理以及不稳定测试(flaky test)策略。

plankton-code-quality

351
from xu-xiang/everything-claude-code-zh

使用 Plankton 实现编写时代码质量强制执行 —— 通过钩子在每次文件编辑时进行自动格式化、代码检查,并由 Claude 驱动自动修复。

autonomous-loops

351
from xu-xiang/everything-claude-code-zh

自主运行 Claude Code 循环的模式与架构 —— 从简单的顺序流水线到 RFC 驱动的多智能体 DAG 系统。

visa-doc-translate

351
from xu-xiang/everything-claude-code-zh

将签证申请文件(图片)翻译成英文,并创建包含原文和译文的双语PDF

swiftui-patterns

351
from xu-xiang/everything-claude-code-zh

SwiftUI 架构模式,使用 @Observable 进行状态管理,视图组合,导航,性能优化,以及现代 iOS/macOS UI 最佳实践。

swift-concurrency-6-2

351
from xu-xiang/everything-claude-code-zh

Swift 6.2 可接近的并发性 — 默认单线程,@concurrent 用于显式后台卸载,隔离一致性用于主 actor 类型。

swift-actor-persistence

351
from xu-xiang/everything-claude-code-zh

在 Swift 中使用 actor 实现线程安全的数据持久化——基于内存缓存与文件支持的存储,通过设计消除数据竞争。

skill-stocktake

351
from xu-xiang/everything-claude-code-zh

用于审计Claude技能和命令的质量。支持快速扫描(仅变更技能)和全面盘点模式,采用顺序子代理批量评估。

search-first

351
from xu-xiang/everything-claude-code-zh

研究优先于编码的工作流程。在编写自定义代码之前,搜索现有的工具、库和模式。调用研究员代理。