
Cpp Testing
Ship and harden modern C++ codebases with GoogleTest, CMake/CTest, and sanitizer-backed regression checks.
Install
npx skills add https://github.com/affaan-m/everything-claude-code --skill cpp-testingWhat is this skill?
- RED → GREEN → REFACTOR TDD loop tuned for C++17/20 agents
- GoogleTest/GoogleMock with CMake `gtest_discover_tests()` and CTest workflows
- Recommended layout: `tests/unit`, `tests/integration`, `tests/testdata`
- Mocks vs fakes guidance and dependency-injection for isolation
- Sanitizers and `--output-on-failure` CI patterns for regressions and flakes
Adoption & trust: 4.8k installs on skills.sh; 210k GitHub stars; 3/3 security scanners passed (skills.sh audits).
Recommended Skills
Agent Browservercel-labs/open-agents
Tddmattpocock/skills
Use My Browserxixu-me/skills
Test Driven Developmentobra/superpowers
Verification Before Completionobra/superpowers
Webapp Testinganthropics/skills
Journey fit
Primary fit
Testing workflow lives in Ship because it gates releases through unit/integration coverage, CI signals, and failure diagnosis—not feature implementation. Canonical shelf is testing: TDD loops, flaky-test investigation, and CTest discovery are all quality-gating activities before or during release.
Common Questions / FAQ
Is Cpp Testing safe to install?
skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.
SKILL.md
READMESKILL.md - Cpp Testing
# C++ Testing (Agent Skill) Agent-focused testing workflow for modern C++ (C++17/20) using GoogleTest/GoogleMock with CMake/CTest. ## When to Use - Writing new C++ tests or fixing existing tests - Designing unit/integration test coverage for C++ components - Adding test coverage, CI gating, or regression protection - Configuring CMake/CTest workflows for consistent execution - Investigating test failures or flaky behavior - Enabling sanitizers for memory/race diagnostics ### When NOT to Use - Implementing new product features without test changes - Large-scale refactors unrelated to test coverage or failures - Performance tuning without test regressions to validate - Non-C++ projects or non-test tasks ## Core Concepts - **TDD loop**: red → green → refactor (tests first, minimal fix, then cleanups). - **Isolation**: prefer dependency injection and fakes over global state. - **Test layout**: `tests/unit`, `tests/integration`, `tests/testdata`. - **Mocks vs fakes**: mock for interactions, fake for stateful behavior. - **CTest discovery**: use `gtest_discover_tests()` for stable test discovery. - **CI signal**: run subset first, then full suite with `--output-on-failure`. ## TDD Workflow Follow the RED → GREEN → REFACTOR loop: 1. **RED**: write a failing test that captures the new behavior 2. **GREEN**: implement the smallest change to pass 3. **REFACTOR**: clean up while tests stay green ```cpp // tests/add_test.cpp #include <gtest/gtest.h> int Add(int a, int b); // Provided by production code. TEST(AddTest, AddsTwoNumbers) { // RED EXPECT_EQ(Add(2, 3), 5); } // src/add.cpp int Add(int a, int b) { // GREEN return a + b; } // REFACTOR: simplify/rename once tests pass ``` ## Code Examples ### Basic Unit Test (gtest) ```cpp // tests/calculator_test.cpp #include <gtest/gtest.h> int Add(int a, int b); // Provided by production code. TEST(CalculatorTest, AddsTwoNumbers) { EXPECT_EQ(Add(2, 3), 5); } ``` ### Fixture (gtest) ```cpp // tests/user_store_test.cpp // Pseudocode stub: replace UserStore/User with project types. #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 ¬ifier) : notifier_(notifier) {} void Publish(const std::string &message) { notifier_.Send(message); } private: Notifier ¬ifier_; }; TEST(ServiceTest, SendsNotifications) { MockNotifier notifier; Service service(notifier); EXPECT_CALL(notifier, Send("hello")).Times(1); service.Publish("hello"); } ``` ### CMake/CTest Quickstart ```cmake # CMakeLists.txt (excerpt) cmake_minimum_required(VERSION 3.20) project(example LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) include(FetchContent) # Prefer project-locked ver