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
Decision gist · record as of 2026-07-10
pytest helps you write well-structured tests using fixtures, parametrize, and clear naming conventions. 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.
Use it when
- pytest fixtures are reusable test components that handle setup and teardown via dependency injection.
- pytest provides monkeypatch fixture for runtime patching: monkeypatch.setattr(target, value) replaces objects.
Verify before relying
Read SKILL.md below before installing (6 files). Open directory: indexed for reading, not audited.
Install
xobotyi/cc-foundry/pytest · repository language: JavaScript
Open directory. Skills are indexed for reading, not audited. Review a skill's body before installing it.
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)
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
Let your AI agent find skills like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 56,283 agent skills by what they can do, searchable in plain language.
wish › “Write and structure pytest tests following best practices”
Give your agent the search over MCP, or paste the wish link into any chat. No install? Search from any chat →
Related skills
Learn to build robust test suites using pytest with fixtures at multiple scopes, parameterized test cases, and mocking strategies. This skill covers async testing patterns, FastAPI application testing, and property-based testing approaches to ensure comprehensive coverage and maintainable test code.
pytest-skill equips you with patterns for writing robust Python tests using pytest's core features: fixtures for setup and teardown, parametrize for data-driven testing, markers for test organization, and mocking for external dependencies. It covers conftest configuration, exception handling, and best practices for production environments.
Test Harness builds complete pytest test files from source code, automatically structuring tests across happy paths, edge cases, and error states. It designs fixtures by scope, applies focused mocks to external dependencies, and handles async patterns—all grounded in reconnaissance of your code's complexity and existing test coverage.
Master pytest fundamentals through opinionated patterns: atomic tests that verify single behaviors, AAA structure (Arrange, Act, Assert), and descriptive naming that pinpoints failures. Learn when to parameterize variations, how to organize test files alongside modules, and when mocking belongs in integration tests instead.
Pytest Patterns teaches you to write reliable Python tests using pytest's core features—fixtures for setup and teardown, parametrization to run tests with multiple inputs, and mocking to isolate dependencies. Learn test organization, coverage analysis, and CI/CD integration to scale from simple unit tests to complex test suites.
Master pytest for Python testing with guidance on fixtures, parametrization, markers, and mocking strategies. Learn test organization patterns, coverage best practices, and common commands to streamline your testing workflow.
More skills Pytest Configuration (unlicensed) · pytest (MIT) · python-testing-patterns (MIT) · python-testing (MIT) · python-testing-patterns (MIT) · pytest-config (MIT) · Setup Pytest Fixtures (unlicensed) · Textual Test Fixtures (unlicensed)