testing-gtest-gmock
Skill5 starsChanged 17 days agoMITView on GitHub
Plan and write C++ unit tests using GTest and GMock - creates test plan and implements tests one by one with user approval
In this repository50 other files it gives its agents.
- analyze-pr-commentsSkill
- architecture-analyzeSkill
- architecture-tradeoffSkill
- askSkill
- code-smellsSkill
- debugging-rocprof-sysSkill
- exploration-explore-codeSkill
- git-commitSkill
- git-gh-clientSkill
- git-prepare-pull-requestSkill
- git-pull-request-statusSkill
- gitSkill
- grill-meSkill
- library-amd-smiSkill
- notify-meSkill
- planning-architectureSkill
- planning-baseSkill
- planning-bugfixSkill
- planning-docsSkill
- planning-featureSkill
- planning-refactorSkill
- programming-cmake-best-practicesSkill
- programming-cpp-constexprSkill
- programming-cpp-design-patternsSkill
- programming-cpp-naming-rulesSkill
- programming-cpp-policy-based-diSkill
- programming-cppSkill
- programming-cpp-stl-algorithmsSkill
- programming-pythonSkill
- pr-review-interactiveSkill
- pr-reviewSkill
- radisha-create-skillSkill
- radisha-helpSkill
- radisha-improve-skillSkill
- radisha-skillsSkill
- radisha-updateSkill
- refactoring-techniquesSkill
- review-architectureSkill
- rocprofsys-buildSkill
- rocprofsys-configureSkill
- rocprofsysSkill
- static-analysisSkill
- testing-pytestSkill
- testing-testplanSkill
- therock-build-to-commitSkill
- therock-commit-in-buildSkill
- therock-first-build-with-commitSkill
- update-pr-descriptionSkill
- verify-pmc-metricsSkill
- watchSkill
The file
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 9 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈9
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 8
Use this skill when writing unit tests for C++ code using Google Test (GTest) and Google Mock (GMock).
- Distinctive: line 12
After completing a feature, refactoring, or bugfix, ASK the user:
- Distinctive: line 18
If user agrees, follow the planning process below.
- Distinctive: line 21
Write ONE test at a time, then STOP and wait for user approval before writing the next test.
- Distinctive: line 26
Before writing tests, identify:
- Distinctive: line 72
WAIT for user approval
- Distinctive: line 73
Only after approval, proceed to next test
- Distinctive: line 76
DO NOT write multiple tests at once!
- Distinctive: line 276
Use descriptive names that explain:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
GTest/GMock Testing Skill
Use this skill when writing unit tests for C++ code using Google Test (GTest) and Google Mock (GMock).
<IMPORTANT>
**When to trigger this skill:**
After completing a feature, refactoring, or bugfix, ASK the user:"Implementation is complete. Would you like me to add unit tests?
- Which components/functions should be tested?
- Are there specific edge cases to cover?"
If user agrees, follow the planning process below.
Test-by-test approval:
Write ONE test at a time, then STOP and wait for user approval before writing the next test.
</IMPORTANT>
Phase 1: Analyze Testable Code
Before writing tests, identify:
- Public API - Functions/methods that should be tested
- Dependencies - What needs to be mocked
- Edge cases - Boundary conditions, error states, empty inputs
- Invariants - Conditions that must always hold
Phase 2: Create Test Plan
Save to planning/tests-<component>.md:
# Test Plan: <Component Name>
## Scope
<What is being tested>
## Test Cases
### <Function/Class 1>
- [ ] Test: <test name> - <what it verifies>
- [ ] Test: <test name> - <what it verifies>
#### Edge Cases
- [ ] <edge case description>
- [ ] <edge case description>
### <Function/Class 2>
...
## Mocks Required
- [ ] `mock_<name>` - mocks `<interface>` for <purpose>
- [ ] `mock_<name>` - mocks `<interface>` for <purpose>
## Test Fixtures
- [ ] `<fixture_name>` - shared setup for <test group>Phase 3: Implement Tests (One by One)
<IMPORTANT>
**CRITICAL: Test-by-test workflow**- Write ONE test
- Show it to the user
- Say: "Here's the test for
<description>. Is it OK? Should I continue with the next test?" - WAIT for user approval
- Only after approval, proceed to next test
- Mark completed test in plan file
DO NOT write multiple tests at once!
</IMPORTANT>
GTest/GMock Patterns
Basic Test Structure
#include <gtest/gtest.h>
#include <gmock/gmock.h>
// Test case naming: TestSuiteName_TestName
TEST(calculator_test, add_returns_sum_of_two_numbers)
{
calculator calc;
EXPECT_EQ(calc.add(2, 3), 5);
}Test Fixture
class parser_test : public ::testing::Test
{
protected:
void SetUp() override
{
m_parser = std::make_unique<parser>();
}
void TearDown() override
{
m_parser.reset();
}
std::unique_ptr<parser> m_parser;
};
TEST_F(parser_test, parse_empty_input_returns_nullopt)
{
EXPECT_EQ(m_parser->parse(""), std::nullopt);
}Creating Mocks
// Interface to mock
class i_database
{
public:
virtual ~i_database() = default;
virtual bool save(const record& r) = 0;
virtual std::optional<record> find(int id) = 0;
};
// Mock class
class mock_database : public i_database
{
public:
MOCK_METHOD(bool, save, (const record& r), (override));
MOCK_METHOD(std::optional<record>, find, (int id), (override));
};Using Mocks
using ::testing::Return;
using ::testing::_;
using ::testing::NiceMock;
using ::testing::StrictMock;
TEST_F(service_test, process_saves_to_database)
{
NiceMock<mock_database> mock_db;
service svc(mock_db);
EXPECT_CALL(mock_db, save(_))
.WillOnce(Return(true));
EXPECT_TRUE(svc.process(some_data));
}Common Matchers
using namespace ::testing;
// Value matchers
EXPECT_THAT(value, Eq(expected));
EXPECT_THAT(value, Ne(unexpected));
EXPECT_THAT(value, Lt(max));
EXPECT_THAT(value, Gt(min));
EXPECT_THAT(value, Le(max));
EXPECT_THAT(value, Ge(min));
// String matchers
EXPECT_THAT(str, StartsWith("prefix"));
EXPECT_THAT(str, EndsWith("suffix"));
EXPECT_THAT(str, HasSubstr("middle"));
EXPECT_THAT(str, MatchesRegex("pattern"));
// Container matchers
EXPECT_THAT(vec, IsEmpty());
EXPECT_THAT(vec, SizeIs(5));
EXPECT_THAT(vec, Contains(element));
EXPECT_THAT(vec, ElementsAre(1, 2, 3));
EXPECT_THAT(vec, UnorderedElementsAre(3, 1, 2));
EXPECT_THAT(vec, Each(Gt(0)));
// Pointer matchers
EXPECT_THAT(ptr, IsNull());
EXPECT_THAT(ptr, NotNull());
EXPECT_THAT(ptr, Pointee(Eq(value)));
// Optional matchers (custom or use value)
EXPECT_TRUE(opt.has_value());
EXPECT_EQ(opt.value(), expected);Testing Exceptions
TEST(parser_test, parse_invalid_throws_exception)
{
parser p;
EXPECT_THROW(p.parse("invalid"), parse_error);
}
TEST(parser_test, parse_invalid_throws_with_message)
{
parser p;
EXPECT_THROW(
{
try {
p.parse("invalid");
} catch (const parse_error& e) {
EXPECT_THAT(e.what(), HasSubstr("invalid"));
throw;
}
},
parse_error
);
}Parameterized Tests
class add_test : public ::testing::TestWithParam<std::tuple<int, int, int>>
{
};
TEST_P(add_test, returns_correct_sum)
{
auto [a, b, expected] = GetParam();
calculator calc;
EXPECT_EQ(calc.add(a, b), expected);
}
INSTANTIATE_TEST_SUITE_P(
calculator_tests,
add_test,
::testing::Values(
std::make_tuple(0, 0, 0),
std::make_tuple(1, 1, 2),
std::make_tuple(-1, 1, 0),
std::make_tuple(INT_MAX, 0, INT_MAX)
)
);Edge Cases Checklist
Always consider testing:
Boundary Values
- Zero / empty
- One element / single character
- Maximum values (INT_MAX, SIZE_MAX)
- Minimum values (INT_MIN, 0 for unsigned)
- Just below/above boundaries
Error Conditions
- Null pointers (if applicable)
- Empty containers
- Invalid input
- Resource exhaustion
- Timeout scenarios
State Transitions
- Initial state
- After single operation
- After multiple operations
- After error recovery
Test Naming Convention
Use descriptive names that explain:
- What is being tested
- Under what conditions
- Expected result
// Pattern: <unit>_<scenario>_<expected_result>
TEST(parser_test, parse_empty_string_returns_nullopt)
TEST(parser_test, parse_valid_json_returns_document)
TEST(parser_test, parse_invalid_json_throws_parse_error)
TEST(calculator_test, divide_by_zero_throws_domain_error)Test File Organization
tests/
├── CMakeLists.txt
├── unit/
│ ├── test_calculator.cpp
│ ├── test_parser.cpp
│ └── mocks/
│ ├── mock_database.hpp
│ └── mock_network.hpp
└── integration/
└── test_system.cppCMakeLists.txt for Tests
enable_testing()
find_package(GTest REQUIRED)
add_executable(unit_tests
unit/test_calculator.cpp
unit/test_parser.cpp
)
target_link_libraries(unit_tests
PRIVATE
GTest::gtest_main
GTest::gmock
mylib # Library being tested
)
include(GoogleTest)
gtest_discover_tests(unit_tests)References
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
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.

