python-testing-patterns

Implement pytest test suites with fixtures, mocking, parameterization, and coverage reporting.

Updated Jun 12, 2026
One-click install
npx skills add https://github.com/bilacchi/agents-skills --skill python-testing-patterns-bilacchi
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: python-testing-patterns
Source: https://github.com/bilacchi/agents-skills/tree/main/skills/python-testing-patterns
Command: npx skills add https://github.com/bilacchi/agents-skills --skill python-testing-patterns-bilacchi

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing reliable Python tests requires knowing pytest fixtures, mocking strategies, async testing, and coverage configuration, which developers often implement inconsistently or skip entirely. ## Core Features & Use Cases - Pytest Patterns: Provides ready-to-use patterns for fixtures, parameterized tests, exception testing, and monkeypatching environment variables. - Mocking & Async Testing: Demonstrates unittest.mock usage for API clients and pytest-asyncio patterns for concurrent code. - Coverage & CI Integration: Includes pytest-cov configuration, test markers, and GitHub Actions workflow templates. - Use Case: When building a new API client, apply the mocking patterns to test HTTP calls without network access, then add parameterized tests for input validation and wire coverage reporting into CI. ## Quick Start Ask the AI to write pytest tests with fixtures and mocks for your Python module, following the patterns in this skill.

Frequently Asked Questions about python-testing-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write pytest fixtures for setup and teardown?▼

Define a function decorated with @pytest.fixture that performs setup, yields the resource to the test, then runs teardown code after the yield. Use scope parameters like "session" or "module" to control how often the fixture is created.

How to mock API requests in Python tests?▼

Use unittest.mock's patch to replace requests.get or requests.post with a Mock object whose return_value simulates the HTTP response. Configure json.return_value and raise_for_status to mimic success or error responses without network calls.

Does pytest support testing async functions?▼

Yes, pytest supports async tests through the pytest-asyncio plugin. Mark test functions with @pytest.mark.asyncio and use await inside them; async fixtures can also yield resources for asynchronous setup and teardown.

How do I run the same test with multiple inputs in pytest?▼

Use @pytest.mark.parametrize with a list of input and expected value tuples. Each tuple generates a separate test case, and pytest.param lets you assign custom IDs for readable test output.

How do I measure test coverage with pytest?▼

Install pytest-cov and run pytest with --cov=yourpackage to measure coverage. Add --cov-report=term-missing to see uncovered lines, or --cov-fail-under=80 to enforce a minimum coverage threshold in CI.

When should I use monkeypatch instead of mock in pytest?▼

Use monkeypatch for modifying environment variables, dictionary entries, or object attributes that pytest automatically reverts after the test. Use unittest.mock when you need to assert call counts, arguments, or simulate complex object behavior.