python-pytest-patterns_skill

This skill analyzes Python pytest patterns to improve test reliability by illustrating fixtures, parametrization, and mocks with concrete examples.
  • Shell

8

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill 0xdarkmatter/claude-mods --skill python-pytest-patterns

  • SKILL.md4.9 KB

Overview

This skill packages modern pytest testing patterns for Python to help teams write reliable, maintainable tests. It collects fixture templates, parametrization techniques, exception and marker usage, mocking patterns, and conftest best practices. Use it to standardize test structure, speed up test creation, and improve coverage reporting.

How this skill works

The skill inspects common pytest constructs and provides ready-to-use patterns and snippets: fixtures with different scopes, parametrized tests, exception assertions, marker usage (skip/skipif/xfail/custom), and mocking with unittest.mock or pytest-mock. It also includes conftest conventions, quick CLI references, and scripts to run or scaffold tests. Patterns are organized so you can copy templates, adapt scopes, and register custom markers in pytest.ini.

When to use it

  • Bootstrapping a new test suite or project
  • Standardizing fixtures and shared test state via conftest.py
  • Writing parameterized or combinatorial tests to cover inputs efficiently
  • Testing error handling with pytest.raises and regex matching
  • Mocking external dependencies or patching modules during unit tests
  • Configuring markers and selective test runs for slow/integration tests

Best practices

  • Prefer fixtures over setUp/tearDown for clarity and reuse; choose scope to minimize setup cost
  • Use pytest.mark.parametrize for table-driven tests instead of loops inside tests
  • Register custom markers in pytest.ini to avoid warnings and enable selective runs
  • Keep conftest.py minimal and focused on shared fixtures; avoid heavy test logic there
  • Use pytest-mock (mocker) or unittest.mock.patch for deterministic unit tests and assert calls/side effects
  • Run tests with coverage and parallelism (pytest-cov, pytest-xdist) for faster feedback

Example use cases

  • Create a session-scoped app fixture in conftest.py for integration tests with a test client
  • Parametrize input/expected pairs to validate pure functions across many cases
  • Use pytest.raises with match to ensure specific error messages are produced
  • Patch external APIs in unit tests to simulate network responses without hitting real services
  • Mark slow or flaky tests and run them separately using pytest -m

FAQ

Set the fixture scope to module, class, or session depending on reuse. Use function scope for isolated tests and session scope for shared resources like an app or DB connection.

When should I use mocker.patch vs unittest.mock.patch?

Use pytest-mock's mocker.patch for concise fixtures-friendly syntax and better integration with pytest; use unittest.mock.patch when you prefer standard-library tooling or explicit import behavior.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
python-pytest-patterns skill by 0xdarkmatter/claude-mods | VeilStrat