원클릭으로
test-driven-development
Use when implementing any feature or bugfix in C++, before writing implementation code
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Use when implementing any feature or bugfix in C++, before writing implementation code
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
| name | test-driven-development |
| description | Use when implementing any feature or bugfix in C++, before writing implementation code |
Write the test first. Watch it fail. Write minimal code to pass.
Core principle: If you didn't watch the test fail (or fail to compile), you don't know if it tests the right thing.
Violating the letter of the rules is violating the spirit of the rules.
Always:
Exceptions (ask your human partner):
Thinking "skip TDD just this once"? Stop. That's rationalization.
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST Write code before the test? Delete it. Start over.
No exceptions:
Implement fresh from tests. Period.
Write one minimal test showing what should happen.
```cpp // math_test.cpp #include #include "MathUtils.h"TEST(MathUtilsTest, AddReturnsSum) { EXPECT_EQ(MathUtils::Add(2, 3), 5); }
Clear name, tests real behavior, one thing. Fails to compile (Red Phase 1).
</Good>
<Bad>
```cpp
// ❌ BAD
TEST(MathTest, Works) {
// Tests implementation details or mocks too much
MockMath mock;
EXPECT_CALL(mock, Add(2,3)).WillOnce(Return(5));
// ...
}
Vague name, tests mock usage not logic.
Requirements:
MANDATORY. Never skip.
make test
# OR
./build/tests/my_tests
Confirm:
Test passes (or compiles and runs)? You're testing existing behavior. Fix test. Test errors (segfault/exception)? Fix error, re-run until it fails correctly (assertion).
Write simplest code to pass the test.
```cpp // MathUtils.h class MathUtils { public: static int Add(int a, int b) { return a + b; } }; ``` Just enough to pass. ```cpp // MathUtils.h class MathUtils { public: // YAGNI: Over-engineered with template metaprogramming or unnecessary flags template static T Add(T a, T b, bool secure = false) { if (secure) { /* ... */ } return a + b; } }; ``` Over-engineered.Don't add features, refactor other code, or "improve" beyond the test.
MANDATORY.
make test
Confirm:
Test fails? Fix code, not test. Other tests fail? Fix now.
After green only:
const, auto, standard algorithms.Keep tests green. Don't add behavior.
Next failing test for next feature.
| Quality | Good | Bad |
|---|---|---|
| Minimal | One assertion concept. | TEST(User, ValidateEmailAndDomainAndWhitespace) |
| Clear | Name describes behavior. | TEST(UserTest, Test1) |
| Shows intent | Demonstrates desired API usage. | Obscures what code should do. |
"I'll write tests after to verify it works" Tests written after code pass immediately. Passing immediately proves nothing:
Test-first forces you to see the test fail, proving it actually tests something.
"I already manually tested all the edge cases" Manual testing is ad-hoc. You think you tested everything but:
Automated tests are systematic. They run the same way every time.
"Deleting X hours of work is wasteful" Sunk cost fallacy. The time is already gone. Your choice now:
"TDD is dogmatic, being pragmatic means adapting" TDD IS pragmatic:
"Pragmatic" shortcuts = debugging in production = slower.
"Tests after achieve the same goals - it's spirit not ritual" No. Tests-after answer "What does this do?" Tests-first answer "What should this do?" Tests-after are biased by your implementation. You test what you built, not what's required. You verify remembered edge cases, not discovered ones. Tests-first force edge case discovery before implementing. Tests-after verify you remembered everything (you didn't). 30 minutes of tests after ≠ TDD. You get coverage, lose proof tests work.
| Excuse | Reality |
|---|---|
| "Too simple to test" | Simple code breaks. Test takes 30 seconds. |
| "I'll test after" | Tests passing immediately prove nothing. |
| "Tests after achieve same goals" | Tests-after = "what does this do?" Tests-first = "what should this do?" |
| "Already manually tested" | Ad-hoc ≠ systematic. No record, can't re-run. |
| "Deleting X hours is wasteful" | Sunk cost fallacy. Keeping unverified code is technical debt. |
| "Keep as reference, write tests first" | You'll adapt it. That's testing after. Delete means delete. |
| "Need to explore first" | Fine. Throw away exploration, start with TDD. |
| "Test hard = design unclear" | Listen to test. Hard to test = hard to use. |
| "TDD will slow me down" | TDD faster than debugging. Pragmatic = test-first. |
| "Manual test faster" | Manual doesn't prove edge cases. You'll re-test every change. |
| "Existing code has no tests" | You're improving it. Add tests for existing code. |
All of these mean: Delete code. Start over with TDD.
Bug: Empty email accepted
RED
// UserTest.cpp
TEST(UserTest, RejectsEmptyEmail) {
User user;
auto result = user.setEmail("");
EXPECT_EQ(result, User::Error::EmailRequired);
}
Verify RED
$ make test
[FAIL] Expected: EmailRequired, Actual: Success
GREEN
// User.cpp
User::Error User::setEmail(const std::string& email) {
if (email.empty()) {
return Error::EmailRequired;
}
// ...
}
Verify GREEN
$ make test
[PASS] UserTest.RejectsEmptyEmail
REFACTOR Extract validation for multiple fields if needed.
Before marking work complete:
Can't check all boxes? You skipped TDD. Start over.
| Problem | Solution |
|---|---|
| Don't know how to test | Write wished-for API first. Write assertion first. Ask your human partner. |
| Test too complicated | Design too complicated. Simplify interface. |
| Must mock everything | Code too coupled. Use dependency injection (interfaces/templates). |
| Test setup huge | Extract helpers. Still complex? Simplify design/reduce dependencies. |
Integration Bug found? Write failing test reproducing it. Follow TDD cycle. Test proves fix and prevents regression. Never fix bugs without a test.
When adding mocks or test utilities, read testing_anti_patterns.md to avoid common pitfalls:
Production code → test exists and failed first Otherwise → not TDD No exceptions without your human partner's permission.