--- name: testing-strategy description: Design a testing strategy — testing pyramid, what to test at each level, coverage targets, mocking boundaries. Use when planning test coverage for a new feature or evaluating confidence in a codebase. license: MIT --- ## What I do Develop comprehensive testing strategies that provide confidence in code correctness. ## The Testing Pyramid - **Unit tests (70%)**: Fast, cheap, isolated - test single function/module - **Integration tests (20%)**: Verify components work together - **E2E tests (10%)**: Few, slow, expensive - test critical user paths > **Tip**: Prefer integration tests—they catch more bugs with less code. Write > tests after prototype when code firms up. ### By Level - Unit: 70%, milliseconds, single function/module - Integration: 20%, seconds, multiple components - E2E: 10%, minutes, full system ## What to Test ### Unit Tests Test **behavior**, not implementation: - Function returns correct output - Module handles error correctly - Input validation works - Don't test: Internal implementation details, private functions ### Integration Tests Test **component interactions**: - Database queries, API endpoints, External service calls ### E2E Tests Test **critical user paths**: - CLI command execution, Full build pipeline ## Test Structure ### Arrange-Act-Assert (AAA) ```rust #[test] fn test_calculate_total() { // Arrange let items = vec![Item { price: 10.0 }, Item { price: 20.0 }]; // Act let total = calculate_total(&items); // Assert assert_eq!(total, 30.0); } ``` ## Test Coverage ### Targets - Line coverage: 80% - Basic confidence - Branch coverage: 75% - All code paths - Function coverage: 100% - Every function tested **Prioritize**: Business logic, critical paths, error handling, edge cases. ## Mocking Strategy ### Mock This - External APIs - Database - File system - Network ### Not This - Core business logic - Pure calculations - Unit testable functions ## Edge Cases to Test - Empty input (null, None, nil, empty slice) - Zero and negative numbers - Maximum values (i32::MAX, etc.) - Special characters, Unicode - Very long strings/buffers ### Error Handling - Invalid input types - Network failures, Permission denied - Resource not found, Timeout scenarios ## Testing Best Practices ### Do - [ ] Test behavior, not implementation - [ ] Use descriptive test names - [ ] Keep tests independent - [ ] Test edge cases - [ ] Follow AAA pattern - [ ] Run tests before commit ### Don't - [ ] Test implementation details - [ ] Share state between tests - [ ] Write flaky tests - [ ] Test external services directly --- ## Advanced Patterns ### Test Features, Not Code Can you reuse your test suite if the entire software is replaced with an opaque neural network? - Test at **boundaries**: public API for libraries, human-visible output for apps ### Make Tests Fast - Caller does IO: Callee does pure compute - No disk/network: Even large code tests fast - Slow tests: Skip by default, run on CI only ### Data Driven Testing ```rust fn check(input: &str, expected: &str) { assert_eq!(process(input), expected); } #[test] fn test_processing() { check("2 + 2", "4"); check("-1 + 1", "0"); check("foo", "error"); } ``` ### Expect Tests (Snapshot Testing) Use **insta**, **k9**, or **expect-test**: ```rust #[test] fn test_format() { expect!(format_ast(ast)).to_equal(r#"(module (fn add [a b] (+ a b)))"#); } ``` ### Beyond Example-Based Testing - Property-based: Random inputs, verify output makes sense - Coverage testing: All inputs for small domains - Fuzzing: Random bytes, check for errors not crashes ### Test Everything Tests can check more than just correctness: - Code formatting - Git history (no secrets committed) - Documentation builds - License headers --- Use this skill to create comprehensive test coverage that provides real confidence.