# Testing Patterns

> This skill should be used when the user asks to "write tests", "test strategy", "coverage", "unit test", "integration test", or needs testing guidance. Provides testing methodology and patterns. Use when this capability is needed.

- Skill: `tomevault-io/testing-patterns-7` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/testing-patterns-7`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/testing-patterns-7/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/testing-patterns-7

---


# Testing Patterns and Strategy

## Test Types

| Type | Scope | Speed | When to Use |
|------|-------|-------|-------------|
| Unit | Single function/class | Fast | Business logic, utilities, transformations |
| Integration | Multiple components | Medium | API endpoints, DB operations, services |
| E2E | Full application | Slow | Critical user journeys, smoke tests |

## Test Structure

### Arrange-Act-Assert (Technical)
```
// Arrange: Set up test data
user = User.new(name: "John")
cart = ShoppingCart.new(user)

// Act: Execute code under test
total = cart.calculate_total

// Assert: Verify outcomes
assert_equal 0, total
```

### Given-When-Then (BDD)
```
given_a_user_with_an_empty_cart
when_the_user_calculates_total
then_the_total_should_be_zero
```

## Test Doubles

| Type | Purpose | Example |
|------|---------|---------|
| Stub | Canned responses | `stub(fetch_user: { id: 1 })` |
| Mock | Verify interactions | `mock.expect(:send_email, args: [...])` |
| Spy | Record calls, real behavior | `spy(Logger.new)` |
| Fake | Working lightweight impl | In-memory database |

## Naming Conventions

**Technical**: `test_[method]_[scenario]_[expected]`
```
test_calculateTotal_withEmptyCart_returnsZero
```

**BDD**: `[method]_should_[behavior]_when_[condition]`
```
calculateTotal_should_returnZero_when_cartIsEmpty
```

## Critical Practices

1. **Test happy path first**, then edge cases, then errors
2. **Edge cases**: Empty inputs, max values, nulls, zero, negatives
3. **Error cases**: Invalid inputs, network failures, timeouts
4. **Isolate tests**: Each test independent with setup/teardown
5. **One assertion per concept**: Single logical verification per test
6. **Readable names**: Tests serve as documentation

## Coverage Guidelines
- 80%+ for critical code paths
- Branch coverage more thorough than line coverage
- High coverage does not guarantee bug-free code
- Prioritize meaningful tests over metrics

## Anti-Patterns
- **Testing implementation** - Test behavior, not internals
- **Excessive mocking** - Use real implementations where practical
- **Flaky tests** - Control time, randomness, async operations
- **Slow tests** - Unit tests should run in milliseconds
- **Test interdependence** - Each test creates its own data

---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/motoki317) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-13 -->

