pytest

pytest - Python's most powerful testing framework with fixtures, parametrization, plugins, and framework integration for FastAPI, Django, Flask

By bobmatnyc · 1,249 installs

npx skills add bobmatnyc/claude-mpm-skills --skill pytest

Source repository · Upstream listing

pytest Professional Python Testing Overview pytest is the industry standard Python testing framework, offering powerful features like fixtures, parametrization, markers, plugins, and seamless integration with FastAPI, Django, and Flask. It provides a simple, scalable approach to testing from unit tests to complex integration scenarios. Key Features : Fixture system for dependency injection Parametrization for data driven tests Rich assertion introspection (no need for self.assertEqual ) Plugin ecosystem (pytest cov, pytest asyncio, pytest mock, pytest django) Async/await support Parallel test execution with pytest xdist Test discovery and organization Detailed failure reporting Installation : Basic Testing Patterns 1. Simple Test Functions Run tests: 2. Test Classes for Organization 3. Assertions and Expected Failures Fixtures Dependency Injection Basic Fixtures Fixture Scopes Fixture Setup and Teardown Fixture Dependencies Parametrization Data Driven Testing Basic Parametrization Multiple Parameters Parametrize with IDs Indirect Parametrization (Fixtures) Test Markers Built in Markers Custom Markers Run tests by marker: FastAPI Testing Basic FastAPI Test Setup FastAPI Test Client Async FastAPI Testing FastAPI with Database Testing Django Testing Django pytest Configuration Django Model Testing Django View Testing Django REST Framework Testing Mocking and Patching pytest mock (pytest.fixture.mocker) Mocking Class Methods Mocking with Side Effects Spy on Calls Coverage and Reporting pytest cov Configuration pytest.ini Coverage Configuration Coverage Reports Async Testing pytest asyncio Async Fixtures Local pytest Profiles (Your Repos) Common settings from your projects' pyproject.toml : asyncio mode = "auto" (default in mcp browser, mcp memory, claude mpm, edgar) addopts includes strict markers and strict config for CI consistency Coverage flags: cov=<package , cov report=term missing , cov report=xml Selective ignores (mcp vector search): ignore=tests/manual , ignore=tests/e2e pythonpath = ["src"] for editable import resolution (mcp ticketer) Typical markers: unit , integration , e2e slow , benchmark , performance requires api (edgar) Reference: see pyproject.toml in claude mpm , edgar , mcp vector search , mcp ticketer , and kuzu memory for full lists. Best Practices 1. Test Organization 2. Naming Conventions 3. Arrange Act Assert Pattern 4. Use Fixtures for Common Setup 5. Parametrize Similar Tests 6. Test One Thing Per Test 7. Use Markers for Test Organization 8. Mock External Dependencies Common Pitfalls ❌ Anti Pattern 1: Test Depends on Execution Order Correct: ❌ Anti Pattern 2: Not Cleaning Up Resources Correct: ❌ Anti Pattern 3: Testing Implementation Details Correct: ❌ Anti Pattern 4: Not Using pytest Features Correct: ❌ Anti Pattern 5: Overly Complex Fixtures Correct: Quick Reference Common Commands pytest.ini Template conftest.py Template Resources Official Documentation : https://docs.pytest.org/ pytest asyncio : https://pytest asyncio.readthedocs.io/ pytest cov : https://pytest cov.readthedocs.io/ pytest mock : https://pytest mock.readthedocs.io/ pytest django : https://pytest django.readthedocs.io/ FastAPI Testing : https://fastapi.tiangolo.com/tutorial/testing/ Related Skills When using pytest, consider these complementary skills: fastapi local dev : FastAPI development server patterns and test fixtures test driven development : Complete TDD workflow (RED/GREEN/REFACTOR cycle) systematic debugging : Root cause investigation for failing tests Quick TDD Workflow Reference (Inlined for Standalone Use) RED → GREEN → REFACTOR Cycle: 1. RED Phase: Write Failing Test 2. GREEN Phase: Make It Pass 3. REFACTOR Phase: Improve Code Test Structure: Arrange Act Assert (AAA) Quick Debugging Reference (Inlined for Standalone Use) Phase 1: Root Cause Investigation Read error messages completely (stack traces, line numbers) Reproduce consistently (document exact steps) Check recent changes (git log, git diff) Understand what changed and why it might cause failure Phase 2: Isolate the Problem Phase 3: Fix Root Cause Fix the underlying problem, not symptoms Add regression test to prevent recurrence Verify fix doesn't break other tests Phase 4: Verify Solution [Full TDD and debugging workflows available in respective skills if deployed together] Python Code Quality Anti Patterns Clean code under test is easier to test, and several Python quality defects directly cause flaky or silently passing tests (overly broad except , malformed exception classes, identity vs equality bugs). Code quality anti patterns now live in their own dedicated skill rather than this testing skill: See the python code quality skill ( toolchains/python/quality/code quality ) for the highest value Python anti patterns — exception hierarchy correctness, singleton identity comparison, narrow exception handling, wildcard import avoidance, magic number naming, and dead local removal — with non compliant vs compliant examples and how to test each. For the project wide severity tagged review checklist, see the code review standards skill. pytest Version Compatibility: This skill covers pytest 7.0+ and reflects current best practices for Python testing in 2025.