home / skills / xfstudio / skills / python-testing-patterns

python-testing-patterns skill

/python-testing-patterns

This skill helps you implement robust Python testing strategies with pytest, fixtures, mocks, and TDD across unit, integration, and async tests.

npx playbooks add skill xfstudio/skills --skill python-testing-patterns

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
1.3 KB
---
name: python-testing-patterns
description: Implement comprehensive testing strategies with pytest, fixtures, mocking, and test-driven development. Use when writing Python tests, setting up test suites, or implementing testing best practices.
---

# Python Testing Patterns

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

## Use this skill when

- 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

## Do not use this skill when

- The task is unrelated to python testing patterns
- You need a different domain or tool outside this scope

## Instructions

- Clarify goals, constraints, and required inputs.
- Apply relevant best practices and validate outcomes.
- Provide actionable steps and verification.
- If detailed examples are required, open `resources/implementation-playbook.md`.

## Resources

- `resources/implementation-playbook.md` for detailed patterns and examples.

Overview

This skill implements comprehensive Python testing strategies using pytest, fixtures, mocking, parameterization, and test-driven development. It helps teams design reliable unit, integration, and async tests and to integrate testing patterns into CI/CD pipelines. The focus is practical patterns that reduce flakiness and speed maintenance.

How this skill works

The skill inspects project structure and recommends testing layouts, fixture scopes, and parameterization to maximize coverage and minimize duplication. It provides patterns for mocking external dependencies, testing async code, and writing property-based and database tests. It also guides CI test orchestration and TDD workflows with concrete verification steps.

When to use it

  • Writing unit tests for Python modules and packages
  • Setting up or refactoring a test suite and test conventions
  • Implementing test-driven development (TDD) for new features
  • Creating integration tests for APIs, services, or databases
  • Mocking external services or testing async and concurrent code

Best practices

  • Prefer small, focused tests that assert one behavior per test
  • Use pytest fixtures for setup/teardown and control scope (function/module/session)
  • Mock external services and I/O at boundaries; keep unit tests deterministic
  • Parameterize tests to cover input variations instead of copy-pasting cases
  • Add integration tests for contracts and end-to-end flows, keep them separate from unit tests
  • Run tests in CI with parallelization and fail-fast settings; collect coverage and flaky-test reports

Example use cases

  • Start a TDD cycle: add a failing pytest, implement minimal code, refactor, repeat
  • Use fixtures to create temporary databases or test clients with session-scoped setup
  • Mock HTTP clients and external APIs to assert request formation and error handling
  • Write async tests with pytest-asyncio for coroutine-based code and verify concurrency behavior
  • Parameterize edge cases like boundary inputs and invalid data to ensure robust validation

FAQ

How do I choose fixture scope?

Use function scope for isolated tests, module scope for shared expensive setup across related tests, and session scope for global resources that are expensive to create.

When should I mock vs run integration tests?

Mock dependencies in unit tests to keep them deterministic; add integration tests to validate real interactions and contracts separately.

How to reduce flaky tests?

Avoid shared mutable state, use explicit waits or deterministic synchronization for async code, isolate external resources, and capture environment-dependent behavior with targeted mocks.