pytest-patterns

Enforce pytest patterns for fixtures, parametrize, mocking, and async tests.

2|Updated Nov 13, 2025
One-click install
npx skills add https://github.com/ricardoroche/ricardos-claude-code --skill pytest-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytest-patterns
Source: https://github.com/ricardoroche/ricardos-claude-code/tree/main/.claude/skills/pytest-patterns
Command: npx skills add https://github.com/ricardoroche/ricardos-claude-code --skill pytest-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps ensure tests follow established pytest patterns and best practices, reducing flaky tests and simplifying maintenance.

Core Features & Use Cases

  • Basic Test Pattern: Structure tests with Arrange/Act/Assert and clear naming.
  • Async Test Pattern: Provide patterns for testing async code with pytest.mark.asyncio.
  • Fixtures: Demonstrates use and scope of fixtures (function, session, module) and teardown.
  • Parametrize for Multiple Test Cases: Run multiple inputs against a single test function.
  • Mocking: Use unittest.mock to mock dependencies within tests.

Quick Start

Apply pytest-patterns to your test file (e.g., test_api.py) to ensure fixtures, parametrize, and mocks follow best practices.

Frequently Asked Questions about pytest-patterns

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

FAQPage Schema
How do I structure pytest tests with fixtures and parametrize?

Pytest fixtures provide reusable setup and teardown across function, module, and session scopes, while parametrize runs multiple test cases against a single test function. Together they enforce consistent test structure and reduce duplication in your test suite.

What's the best way to mock dependencies in pytest?

Use unittest.mock to mock external dependencies within tests, ensuring isolation and predictable behavior. Proper mocking patterns prevent flaky tests and clarify what each test actually validates.

How do I test async code with pytest?

Apply pytest.mark.asyncio to async test functions to handle coroutine execution. This pattern ensures async code runs correctly within pytest's test lifecycle without blocking or race conditions.

Why are my pytest tests flaky and how do I fix them?

Flaky tests often stem from inconsistent fixture usage, improper mocking, or missing async patterns. Following established pytest best practices—correct scoping, dependency isolation, and proper async handling—reduces flakiness and improves maintainability.

Can I use Arrange/Act/Assert structure with pytest fixtures?

Yes. Arrange/Act/Assert pairs naturally with pytest fixtures: fixtures handle Arrange setup, your test body performs Act and Assert. This pattern keeps tests readable and maintainable across your suite.

Do I need to refactor existing tests to follow pytest best practices?

Incremental adoption is possible; pytest patterns apply to new tests immediately and can guide refactoring of older tests. Consistent patterns reduce maintenance burden and catch issues earlier.