Core Principles
- •Test behavior, not implementation - Tests should verify what code does, not how
- •One assertion per concept - Each test should verify one logical concept
- •Readable tests are documentation - Test names and structure should explain the code
- •Fast feedback - Unit tests should run in milliseconds
- •Deterministic - Same input always produces same result
Test Pyramid
code
/\
/ \ E2E Tests (few)
/----\
/ \ Integration Tests (some)
/--------\
/ \ Unit Tests (many)
--------------
Unit Tests
- •Test individual functions/methods in isolation
- •Mock external dependencies
- •Fast execution (<100ms per test)
- •High coverage target (80%+)
Integration Tests
- •Test component interactions
- •Use real dependencies when practical
- •Test API contracts
- •Database interactions
E2E Tests
- •Test complete user flows
- •Run against real environment
- •Slower but high confidence
- •Critical paths only
AAA Pattern (Arrange-Act-Assert)
javascript
it('should return sum of two numbers', () => {
// Arrange
const calc = new Calculator();
// Act
const result = calc.add(2, 3);
// Assert
expect(result).toBe(5);
});
Input Validation
- • Empty/null/undefined inputs
- • Boundary values (0, -1, MAX_INT)
- • Invalid types
- • Malformed data
- • Unicode/special characters
- • Very long strings
- • Empty arrays/objects
State Handling
- • Initial state
- • After multiple operations
- • Concurrent modifications
- • Error recovery state
Async Operations
- • Success path
- • Timeout scenarios
- • Network failures
- • Race conditions
- • Retry logic
Error Handling
- • Expected errors thrown
- • Error messages correct
- • Error recovery works
- • Cleanup on error
When to Mock
External APIs, database calls, time/random, file system, network
When NOT to Mock
The code under test, simple value objects, pure functions
</MockingGuidelines> <FrameworkPatterns>Supported Frameworks
- •Jest/Vitest (JS/TS):
describe/it,jest.fn(),beforeEach/afterEach - •Pytest (Python):
@pytest.fixture,Mock,assert - •React Testing Library:
render,screen,userEvent - •Go:
testing.T, table-driven tests
Detect framework from package.json, pytest.ini, go.mod, or existing tests.
Patterns: should_[expected]_when_[condition] or [method]_[scenario]_[expected]
Targets: Statements 80%+, Branches 75%+, Functions 80%+
Prioritize: Business logic, error handling, edge cases, security code
Skip: Simple getters/setters, framework boilerplate, generated code
</CoverageGuidelines> <OutputFormat>Provide: test file location, dependencies, test structure, coverage notes, run command.
</OutputFormat>