test-patterns
Patterns for generating and organizing tests — unit, integration, data factories, coverage strategies — across multiple languages and frameworks. Trigger on test-writing or coverage-improvement requests.
What this skill does
# Test Patterns Skill
Generate and organize tests following project conventions and industry best practices.
## When to Use
- Generating unit tests for new or existing code
- Creating integration/API tests
- Setting up test data factories
- Analyzing and improving test coverage
- Establishing testing conventions in a project
## Reference Documents
- [Unit Test Patterns](./references/unit-test-patterns.md) - Patterns for unit tests including mocking, fixtures, and assertions
- [Integration Test Patterns](./references/integration-test-patterns.md) - API and integration testing patterns
- [Test Data Factories](./references/test-data-factories.md) - Factory patterns for generating test data
- [Coverage Strategies](./references/coverage-strategies.md) - Approaches to meaningful test coverage
## Core Principles
### 1. Test Behavior, Not Implementation
```
WRONG: Testing internal method calls
RIGHT: Testing observable behavior and outputs
```
Tests should verify what code does, not how it does it. This makes tests resilient to refactoring.
### 2. Arrange-Act-Assert (AAA) Pattern
Every test should have three distinct sections:
```python
# Arrange - Set up test data and conditions
user = create_user(name="Alice")
order = create_order(user=user, items=[item1, item2])
# Act - Execute the behavior being tested
result = order.calculate_total()
# Assert - Verify the expected outcome
assert result == 150.00
```
### 3. One Assertion Per Test (Logical)
Each test should verify one logical concept, though it may have multiple assertions for that concept:
```python
def test_user_creation_sets_defaults():
user = User.create(email="[email protected]")
# Multiple assertions for one concept: default values
assert user.status == "pending"
assert user.role == "member"
assert user.created_at is not None
```
### 4. Descriptive Test Names
Test names should describe the scenario and expected outcome:
```python
# WRONG
def test_order():
def test_calculate():
# RIGHT
def test_order_with_discount_applies_percentage_reduction():
def test_calculate_total_includes_tax_for_taxable_items():
```
### 5. Test Independence
Tests must not depend on each other or on execution order:
- Each test sets up its own data
- Each test cleans up after itself (or uses transactions)
- No shared mutable state between tests
## Workflow: Generating Tests
### Step 1: Analyze the Code Under Test
1. Read the file/function to be tested
2. Identify public interfaces and behaviors
3. List edge cases and error conditions
4. Note dependencies that need mocking
### Step 2: Determine Test Type
| Code Type | Test Type | Focus |
|-----------|-----------|-------|
| Pure function | Unit test | Input/output |
| Class with dependencies | Unit test with mocks | Behavior |
| API endpoint | Integration test | Request/response |
| Database operation | Integration test | Data persistence |
| External service call | Unit test with mocks | Contract |
### Step 3: Create Test Structure
```
tests/
├── unit/
│ └── [module]/
│ └── test_[file].py
├── integration/
│ └── test_[feature].py
└── fixtures/
└── [shared fixtures]
```
### Step 4: Write Tests
Follow the patterns in reference documents for specific test types.
### Step 5: Verify Coverage
Run coverage analysis and add tests for uncovered critical paths.
## Language-Specific Patterns
### Python (pytest)
```python
import pytest
from unittest.mock import Mock, patch
class TestOrderCalculation:
@pytest.fixture
def order(self):
return Order(items=[Item(price=100), Item(price=50)])
def test_calculates_subtotal(self, order):
assert order.subtotal == 150
@pytest.mark.parametrize("discount,expected", [
(0, 150),
(10, 135),
(50, 75),
])
def test_applies_discount(self, order, discount, expected):
order.apply_discount(discount)
assert order.total == expected
```
### TypeScript (Jest/Vitest)
```typescript
import { describe, it, expect, vi } from 'vitest';
describe('OrderService', () => {
it('calculates total with tax', () => {
const order = new Order([
{ price: 100 },
{ price: 50 }
]);
expect(order.totalWithTax(0.1)).toBe(165);
});
it('sends confirmation email on completion', async () => {
const emailService = { send: vi.fn() };
const order = new Order([], { emailService });
await order.complete();
expect(emailService.send).toHaveBeenCalledWith(
expect.objectContaining({ type: 'confirmation' })
);
});
});
```
### Ruby (RSpec)
```ruby
RSpec.describe Order do
subject(:order) { described_class.new(items: items) }
let(:items) { [Item.new(price: 100), Item.new(price: 50)] }
describe '#total' do
it 'sums item prices' do
expect(order.total).to eq(150)
end
context 'with discount applied' do
before { order.apply_discount(10) }
it 'reduces total by percentage' do
expect(order.total).to eq(135)
end
end
end
end
```
## Quick Reference
| Pattern | When to Use |
|---------|-------------|
| Factory | Creating test objects with defaults |
| Builder | Creating complex test objects step-by-step |
| Mock | Replacing dependencies |
| Stub | Providing canned responses |
| Spy | Verifying method calls |
| Fake | Lightweight implementation for testing |
| Fixture | Shared test data setup |
| Parametrize | Testing multiple inputs |
Related in Writing & Docs
jax-development
IncludedUse this skill when the user is writing, debugging, profiling, refactoring, reviewing, benchmarking, parallelising, exporting, or explaining JAX code, or when they mention JAX, jax.numpy, jit, grad, value_and_grad, vmap, scan, lax, random keys, pytrees, jax.Array, sharding, Mesh, PartitionSpec, NamedSharding, pmap, shard_map, Pallas, XLA, StableHLO, checkify, profiler, or the JAX repo. It helps turn NumPy or PyTorch-style code into pure functional JAX, fix tracer/control-flow/shape/PRNG bugs, remove recompiles and host-device syncs, choose transforms and sharding strategies, inspect jaxpr/lowering/IR, and benchmark compiled code correctly.
nature-article-writer
IncludedDrafts, rewrites, diagnostically critiques, and style-calibrates primary research manuscripts for Nature and Nature Portfolio journals. Use when the user wants a Nature-style title, summary paragraph or abstract, introduction, results, discussion, methods, figure legends, presubmission enquiry, cover letter, reviewer response, or when a scientific draft sounds generic, jargon-heavy, structurally weak, or AI-ish and needs precise, broad-reader-friendly prose without inventing data, analyses, or references. Best for primary research articles and letters rather than reviews or press releases unless explicitly adapting one.
deckrd
IncludedDocument-driven framework that derives requirements, specifications, implementation plans, and executable tasks from goals through structured AI dialogue. Use when user says "write requirements", "create spec", "plan implementation", "derive tasks", "structure this feature", "break down into tasks", or "document this module". Also use for reverse engineering existing code into docs (/deckrd rev). Do NOT use for direct code writing — use /deckrd-coder after tasks are generated. Do NOT use when the user only wants to run or fix existing code without planning.
clinical-decision-support
IncludedGenerate professional clinical decision support (CDS) documents for pharmaceutical and clinical research settings, including patient cohort analyses (biomarker-stratified with outcomes) and treatment recommendation reports (evidence-based guidelines with decision algorithms). Supports GRADE evidence grading, statistical analysis (hazard ratios, survival curves, waterfall plots), biomarker integration, and regulatory compliance. Outputs publication-ready LaTeX/PDF format optimized for drug development, clinical research, and evidence synthesis.
handling-sf-data
IncludedSalesforce data operations with 130-point scoring. Use this skill to create, update, delete, bulk import/export, generate test data, and clean up org records using sf CLI and anonymous Apex. TRIGGER when: user creates test data, performs bulk import/export, uses sf data CLI commands, needs data factory patterns for Apex tests, or needs to seed/clean records in a Salesforce org. DO NOT TRIGGER when: SOQL query writing only (use querying-soql), Apex test execution (use running-apex-tests), or metadata deployment (use deploying-metadata).
accelint-ac-to-playwright
IncludedConvert and validate acceptance criteria for Playwright test automation. Use when user asks to (1) review/evaluate/check if AC are ready for automation, (2) assess if AC can be converted as-is, (3) validate AC quality for Playwright, (4) turn AC into tests, (5) generate tests from acceptance criteria, (6) convert .md bullets or .feature Gherkin files to Playwright specs, (7) create test automation from requirements. Handles both bullet-style markdown and Gherkin syntax with JSON test plan generation and validation.