cpp-testing

Write, run, and debug C++ tests using GoogleTest, CMake, and CTest.

Updated Apr 18, 2026
One-click install
npx skills add https://github.com/JohnRebellion/.claude-public --skill cpp-testing-johnrebellion
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: cpp-testing
Source: https://github.com/JohnRebellion/.claude-public/tree/main/claude/skills/cpp-testing
Command: npx skills add https://github.com/JohnRebellion/.claude-public --skill cpp-testing-johnrebellion

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? C++ test suites often suffer from flaky tests, inconsistent CMake/CTest configuration, missing coverage, and undetected memory or threading bugs. This Skill provides a structured workflow for writing, running, and hardening C++ tests with GoogleTest and GoogleMock. ## Core Features & Use Cases - TDD Workflow Guidance: Follows the red-green-refactor loop with concrete GoogleTest fixture and gmock mock examples for C++17/20 code. - CMake/CTest Configuration: Sets up FetchContent-based GoogleTest integration, gtest_discover_tests() discovery, coverage flags (gcov/lcov, llvm-cov), and ASan/UBSan/TSan sanitizer builds. - Flaky Test Diagnosis: Provides guardrails against common pitfalls like fixed temp paths, wall-clock dependencies, sleep-based synchronization, and hidden global state. - Use Case: A developer inherits a C++ project with failing tests and no coverage. Use this Skill to configure CTest discovery, isolate the failing test with gtest filters, enable AddressSanitizer to find the memory bug, and add coverage reporting to CI. ## Quick Start Ask the agent to write a GoogleTest unit test with a CMake/CTest setup for your C++ class, or to diagnose a specific failing or flaky test.

Frequently Asked Questions about cpp-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up GoogleTest with CMake and CTest?▼

Use FetchContent to declare googletest with a pinned version tag, link your test executable against GTest::gtest, GTest::gmock, and GTest::gtest_main, then call gtest_discover_tests() after enable_testing(). Run tests with ctest --test-dir build --output-on-failure.

How do I run a single GoogleTest test case?▼

Run a single test with ctest using ctest --test-dir build -R TestName, or invoke the test binary directly with --gtest_filter=TestSuite.TestName. This isolates failures before expanding to the full suite.

GoogleTest vs Catch2 vs doctest for C++ testing?▼

GoogleTest with GoogleMock provides full mocking support and mature CTest integration via gtest_discover_tests(). Catch2 is header-only with expressive matchers, while doctest minimizes compile overhead. Choose based on mocking needs and build-time constraints.

How do I enable AddressSanitizer in a CMake C++ project?▼

Add an ENABLE_ASAN option that appends -fsanitize=address -fno-omit-frame-pointer to compile options and -fsanitize=address to link options. The same pattern applies to UBSan and TSan, which should run in CI for memory and race detection.

Why are my C++ tests flaky and how do I fix them?▼

Flaky tests usually come from sleep-based synchronization, fixed temp paths, wall-clock dependencies, or hidden global state. Replace sleeps with condition variables or latches, generate unique temp directories per test, inject clocks, and reset global state in fixtures.

When should I use mocks versus fakes in C++ tests?▼

Use gmock mocks to verify interactions, such as expecting a Send call exactly once. Use fakes for stateful behavior where the test needs working logic rather than interaction assertions. Avoid over-mocking simple value objects.