pytest-patterns

Apply pytest patterns for fixtures, mocking, and deterministic time control.

3|Updated Mar 1, 2010
One-click install
npx skills add https://github.com/harleypig/dotfiles --skill pytest-patterns-harleypig
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytest-patterns
Source: https://github.com/harleypig/dotfiles/tree/main/config/claude/skills/pytest-patterns
Command: npx skills add https://github.com/harleypig/dotfiles --skill pytest-patterns-harleypig

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Concrete pytest depth for Python tests including fixtures, scope, yield-teardown, factory-as-fixture, conftest hierarchy, and disciplined mocking to reduce flakiness and maintenance costs.

Core Features & Use Cases

  • Fixture design and scope control: function/module/session scopes with safe teardown.
  • Factory-as-fixture patterns: generate configurable test data on demand.
  • Conftest hierarchy and patching discipline: promote reuse and patch at the right boundary (use-site patching and autospec).
  • Time and randomness freezing: deterministic tests with controlled environments.
  • Real-world guidance: apply to APIs, DB layers, and integration tests.

Quick Start

Apply the patterns to organize tests with scoped fixtures, conftest hierarchies, proper mocking, and deterministic time control.

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 fixtures with proper teardown and scope control?

Use factory-as-fixture patterns in pytest to generate configurable test data on demand, yielding dynamic fixtures directly within test functions for flexible data isolation.

When should I use AsyncMock versus MagicMock in pytest?

Use AsyncMock for asynchronous Python code and MagicMock for synchronous code in pytest, enforcing autospec or spec settings to ensure mocks match the real API signatures.

What is the best way to patch dependencies and organize conftest hierarchies in pytest?

Organize pytest tests with conftest hierarchies for reuse and apply patching discipline at the use-site boundary with autospec to prevent leaking mocks and reduce maintenance costs.

How do I freeze time and randomness for deterministic pytest tests?

Freeze time and control randomness in pytest tests using deterministic time-freezing techniques to create controlled environments that eliminate flaky test failures from timing issues.

Can I use these pytest mocking and fixture patterns for integration and API tests?

Apply these pytest patterns to integration tests, API layers, and DB layers to enforce fixture scope guidelines, proper patching strategies, and reliable teardown across Python projects.