---
name: testing-patterns
description: ALWAYS use this when choosing or improving test strategy, test shape, coverage boundaries, or the right mix of unit, integration, and end-to-end checks.
Testing Patterns
Selective Reading Rule
Start with:
references/senior-master-standard.md
references/usage-routing.md
references/quality-checklist.md
Then load only the inherited docs, scripts, assets, or examples that match the user's actual task.
Remember: Tests are documentation. If someone can't understand what the code does from the tests, rewrite them.
1---2name: testing-patterns3description: ---4---5---6name: testing-patterns7description: ALWAYS use this when choosing or improving test strategy, test shape, coverage boundaries, or the right mix of unit, integration, and end-to-end checks.8---910# Testing Patterns1112## Selective Reading Rule1314Start with:1516- `references/senior-master-standard.md`17- `references/usage-routing.md`18- `references/quality-checklist.md`1920Then load only the inherited docs, scripts, assets, or examples that match the user's actual task.2122> Principles for reliable test suites.2324---2526## 1. Testing Pyramid2728```29 /\ E2E (Few)30 / \ Critical flows31 /----\32 / \ Integration (Some)33 /--------\ API, DB queries34 / \35 /------------\ Unit (Many)36 Functions, classes37```3839---4041## 2. AAA Pattern4243| Step | Purpose |44|------|---------|45| **Arrange** | Set up test data |46| **Act** | Execute code under test |47| **Assert** | Verify outcome |4849---5051## 3. Test Type Selection5253### When to Use Each5455| Type | Best For | Speed |56|------|----------|-------|57| **Unit** | Pure functions, logic | Fast (<50ms) |58| **Integration** | API, DB, services | Medium |59| **E2E** | Critical user flows | Slow |6061---6263## 4. Unit Test Principles6465### Good Unit Tests6667| Principle | Meaning |68|-----------|---------|69| Fast | < 100ms each |70| Isolated | No external deps |71| Repeatable | Same result always |72| Self-checking | No manual verification |73| Timely | Written with code |7475### What to Unit Test7677| Test | Don't Test |78|------|------------|79| Business logic | Framework code |80| Edge cases | Third-party libs |81| Error handling | Simple getters |8283---8485## 5. Integration Test Principles8687### What to Test8889| Area | Focus |90|------|-------|91| API endpoints | Request/response |92| Database | Queries, transactions |93| External services | Contracts |9495### Setup/Teardown9697| Phase | Action |98|-------|--------|99| Before All | Connect resources |100| Before Each | Reset state |101| After Each | Clean up |102| After All | Disconnect |103104---105106## 6. Mocking Principles107108### When to Mock109110| Mock | Don't Mock |111|------|------------|112| External APIs | The code under test |113| Database (unit) | Simple dependencies |114| Time/random | Pure functions |115| Network | In-memory stores |116117### Mock Types118119| Type | Use |120|------|-----|121| Stub | Return fixed values |122| Spy | Track calls |123| Mock | Set expectations |124| Fake | Simplified implementation |125126---127128## 7. Test Organization129130### Naming131132| Pattern | Example |133|---------|---------|134| Should behavior | "should return error when..." |135| When condition | "when user not found..." |136| Given-when-then | "given X, when Y, then Z" |137138### Grouping139140| Level | Use |141|-------|-----|142| describe | Group related tests |143| it/test | Individual case |144| beforeEach | Common setup |145146---147148## 8. Test Data149150### Strategies151152| Approach | Use |153|----------|-----|154| Factories | Generate test data |155| Fixtures | Predefined datasets |156| Builders | Fluent object creation |157158### Principles159160- Use realistic data161- Randomize non-essential values (faker)162- Share common fixtures163- Keep data minimal164165---166167## 9. Best Practices168169| Practice | Why |170|----------|-----|171| One assert per test | Clear failure reason |172| Independent tests | No order dependency |173| Fast tests | Run frequently |174| Descriptive names | Self-documenting |175| Clean up | Avoid side effects |176177---178179## 10. Anti-Patterns180181| ⌠Don't | ✅ Do |182|----------|-------|183| Test implementation | Test behavior |184| Duplicate test code | Use factories |185| Complex test setup | Simplify or split |186| Ignore flaky tests | Fix root cause |187| Skip cleanup | Reset state |188189---190191> **Remember:** Tests are documentation. If someone can't understand what the code does from the tests, rewrite them.
Run npx skillmds@latest add anhvu1107/testing-patterns in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
--- It is listed under AI & ML on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
anhvu1107 (@anhvu1107) published this skill. Their other Agent Skills are listed on their SkillMD profile.