test-patterns
Test Patterns
A technology-agnostic skill for writing test code following established patterns.
Test Structure
Arrange-Act-Assert (AAA)
pattern:
arrange: "Set up test data and preconditions"
act: "Execute the code under test"
assert: "Verify the expected outcome"
rules:
- "Each test has exactly one Act step"
- "Assert only what the test name promises"
- "Arrange should use fixtures or factories, not inline data"
Test Naming
naming_convention:
pattern: "test<Action><ExpectedBehavior>"
examples:
- "testLoginWithValidCredentialsReturnsToken"
- "testCreateUserWithDuplicateEmailThrowsError"
- "testCalculateTotalAppliesDiscount"
rules:
- "Name describes the scenario and expected outcome"
- "No generic names like testMethod1, testHappyPath"
- "Read the name alone to understand what is being verified"
Test Isolation
isolation_principles:
no_shared_state:
description: "Each test runs independently"
rules:
- "No dependency on test execution order"
- "No shared mutable state between tests"
- "setUp/tearDown restore clean state"
no_external_dependencies:
description: "Tests don't rely on external systems"
rules:
- "Mock external APIs and services"
- "Use in-memory or test databases"
- "No network calls in unit tests"
deterministic:
description: "Same input always produces same result"
rules:
- "No reliance on current time (inject clock)"
- "No random values without seeding"
- "No filesystem side effects"
Fixture Patterns
fixture_patterns:
factory:
description: "Generate test data with defaults and overrides"
use_when: "Need multiple variations of same entity"
benefit: "Readable, maintainable test data"
fixture_files:
description: "Predefined data loaded before tests"
use_when: "Database state needed for integration tests"
benefit: "Consistent starting state"
builder:
description: "Fluent API for constructing test objects"
use_when: "Complex objects with many optional fields"
benefit: "Only specify what matters for each test"
Assertion Patterns
assertion_rules:
one_concept_per_test:
description: "Each test verifies one behavior"
anti_pattern: "Multiple unrelated assertions in one test"
specific_assertions:
description: "Use the most specific assertion available"
examples:
- "assertEquals over assertTrue(a == b)"
- "assertContains over assertTrue(str.includes(x))"
- "assertThrows over try-catch with fail()"
meaningful_messages:
description: "Include context in assertion failures"
rule: "Failure message should explain what went wrong without reading the code"
Mocking Guidelines
mocking_rules:
mock_boundaries_only:
description: "Only mock at system boundaries"
mock: "External APIs, databases, file systems, time"
dont_mock: "Internal classes, business logic, value objects"
verify_interactions:
description: "When mocking, verify the interaction matters"
rule: "Mock verification should prove a business requirement"
prefer_fakes:
description: "Use fakes over mocks when possible"
benefit: "Fakes exercise more real code, catch more bugs"
Test Categories
categories:
unit:
scope: "Single function or class"
speed: "Milliseconds"
isolation: "Full - no external dependencies"
when: "Every code change"
integration:
scope: "Multiple components working together"
speed: "Seconds"
isolation: "Partial - may use test database"
when: "After unit tests pass"
e2e:
scope: "Full user workflow"
speed: "Seconds to minutes"
isolation: "Minimal - uses real services"
when: "Pre-merge, pre-release"
Used By Agents
primary_users:
- test-developer: "Test code implementation"
More from masanao-ohba/claude-manifests
requirement-analyzer
Invoke when goal-clarifier analyzes user requirements to extract goals and constraints. Provides structured requirement decomposition into functional/non-functional categories, stakeholder mapping, assumption identification, and documentation format.
37test-case-designer
Invoke when test-strategist plans test coverage for CakePHP features. Produces categorized test case specifications (unit/integration/system) with CakePHP-specific fixtures, IntegrationTestTrait usage, and proper test documentation format.
26functional-designer
Invoke when design-architect creates functional specs for CakePHP features. Produces detailed technical specifications mapping requirements to CakePHP controllers, models, services, and views with data flow diagrams and API endpoint definitions.
18react-architectural-patterns
Invoke when design-architect or code-developer designs React 19 component architecture. Provides component type taxonomy, composition patterns, state management strategies, render optimization techniques, and React 19 feature guidance.
15nextjs-code-reviewer
Invoke when quality-reviewer reviews Next.js 15 App Router code. Provides framework-specific review checklist covering file organization, server/client component usage, data fetching correctness, metadata configuration, and performance patterns.
14security-patterns
Invoke when code-developer or quality-reviewer handles PHP security concerns. Provides input validation patterns, SQL injection prevention, XSS protection, CSRF mitigation, secure session management, and password hashing best practices for PHP.
14