skillfed

python-testing-patterns

Learn to build reliable Python tests using pytest, fixtures, mocking, and test-driven development. This skill covers test organization, naming conventions, retry logic, time control with freezegun, markers, and coverage reporting to help you write maintainable test suites.

Python Testing Patterns helps you write and organize unit tests for Python code using pytest, fixtures, and mocking.

AI-generated summary based on this skill's SKILL.md

38,308 4,097 MIT updated by wshobson

Install

wshobson/agents/python-testing-patterns · repository language: Python

git clone https://github.com/wshobson/agents
cp -r agents/plugins/python-development/skills/python-testing-patterns ~/.claude/skills/python-testing-patterns
npx skillfed install wshobson/agents/python-testing-patterns

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

How to write pytest tests in Python?

python-testing-patterns teaches you to write pytest tests by starting with simple test functions prefixed with `test_`, organizing them into classes, and using assertions. You'll learn naming conventions, test discovery, how to structure test files alongside your code, and best practices for readable, maintainable tests that catch real bugs.

What are pytest fixtures and how do I use mocking?

python-testing-patterns covers pytest fixtures as reusable test setup components defined with `@pytest.fixture` that eliminate duplication. For mocking, you'll learn to replace external dependencies (APIs, databases, files) with stubs using `unittest.mock` or `pytest-mock`, isolate units under test, and verify interactions without side effects.

How do I set up test suites with pytest?

python-testing-patterns shows how to organize test suites by grouping related tests in classes or modules, using conftest.py for shared fixtures, applying pytest markers to categorize tests, and configuring pytest.ini or pyproject.toml for discovery rules. You'll structure tests for unit, integration, and end-to-end layers.

How can I improve code coverage and test quality?

python-testing-patterns helps you measure coverage with pytest-cov, identify untested branches, and set coverage thresholds in CI/CD. You'll learn to write meaningful assertions, avoid flaky tests through proper isolation and cleanup, use parameterization to test multiple scenarios, and apply test-driven development to guide design.

What debugging techniques help when tests fail?

python-testing-patterns teaches debugging failing tests by using pytest's `-vv` flag for detailed output, `--pdb` for interactive debugging, and `--lf` to rerun last failures. You'll learn to isolate test dependencies, control time with freezegun, verify mock calls, and recognize test isolation issues that cause flakiness.

How does python-testing-patterns support test-driven development?

python-testing-patterns emphasizes writing tests before code, using red-green-refactor cycles, and organizing tests to drive API design. You'll learn integration testing for Python APIs, async testing with pytest-asyncio, property-based testing for edge cases, and database testing patterns that support TDD workflows.

SKILL.md

rendered from the published skill — quoted content, verbatim

Python Testing Patterns

Comprehensive guide to implementing robust testing strategies in Python using pytest, fixtures, mocking, parameterization, and test-driven development practices.

When to Use This Skill

  • Writing unit tests for Python code
  • Setting up test suites and test infrastructure
  • Implementing test-driven development (TDD)
  • Creating integration tests for APIs and services
  • Mocking external dependencies and services
  • Testing async code and concurrent operations
  • Setting up continuous testing in CI/CD
  • Implementing property-based testing
  • Testing database operations
  • Debugging failing tests

Core Concepts

1. Test Types
  • Unit Tests: Test individual functions/classes in isolation
  • Integration Tests: Test interaction between components
  • Functional Tests: Test complete features end-to-end
  • Performance Tests: Measure speed and resource usage
2. Test Structure (AAA Pattern)

-

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 3 files
plugins/python-development/skills/python-testing-patterns/SKILL.md
plugins/python-development/skills/python-testing-patterns/references/advanced-patterns.md
plugins/python-development/skills/python-testing-patterns/references/details.md

Related skills

Tags

unit-testing test-automation mock-objects code-coverage test-infrastructure continuous-integration quality-assurance behavior-verification test-fixtures