skillfed

pytest

Master pytest's core patterns for writing maintainable tests: fixtures for setup and teardown, parametrize for test variations, and monkeypatch for mocking. Learn test structure, granularity, and scope management to build fast, isolated unit tests that serve as executable documentation.

pytest helps you write well-structured tests using fixtures, parametrize, and clear naming conventions.

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

18 0 MIT updated by xobotyi

Install

xobotyi/cc-foundry/pytest · repository language: JavaScript

git clone https://github.com/xobotyi/cc-foundry
cp -r cc-foundry/plugins/python/skills/pytest ~/.claude/skills/pytest
npx skillfed install xobotyi/cc-foundry/pytest

Frequently asked questions

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

How to write pytest tests following best practices?

pytest tests follow a simple naming convention (test_*.py files, test_* functions) and the Arrange-Act-Assert pattern. pytest automatically discovers and runs matching test functions. Best practices include: keep tests focused on one behavior, use descriptive names, avoid test interdependencies, and leverage fixtures for setup. pytest's assertion introspection provides detailed failure messages without custom assertion helpers.

What are pytest fixtures and how do setup/teardown work?

pytest fixtures are reusable test components that handle setup and teardown via dependency injection. Define fixtures with @pytest.fixture decorator; tests request them as function parameters. Use yield for teardown code (runs after test completes). Fixture scope controls lifetime: function (default, per test), class, module, or session. Fixtures compose by requesting other fixtures, enabling complex dependency chains without boilerplate.

How do I mock in pytest using monkeypatch or unittest.mock?

pytest provides monkeypatch fixture for runtime patching: monkeypatch.setattr(target, value) replaces objects, monkeypatch.setenv() modifies environment. For complex mocking, use unittest.mock.Mock, MagicMock, or patch(). Mock objects track calls via call_count, call_args, assert_called_with(). Combine mocking with fixtures for reusable mock setups. Monkeypatch auto-reverts after tests, preventing side effects.

How can I parametrize tests to run multiple scenarios?

pytest parametrize decorator (@pytest.mark.parametrize) runs a test function multiple times with different inputs. Syntax: @pytest.mark.parametrize('param_names', [values]). Stack multiple decorators for cross-product testing. Use pytest_generate_tests hook for dynamic parametrization. Parametrized tests appear as separate items in reports, aiding failure isolation and coverage clarity.

What is pytest conftest.py and how do I configure it?

conftest.py is a special file pytest auto-discovers for shared fixtures, hooks, and configuration. Place it in project root or test directories; pytest loads it before running tests. Define reusable fixtures here for all tests in that directory tree. Use pytest_configure hook for startup logic, pytest_collection_modifyitems for marker handling. conftest enables test organization without coupling individual test files.

How do pytest markers, skip, and xfail work?

pytest markers (@pytest.mark.name) tag tests for selective execution and organization. Built-in markers: skip (unconditionally skip), skipif (conditional skip), xfail (expect failure). Custom markers require registration in pytest.ini. Run specific markers with -m flag. xfail marks tests expected to fail; pytest reports them separately, useful for known bugs. Markers enable flexible test filtering without code changes.

SKILL.md

rendered from the published skill — quoted content, verbatim

pytest

Test behavior, not implementation. Tests are executable documentation — if the test name doesn't explain what the code does, rewrite it.

pytest is Python's standard testing framework. It uses plain assert statements, fixtures for setup/teardown, and a rich plugin ecosystem. All patterns target Python 3.14+.

References

  • Fixture patterns, scope, factories, teardown — [${CLAUDE_SKILL_DIR}/references/fixtures.md]: Fixture lifecycle, yield fixtures, factory pattern, request object, parametrized fixtures
  • Parametrize patterns, indirect, IDs — [${CLAUDE_SKILL_DIR}/references/parametrize.md]: Multi-parameter examples, indirect fixtures, custom IDs, stacking decorators
  • Monkeypatch patterns, scoped patches — [${CLAUDE_SKILL_DIR}/references/monkeypatch.md]: API overview, attribute/env/dict patching, scoped monkeypatch, common recipes
  • Plugin ecosystem and configuration

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

Read as markdown · JSON record · Browse the source repository

File tree — 6 files
plugins/python/skills/pytest/.dev/reference-inventory.json
plugins/python/skills/pytest/SKILL.md
plugins/python/skills/pytest/references/fixtures.md
plugins/python/skills/pytest/references/monkeypatch.md
plugins/python/skills/pytest/references/parametrize.md
plugins/python/skills/pytest/references/plugins.md

Related skills

Tags

test-framework unit-testing mock-patching fixture-management parametrized-tests async-testing test-discovery assertion-rewriting plugin-ecosystem