agentleFS
Premchand006Premchand006/AI-assistant-skills/skills/cpp-testing/SKILL.md

cpp-testing

Skill0 starsChanged 51 days agoMITView on GitHub

Test-driven development and unit testing for modern C++ using GoogleTest, GoogleMock, and CMake CTest. Covers test layout, fixture lifecycle, fakes versus mocks, parameterized tests, test discovery, and sanitizer configurations (ASan, UBSan, TSan). Use when writing, updating, or fixing C++ tests, configuring CTest or GoogleTest, investigating flaky test behavior, or setting up memory sanitizers for C++ test suites.

18 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

7 rules. 0 are common, 4 are distinctive, and 3 are very specific to this project.

  • 0

    Common

    Most of the 17 edited versions we read keep these.

  • 4

    Distinctive

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Red-Green-Refactor loop: Write the failing assertion first, implement the minimum code to turn it green, then refactor with test …

    line 20
  2. Distinctive:

    Isolation over integration: Prefer fakes and explicit dependency injection rather than relying on shared global state.

    line 21
  3. Very specific:

    Structured directory layout: Keep tests mirrored against source under tests/unit/, tests/integration/, and tests/fixtures/.

    line 22
  4. Very specific:

    Dynamic discovery: Use gtest_discover_tests() in CMake so CTest registers tests automatically without manual listing.

    line 23
  5. Very specific:

    Prefer EXPECT_* for non-fatal checks that allow remaining assertions in the test to execute, and ASSERT_* when failure invalidates …

    line 27
  6. Distinctive:

    When verifying mocked interactions with GoogleMock:

    line 42
  7. Distinctive:

    Run tests under sanitizers by enabling compiler flags during configuration:

    line 54

“Common” is measured: kept by at least 6 in 10 of the 17 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

C++ Testing with GoogleTest and CTest

Flaky tests, silent memory corruption in test fixtures, and slow test discovery degrade confidence in test suites. Establishing a deterministic test harness with fast discovery and active sanitizers catches memory leaks and race conditions before code merges.

ctest --test-dir build --output-on-failure -j4

Detailed test fixture syntax, parameterized test suites, and CMake integration patterns are documented in references/gtest-patterns.md.

Core Concepts

  1. Red-Green-Refactor loop: Write the failing assertion first, implement the minimum code to turn it green, then refactor with test verification.
  2. Isolation over integration: Prefer fakes and explicit dependency injection rather than relying on shared global state.
  3. Structured directory layout: Keep tests mirrored against source under tests/unit/, tests/integration/, and tests/fixtures/.
  4. Dynamic discovery: Use gtest_discover_tests() in CMake so CTest registers tests automatically without manual listing.

Writing Deterministic Tests

Prefer EXPECT_* for non-fatal checks that allow remaining assertions in the test to execute, and ASSERT_* when failure invalidates subsequent logic (such as checking pointer nullity before dereferencing).

#include <gtest/gtest.h>

TEST(RingBufferTest, OverwriteAdvancesTail) {
    RingBuffer<int, 4> buffer;
    for (int i = 0; i < 5; ++i) {
        buffer.push(i);
    }
    EXPECT_EQ(buffer.size(), 4u);
    EXPECT_EQ(buffer.pop(), 1);
}

When verifying mocked interactions with GoogleMock:

class MockHardwareBus : public HardwareBus {
public:
    MOCK_METHOD(bool, WriteRegister, (uint16_t reg, uint8_t val), (override));
    MOCK_METHOD(uint8_t, ReadRegister, (uint16_t reg), (override));
};

Diagnosing Memory and Concurrency Bugs

Run tests under sanitizers by enabling compiler flags during configuration:

  • AddressSanitizer (-fsanitize=address): Catches out-of-bounds accesses, use-after-free, and stack corruption.
  • UndefinedBehaviorSanitizer (-fsanitize=undefined): Catches integer overflows, null pointer dereferences, and alignment violations.
  • ThreadSanitizer (-fsanitize=thread): Detects data races in multithreaded test cases. Do not combine TSan with ASan in the same binary.

Honest limits

Unit tests demonstrate the presence of specific verified behaviors, but cannot prove the absence of subtle hardware interactions, cache coherency anomalies, or platform-specific driver timing errors. Sanitizers introduce CPU and memory overhead, meaning timing-sensitive bugs in real-time loops may not manifest during sanitized test runs.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.