TsukiNi22/ai-utils / skills/tests/cpp-tests
cpp-tests
UnverifiedC++ unit tests (GoogleTest, libutils way)
<name> (a skill) = the folder of that skill: ~/.claude/skills/<name> (setup.sh) or ${CLAUDE_SKILL_DIR}/../../*/<name> (plugin of the marketplace).
Reference: ~/personal_delivery/cpp/libutils/tests/ (whole library) and ~/personal_delivery/cpp/context-forge/tests/
(application: parent sources reused, mocks, plugin path). SKILL_DIR = directory of this file.
Code style of the tests: cpp-style + cpp-comments, file header with cpp-class/scripts/header.py.
1. Setup (once per project)
tests/CMakeLists.txtfromcmake-style/templates/tests/CMakeLists.txt:set(TARGET unit_tests),CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}(binary at the root),find_package(GTest REQUIRED), explicitSRCgrouped like the tree,include(GoogleTest)+gtest_discover_tests,target_compile_options(... -g -ggdb3).- library: link the library target (
target_link_libraries(${TARGET} PRIVATE <lib> GTest::gtest GTest::gtest_main)); - executable: reuse the parent
SRCwithoutmain.cpp(list(REMOVE_ITEM ...)+list(TRANSFORM ... PREPEND ${CMAKE_SOURCE_DIR}/)), same includes/links/add_dependencies(exception header) as the main target; - runtime paths needed by the tests (plugins, data):
target_compile_definitions(${TARGET} PRIVATE TESTS_X_DIR="${CMAKE_SOURCE_DIR}/x")and#errorin the test if it is missing.
- library: link the library target (
- Root CMake:
option(BUILD_TESTS "Build unit tests" OFF)+if(BUILD_TESTS) enable_testing() add_subdirectory(tests) endif(). .gitignore:python3 <cpp-project>/scripts/update_gitignore.py .(adds/unit_tests).- A project of cpp_project_template already has
tests/CMakeLists.txtand its first test: the unit tests link the library target (<name>_lib) when it exists, else build the shared sources of the parent (SRC_SHARED, nevermain.cpp); cross compiled (MinGW): GTest from the sysroot or fetched from its sources (FetchContent), the tests listed whenctestruns (DISCOVERY_MODE PRE_TEST, underwine),-staticlink of the.exe. - CI: the
unit-tests.ymlof cpp_project_template (Linuxunit-testsimage, Windowsmingw64-unit-testsunderwine; Debug +BUILD_TESTS=ON, checks./unit_tests,ctest --output-on-failure). - Helpers shared by the tests in
tests/tools/(namespacetests::tools):templates/tools/TempDir.hpp(temporary directory +ScopedEnvfor environment variables),templates/tools/StepSynchronizer.hpp(order the steps of several threads), mockstests/tools/Mock<Interface>.hpp.
2. Plan: test the whole project
python3 SKILL_DIR/scripts/untested.py <root> [--methods] [--all]
Inventory of the public classes / structs / enums / free functions (and methods) of include/, per module, with the
names never referenced by tests/. It is an inventory, not a coverage metric: read the headers before writing tests.
Then one test file per module, mirroring the tree: include/utils/system/Scheduler.hpp -> tests/system/Scheduler.cpp
(a file can group close classes of a module: Scheduler.cpp covers Scheduler, LoadBalancer, IdHandler).
For a big project, list the files to create and the main cases per class to the user before writing them.
3. Write the tests (templates/Module.cpp)
- Header:
File Description: ## Unit tests of the <Class>, the <Other> & ...; includes: the project umbrella ("utils.hpp") or the header under test,<gtest/gtest.h>, the STL. - One block per class:
/* ------------------------------- Scheduler ------------------------------- */. - Names: suite = class name (
TEST(Scheduler, CancelOne)), case = PascalCase behavior (ExecuteAfterDelay,CancelUnknown,DestructionCancelPendingTasks); fixtures<Class>Test, parametrized<Class><Aspect>with astruct <Class>Case+operator<<(readable name) +INSTANTIATE_TEST_SUITE_P(Cases, ...). TEST(...) {brace on the same line; static helpers (waitFor(condition, timeout),<Name>Scenario(...)) brace on their own line;using namespace std::chrono_literals;is accepted in tests only (time literals).- For every public class / function, cover:
- default state / construction;
- nominal behavior (each public method);
- edge cases (empty, 0, max, overflow, unicode, separators, reentrancy);
- errors: the exact exception code
try { ...; FAIL() << "Expected an exception"; } catch (const utils::exception::IException& e) {EXPECT_EQ(e.getCode(), utils::exception::InternalCode::UnknownId);}(EXPECT_THROWonly when the type is enough); - threads / timing:
waitForwith a timeout instead of fixed sleeps,std::atomicflags,StepSynchronizer; - anything that can hang or crash (IPC, sockets, destruction while working,
fork): sub-process with a timeout#define ISOLATED(...) EXPECT_EXIT({::alarm(10); __VA_ARGS__; std::exit(::testing::Test::HasFailure() ? 1 : 0);}, ::testing::ExitedWithCode(0), "")around a static<Name>Scenariofunction; - global state (verbose level, env, singletons): set and restore it in the fixture
SetUp/TearDown; - interfaces: test the users of
IXwith aMockX(records the calls in a journal, configurable result).
- The tests describe the expected behavior. A test that reveals a bug stays failing (comment
// Expected behavior: fails until <bug> is fixed), the library code is never changed to make a test pass unless the user asks; code that can't even be instantiated is reported by a failing test, not by breaking the build. List the failing tests (= bugs found) in the report.
4. Run
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON && cmake --build build --parallel $(nproc)
ctest --test-dir build --output-on-failure --timeout 30 # or ./unit_tests --gtest_filter='Scheduler.*'
- Asan pass for memory errors:
-DCMAKE_BUILD_TYPE=Asan(needslibutils-as[-pre]when libutils is used). - Optional coverage:
-DCMAKE_CXX_FLAGS="--coverage"thengcovr -r . --exclude tests(orllvm-covwith clang). - Report: tests added per module, results (passed / failing = bugs found, with the reason), what remains untested
Deliver the report through the
reportskill (ask the output format ofreport(Markdown + PDFrecommended, PDF only, Markdown only)), in English, inaudit/at the root of the repository (or of the current folder outside a repository), neverdocs/unless asked (audit/<YYYY-MM-DD>-tests.*), not only in the chat. (untested.py).
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |