Claude
Cursor
opencode
Skill
testing-patterns
Testing patterns and principles. Unit, integration, mocking strategies.
Virus-scanned
Reviewed automatically before listing.
Download
vodailocz-kilo-kit-mcp-skills_engineering_testing-patterns-0448e6c.zip · 3 KB
Install
skills CLI
npx skills add https://github.com/VoDaiLocz/kilo-kit-mcp/tree/main/skills/engineering/testing-patterns
Claude Code
claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install vodailocz-kilo-kit-mcp@llmmart
Git
git clone https://github.com/VoDaiLocz/kilo-kit-mcp.git
The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole vodailocz/kilo-kit-mcp collection as a plugin from our marketplace. Git is the plain clone.
Skill manifest
Testing Patterns
Principles for reliable test suites.
1. Testing Pyramid
/\ E2E (Few)
/ \ Critical flows
/----\
/ \ Integration (Some)
/--------\ API, DB queries
/ \
/------------\ Unit (Many)
Functions, classes
2. AAA Pattern
| Step | Purpose |
|---|---|
| Arrange | Set up test data |
| Act | Execute code under test |
| Assert | Verify outcome |
3. Test Type Selection
When to Use Each
| Type | Best For | Speed |
|---|---|---|
| Unit | Pure functions, logic | Fast (<50ms) |
| Integration | API, DB, services | Medium |
| E2E | Critical user flows | Slow |
4. Unit Test Principles
Good Unit Tests
| Principle | Meaning |
|---|---|
| Fast | < 100ms each |
| Isolated | No external deps |
| Repeatable | Same result always |
| Self-checking | No manual verification |
| Timely | Written with code |
What to Unit Test
| Test | Don't Test |
|---|---|
| Business logic | Framework code |
| Edge cases | Third-party libs |
| Error handling | Simple getters |
5. Integration Test Principles
What to Test
| Area | Focus |
|---|---|
| API endpoints | Request/response |
| Database | Queries, transactions |
| External services | Contracts |
Setup/Teardown
| Phase | Action |
|---|---|
| Before All | Connect resources |
| Before Each | Reset state |
| After Each | Clean up |
| After All | Disconnect |
6. Mocking Principles
When to Mock
| Mock | Don't Mock |
|---|---|
| External APIs | The code under test |
| Database (unit) | Simple dependencies |
| Time/random | Pure functions |
| Network | In-memory stores |
Mock Types
| Type | Use |
|---|---|
| Stub | Return fixed values |
| Spy | Track calls |
| Mock | Set expectations |
| Fake | Simplified implementation |
7. Test Organization
Naming
| Pattern | Example |
|---|---|
| Should behavior | "should return error when..." |
| When condition | "when user not found..." |
| Given-when-then | "given X, when Y, then Z" |
Grouping
| Level | Use |
|---|---|
| describe | Group related tests |
| it/test | Individual case |
| beforeEach | Common setup |
8. Test Data
Strategies
| Approach | Use |
|---|---|
| Factories | Generate test data |
| Fixtures | Predefined datasets |
| Builders | Fluent object creation |
Principles
- Use realistic data
- Randomize non-essential values (faker)
- Share common fixtures
- Keep data minimal
9. Best Practices
| Practice | Why |
|---|---|
| One assert per test | Clear failure reason |
| Independent tests | No order dependency |
| Fast tests | Run frequently |
| Descriptive names | Self-documenting |
| Clean up | Avoid side effects |
10. Anti-Patterns
| ❌ Don't | ✅ Do |
|---|---|
| Test implementation | Test behavior |
| Duplicate test code | Use factories |
| Complex test setup | Simplify or split |
| Ignore flaky tests | Fix root cause |
| Skip cleanup | Reset state |
Remember: Tests are documentation. If someone can't understand what the code does from the tests, rewrite them.
Files (kilo-kit-mcp)
-
scripts
-
test_runner.py 6.9 KB
#!/usr/bin/env python3 """ Test Runner - Unified test execution and coverage reporting Runs tests and generates coverage report based on project type. Usage: python test_runner.py <project_path> [--coverage] Supports: - Node.js: npm test, jest, vitest - Python: pytest, unittest """ import subprocess import sys import json from pathlib import Path from datetime import datetime # Fix Windows console encoding try: sys.stdout.reconfigure(encoding='utf-8', errors='replace') except: pass def detect_test_framework(project_path: Path) -> dict: """Detect test framework and commands.""" result = { "type": "unknown", "framework": None, "cmd": None, "coverage_cmd": None } # Node.js project package_json = project_path / "package.json" if package_json.exists(): result["type"] = "node" try: pkg = json.loads(package_json.read_text(encoding='utf-8')) scripts = pkg.get("scripts", {}) deps = {**pkg.get("dependencies", {}), **pkg.get("devDependencies", {})} # Check for test script if "test" in scripts: result["framework"] = "npm test" result["cmd"] = ["npm", "test"] # Try to detect specific framework for coverage if "vitest" in deps: result["framework"] = "vitest" result["coverage_cmd"] = ["npx", "vitest", "run", "--coverage"] elif "jest" in deps: result["framework"] = "jest" result["coverage_cmd"] = ["npx", "jest", "--coverage"] elif "vitest" in deps: result["framework"] = "vitest" result["cmd"] = ["npx", "vitest", "run"] result["coverage_cmd"] = ["npx", "vitest", "run", "--coverage"] elif "jest" in deps: result["framework"] = "jest" result["cmd"] = ["npx", "jest"] result["coverage_cmd"] = ["npx", "jest", "--coverage"] except: pass # Python project if (project_path / "pyproject.toml").exists() or (project_path / "requirements.txt").exists(): result["type"] = "python" result["framework"] = "pytest" result["cmd"] = ["python", "-m", "pytest", "-v"] result["coverage_cmd"] = ["python", "-m", "pytest", "--cov", "--cov-report=term-missing"] return result def run_tests(cmd: list, cwd: Path) -> dict: """Run tests and return results.""" result = { "passed": False, "output": "", "error": "", "tests_run": 0, "tests_passed": 0, "tests_failed": 0 } try: proc = subprocess.run( cmd, cwd=str(cwd), capture_output=True, text=True, encoding='utf-8', errors='replace', timeout=300 # 5 min timeout for tests ) result["output"] = proc.stdout[:3000] if proc.stdout else "" result["error"] = proc.stderr[:500] if proc.stderr else "" result["passed"] = proc.returncode == 0 # Try to parse test counts from output output = proc.stdout or "" # Jest/Vitest pattern: "Tests: X passed, Y failed, Z total" if "passed" in output.lower() and "failed" in output.lower(): import re match = re.search(r'(\d+)\s+passed', output, re.IGNORECASE) if match: result["tests_passed"] = int(match.group(1)) match = re.search(r'(\d+)\s+failed', output, re.IGNORECASE) if match: result["tests_failed"] = int(match.group(1)) result["tests_run"] = result["tests_passed"] + result["tests_failed"] # Pytest pattern: "X passed, Y failed" if "pytest" in str(cmd): import re match = re.search(r'(\d+)\s+passed', output) if match: result["tests_passed"] = int(match.group(1)) match = re.search(r'(\d+)\s+failed', output) if match: result["tests_failed"] = int(match.group(1)) result["tests_run"] = result["tests_passed"] + result["tests_failed"] except FileNotFoundError: result["error"] = f"Command not found: {cmd[0]}" except subprocess.TimeoutExpired: result["error"] = "Timeout after 300s" except Exception as e: result["error"] = str(e) return result def main(): project_path = Path(sys.argv[1] if len(sys.argv) > 1 else ".").resolve() with_coverage = "--coverage" in sys.argv print(f"\n{'='*60}") print(f"[TEST RUNNER] Unified Test Execution") print(f"{'='*60}") print(f"Project: {project_path}") print(f"Coverage: {'enabled' if with_coverage else 'disabled'}") print(f"Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") # Detect test framework test_info = detect_test_framework(project_path) print(f"Type: {test_info['type']}") print(f"Framework: {test_info['framework']}") print("-"*60) if not test_info["cmd"]: print("No test framework found for this project.") output = { "script": "test_runner", "project": str(project_path), "type": test_info["type"], "framework": None, "passed": True, "message": "No tests configured" } print(json.dumps(output, indent=2)) sys.exit(0) # Choose command cmd = test_info["coverage_cmd"] if with_coverage and test_info["coverage_cmd"] else test_info["cmd"] print(f"Running: {' '.join(cmd)}") print("-"*60) # Run tests result = run_tests(cmd, project_path) # Print output (truncated) if result["output"]: lines = result["output"].split("\n") for line in lines[:30]: print(line) if len(lines) > 30: print(f"... ({len(lines) - 30} more lines)") # Summary print("\n" + "="*60) print("SUMMARY") print("="*60) if result["passed"]: print("[PASS] All tests passed") else: print("[FAIL] Some tests failed") if result["error"]: print(f"Error: {result['error'][:200]}") if result["tests_run"] > 0: print(f"Tests: {result['tests_run']} total, {result['tests_passed']} passed, {result['tests_failed']} failed") output = { "script": "test_runner", "project": str(project_path), "type": test_info["type"], "framework": test_info["framework"], "tests_run": result["tests_run"], "tests_passed": result["tests_passed"], "tests_failed": result["tests_failed"], "passed": result["passed"] } print("\n" + json.dumps(output, indent=2)) sys.exit(0 if result["passed"] else 1) if __name__ == "__main__": main()
-
-
SKILL.md 3.4 KB
--- name: testing-patterns description: Testing patterns and principles. Unit, integration, mocking strategies. allowed-tools: Read, Write, Edit, Glob, Grep, Bash --- # Testing Patterns > Principles for reliable test suites. --- ## 1. Testing Pyramid ``` /\ E2E (Few) / \ Critical flows /----\ / \ Integration (Some) /--------\ API, DB queries / \ /------------\ Unit (Many) Functions, classes ``` --- ## 2. AAA Pattern | Step | Purpose | |------|---------| | **Arrange** | Set up test data | | **Act** | Execute code under test | | **Assert** | Verify outcome | --- ## 3. Test Type Selection ### When to Use Each | Type | Best For | Speed | |------|----------|-------| | **Unit** | Pure functions, logic | Fast (<50ms) | | **Integration** | API, DB, services | Medium | | **E2E** | Critical user flows | Slow | --- ## 4. Unit Test Principles ### Good Unit Tests | Principle | Meaning | |-----------|---------| | Fast | < 100ms each | | Isolated | No external deps | | Repeatable | Same result always | | Self-checking | No manual verification | | Timely | Written with code | ### What to Unit Test | Test | Don't Test | |------|------------| | Business logic | Framework code | | Edge cases | Third-party libs | | Error handling | Simple getters | --- ## 5. Integration Test Principles ### What to Test | Area | Focus | |------|-------| | API endpoints | Request/response | | Database | Queries, transactions | | External services | Contracts | ### Setup/Teardown | Phase | Action | |-------|--------| | Before All | Connect resources | | Before Each | Reset state | | After Each | Clean up | | After All | Disconnect | --- ## 6. Mocking Principles ### When to Mock | Mock | Don't Mock | |------|------------| | External APIs | The code under test | | Database (unit) | Simple dependencies | | Time/random | Pure functions | | Network | In-memory stores | ### Mock Types | Type | Use | |------|-----| | Stub | Return fixed values | | Spy | Track calls | | Mock | Set expectations | | Fake | Simplified implementation | --- ## 7. Test Organization ### Naming | Pattern | Example | |---------|---------| | Should behavior | "should return error when..." | | When condition | "when user not found..." | | Given-when-then | "given X, when Y, then Z" | ### Grouping | Level | Use | |-------|-----| | describe | Group related tests | | it/test | Individual case | | beforeEach | Common setup | --- ## 8. Test Data ### Strategies | Approach | Use | |----------|-----| | Factories | Generate test data | | Fixtures | Predefined datasets | | Builders | Fluent object creation | ### Principles - Use realistic data - Randomize non-essential values (faker) - Share common fixtures - Keep data minimal --- ## 9. Best Practices | Practice | Why | |----------|-----| | One assert per test | Clear failure reason | | Independent tests | No order dependency | | Fast tests | Run frequently | | Descriptive names | Self-documenting | | Clean up | Avoid side effects | --- ## 10. Anti-Patterns | ❌ Don't | ✅ Do | |----------|-------| | Test implementation | Test behavior | | Duplicate test code | Use factories | | Complex test setup | Simplify or split | | Ignore flaky tests | Fix root cause | | Skip cleanup | Reset state | --- > **Remember:** Tests are documentation. If someone can't understand what the code does from the tests, rewrite them.
Comments (0)
Sign in to join the conversation.
Reviews (0)
No reviews yet.
No comments yet.