cpp-testing

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

Updated Mar 25, 2026
One-click install
npx skills add https://github.com/Femad-6/my-skills --skill cpp-testing-femad-6
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cpp-testing
Source: https://github.com/Femad-6/my-skills/tree/main/.github/skills/cpp-testing
Command: npx skills add https://github.com/Femad-6/my-skills --skill cpp-testing-femad-6

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing reliable C++ tests is hard: flaky tests, misconfigured CMake/CTest setups, missing coverage, and undetected memory or threading bugs slow down development. This Skill provides a structured TDD workflow and ready-to-use configurations for GoogleTest/GoogleMock with CMake and CTest. ## Core Features & Use Cases - TDD Workflow Guidance: Follows the red-green-refactor loop with concrete gtest, fixture, and gmock code examples. - Build & Test Configuration: Provides CMake snippets for FetchContent-based GoogleTest integration, gtest_discover_tests, coverage flags (GCC/lcov and Clang/llvm-cov), and ASan/UBSan/TSan sanitizers. - Flaky Test Diagnosis: Offers guardrails against common pitfalls like sleeps, fixed temp paths, wall-clock dependencies, and hidden global state. - Use Case: You inherit a C++ project with failing tests. Use this Skill to isolate the failing test with gtest filters, re-run under AddressSanitizer to find a memory bug, then add a regression test and wire coverage reporting into CI. ## Quick Start Ask the assistant 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 CMake FetchContent to pull a pinned googletest release, 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 the test binary directly with --gtest_filter, for example ./build/example_tests --gtest_filter=UserStoreTest.FindsExistingUser. Alternatively use ctest with -R to match a test name pattern.

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 constraints.

How do I enable code coverage for C++ tests?

For GCC, add --coverage compile and link options, then capture results with lcov and render HTML via genhtml. For Clang, use -fprofile-instr-generate -fcoverage-mapping and report with llvm-profdata and llvm-cov.

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

Flakiness usually comes from sleeps used for 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 sanitizers in C++ testing?

Enable AddressSanitizer and UndefinedBehaviorSanitizer in CI builds to catch memory errors and undefined behavior, and ThreadSanitizer for race detection. Add them via -fsanitize compile and link options behind CMake options like ENABLE_ASAN.