| name | pytest-patterns |
| description | Pytest is a powerful testing framework for Python that makes it easy Use when this capability is needed. |
| metadata | {"author":"AmnadTaowsoam"} |
Pytest Patterns
Skill Profile
(Select at least one profile to enable specific modules)
Overview
Pytest is a powerful testing framework for Python that makes it easy to write simple yet scalable test cases. This skill covers pytest basics, test structure, fixtures, parametrization, markers, mocking, async tests, coverage, FastAPI testing, and database testing.
Why This Matters
Pytest provides:
- Simple and intuitive test syntax
- Powerful fixture system for test setup
- Built-in assertion introspection
- Extensive plugin ecosystem
- Parallel test execution
- Excellent integration with CI/CD
Core Concepts
- Test Discovery: Automatically finds test files matching patterns
- Fixtures: Reusable test setup and teardown
- Parametrization: Run tests with multiple inputs
- Markers: Categorize and select tests
- Mocking: Isolate code under test
- Async Support: Test async code with pytest-asyncio
- Coverage: Measure test coverage with pytest-cov
Inputs / Outputs / Contracts
- Inputs:
- <e.g., env vars, request payload, file paths, schema>
- Entry Conditions:
- <Pre-requisites: e.g., Repo initialized, DB running, specific branch checked out>
- Outputs:
- <e.g., artifacts (PR diff, docs, tests, dashboard JSON)>
- Artifacts Required (Deliverables):
- <e.g., Code Diff, Unit Tests, Migration Script, API Docs>
- Acceptance Evidence:
- <e.g., Test Report (screenshot/log), Benchmark Result, Security Scan Report>
- Success Criteria:
- <e.g., p95 < 300ms, coverage ≥ 80%>
Skill Composition
- Depends on: None
- Compatible with: None
- Conflicts with: None
- Related Skills: None
Quick Start
Assumptions
- Python 3.8+ is installed
- Project uses pytest as the test framework
- Source code follows Python best practices
Compatibility
- Python: 3.8+
- pytest: 7.0+
- pytest-asyncio: 0.21+
- pytest-mock: 3.10+
Test Scenario Matrix (QA Strategy)
| Type | Focus Area | Required Scenarios / Mocks |
|---|
| Unit | Core Logic | Must cover primary logic and at least 3 edge/error cases. Target minimum 80% coverage |
| Integration | DB / API | All external API calls or database connections must be mocked during unit tests |
| E2E | User Journey | Critical user flows to test |
| Performance | Latency / Load | Benchmark requirements |
| Security | Vuln / Auth | SAST/DAST or dependency audit |
| Frontend | UX / A11y | Accessibility checklist (WCAG), Performance Budget (Lighthouse score) |
Technical Guardrails & Security Threat Model
1. Security & Privacy (Threat Model)
- Top Threats: Injection attacks, authentication bypass, data exposure
2. Performance & Resources
3. Architecture & Scalability
4. Observability & Reliability
Agent Directives
- Always run tests before committing
- Use fixtures to reduce duplication
- Mock external dependencies
- Use parametrization for multiple inputs
- Keep tests independent
- Use markers to categorize tests
Definition of Done (DoD) Checklist
Anti-patterns
-
Testing Implementation Details
def test_internal_method():
user = User("John", "john@example.com")
assert user._internal_method() == "result"
def test_public_behavior():
user = User("John", "john@example.com")
assert user.full_name == "John (john@example.com)"
-
Multiple Assertions Per Test
def test_user():
user = User("John", "john@example.com")
assert user.name == "John"
assert user.email == "john@example.com"
assert user.id is None
def test_user_name():
user = User("John", "john@example.com")
assert user.name == "John"
def test_user_email():
user = User("John", "john@example.com")
assert user.email == "john@example.com"
-
Hardcoded Test Data
def test_addition():
+ ==
+ ==
+ ==
():
a + b == expected
Reference Links & Examples
- Internal documentation and examples
- Official documentation and best practices
- Community resources and discussions
Versioning & Changelog
- Version: 1.0.0
- Changelog:
- 2026-02-22: Initial version with complete template structure
Source: AmnadTaowsoam/CerebraSkills — distributed by TomeVault.