pytest-skill
Production-grade pytest guidance for Python tests: authoring, fixtures, parametrization, assertions, mocking, markers, discovery, plugins, hooks, debugging, output, warnings, doctests, CI, and migration from unittest. Use this skill whenever a request mentions pytest, conftest.py, fixtures, pytest.mark, parametrization, test discovery, pytest plugins/hooks, or asks how to write, run, debug, organize, or improve Python tests with pytest. For PySpark or Databricks-specific testing, also use pytest-databricks.
- License 许可
- MIT
- 最近更新
- 2026-09-13
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- pytest-skill
- license
- MIT
- metadata
- { "author": "TestMu AI", "source": "https://docs.pytest.org/en/stable/", "version": "2.0" }
- description
- Production-grade pytest guidance for Python tests: authoring, fixtures, parametrization, assertions, mocking, markers, discovery, plugins, hooks, debugging, output, warnings, doctests, CI, and migration from unittest. Use this skill whenever a request mentions pytest, conftest.py, fixtures, pytest.mark, parametrization, test discovery, pytest plugins/hooks, or asks how to write, run, debug, organize, or improve Python tests with pytest. For PySpark or Databricks-specific testing, also use pytest-databricks.
02 资源文件 3
- 参考文档reference/advanced-patterns.md20.8 KB
- 参考文档reference/official-patterns.md20.3 KB
- 参考文档reference/playbook.md36.2 KB
03 SKILL.md 正文
Pytest Skill
Use this as an implementation playbook, not as a reason to add dependencies or over-engineer a small test. Prefer the smallest pytest feature that gives a clear, isolated, behavior-focused test.
Operating workflow
- Inspect before editing. Read the target module, nearby tests,
conftest.pyfiles, and the active pytest configuration. Checkpytest --versionand installed plugins when behavior may be version- or plugin-dependent. - Classify the test. Choose pure unit, integration, subprocess/CLI, async, plugin, doctest, or compatibility testing. Do not use a real network, cloud resource, clock, or random source in a unit test unless that behavior is explicitly the subject of the test.
- Choose the narrowest mechanism. Organize pytest tests in a dedicated
Test*class for the production function or class under test, and use plainassertstatements within its test methods; use fixtures for dependencies/lifecycle;parametrizefor known static decision tables;subtestsonly for dynamic cases discovered during execution;hypothesisfor invariant fuzzing; and mocks only at an external boundary. Control clocks withtime-machinerather thanfreezegunortime.sleep(). - Make isolation explicit. Prefer function-scoped fixtures,
tmp_path, and transactional rollback savepoints for databases (join_transaction_mode="create_savepoint"). Split state-changing setup into small yield fixtures with teardown immediately afteryield. Avoid hiddenautousestate unless it is truly global test policy. - Verify in layers. Run collection first, the focused test, the relevant test directory/marker, and then the full suite when practical. Report skipped, xfailed, warnings, and deselected tests rather than hiding them.
Read the reference that matches the task:
| Need | Reference |
|---|---|
| Official how-to, pytest 8/9 features (RaisesGroup, subtests, @argument-file, colored diffs) | reference/official-patterns.md |
| Deep fixture, assertion, cache, configuration, CI, and plugin playbook | reference/playbook.md |
| Advanced architectures: typed factories, SQLAlchemy 2 savepoint rollback, pytest-asyncio 0.24+, time-machine, hypothesis, syrupy snapshots, xdist worker isolation | reference/advanced-patterns.md |
For Spark, Databricks Connect, dbutils, WorkspaceClient, or
databricks-labs-pytester, load pytest-databricks as well; do not replace its
environment-specific guidance with generic mocks.
Core test patterns
Project test organization, paths, and documentation
Apply these conventions to all pytest code unless the project has an explicit, documented exception:
-
Separate test suites into
tests/unit/andtests/integration/. Put shared fixtures, hooks, and pytest configuration intests/conftest.py, and reuse those fixtures rather than recreating common setup in individual test files. -
Treat
pytestconfig.rootpathas the base for test resources. Construct paths withpathlib.Path, never hard-code absolute paths, and avoid.resolve()when it would bind the test to a machine-specific layout. Define reusable resource-directory fixtures intests/conftest.pyand inject them where needed:from pathlib import Path import pytest @pytest.fixture def fixtures_dir(pytestconfig) -> Path: return pytestconfig.rootpath / "tests" / "fixtures"A test should then compose its resource path from the injected fixture, for example
data_path = fixtures_dir / "data.json". -
Begin every test file with a module docstring that identifies the component, the covered scenarios, and whether the file contains unit or integration tests. Give every test method a concise docstring stating the behavior under test, its input or setup, its expected behavior, and the regression or contract it protects.
-
Organize tests by production symbol. Use one dedicated
Test*class for each production function or class, with only the relevant cases for that symbol in the class. Do not write standalonetest_*functions. Name classes and methods with pytest conventions and observable behavior, such asTestParseEmail,test_returns_expected_value, andtest_raises_for_invalid_input.
The expected layout is:
tests/
├── conftest.py
├── unit/
└── integration/
Keep unit tests isolated and deterministic. Use integration tests when a case depends on multiple components, external services, databases, filesystems, or infrastructure.
Plain tests and assertion introspection
import pytest
class TestTotal:
def test_includes_tax(self):
"""Checks subtotal 10 at 20 percent tax returns 12, protecting tax arithmetic."""
assert total(subtotal=10, tax_rate=0.2) == 12
class TestParseEmail:
def test_reports_invalid_input(self):
"""Checks invalid email input raises its validation error, preserving the input contract."""
with pytest.raises(ValueError, match=r"invalid email") as exc_info:
parse_email("not-an-email")
assert exc_info.value.args[0] == "invalid email"
class TestCalculateRatio:
def test_returns_approximate_float_result(self):
"""Checks one divided by three is accurate within tolerance, guarding float precision."""
assert calculate_ratio(1, 3) == pytest.approx(1 / 3, rel=1e-6)
Use assert left == right, not self.assertEqual; pytest rewrites asserts to
show useful diffs for strings, sequences, mappings, sets, and expressions.
Add a message only when it adds domain context. Use pytest.fail() for a
failure discovered by control flow, not as a replacement for normal asserts.
For modern Python exception groups, use pytest.RaisesGroup and
pytest.RaisesExc when the group structure matters. Do not assert only that an
exception group contains one expected exception if additional unexpected
exceptions would make the test unsafe.
Fixtures: dependency injection and lifecycle
import pytest
@pytest.fixture
def user_factory():
created = []
def make_user(name="Alice"):
user = {"name": name}
created.append(user)
return user
yield make_user
# Replace this with real cleanup when the fixture creates resources.
created.clear()
class TestUserFactory:
def test_creates_user_with_given_name(self, user_factory):
"""Checks the fixture factory creates Bob data, preserving fixture-provided setup."""
assert user_factory("Bob") == {"name": "Bob"}
Rules that prevent most fixture bugs:
- A test requests a fixture by naming it as an argument; fixtures can request other fixtures and pytest caches one instance per test/scope.
- Use
functionscope by default. Useclass,module,package, orsessiononly when sharing is safe and materially reduces setup cost. - A broader-scoped fixture cannot depend on a narrower-scoped fixture
(
ScopeMismatch). A callablescope=can select scope from a CLI option. - Prefer one state-changing action per yield fixture. Teardown runs in reverse
dependency order;
addfinalizeris useful when cleanup must be registered conditionally after setup succeeds. - Use factory fixtures when one test needs multiple independently-created
objects. Do not call a fixture function directly; compose it or use
request.getfixturevalue()only for genuinely dynamic lookup. - Use
request.node,request.param,request.config, and markers for carefully scoped dynamic behavior. Keep the normal dependency graph explicit. - Put shared fixtures in the nearest appropriate
conftest.py. A closerconftest.pycan override an upstream fixture;usefixturesis for setup whose returned value is not needed by the test.
autouse=True is appropriate for narrowly-scoped global policy such as
resetting process state or installing a safety guard. It is usually a smell for
data setup because it hides why a test depends on that state.
Parametrization and test IDs
import pytest
class TestNormalizeName:
@pytest.mark.parametrize(
"raw,expected",
[
pytest.param(" Alice ", "Alice", id="trimmed"),
pytest.param("", "", id="empty"),
pytest.param(None, None, marks=pytest.mark.xfail(reason="pending API decision"), id="none"),
],
)
def test_returns_normalized_value(self, raw, expected):
"""Checks named inputs normalize as expected, documenting API edge-case behavior."""
assert normalize_name(raw) == expected
- Give meaningful
idsfor domain cases; IDs become part of node IDs and make failures/selective reruns understandable. - Stack decorators for a Cartesian product only when every combination is meaningful. Prefer explicit case objects when the matrix would explode.
- Use
indirect=Trueto pass a value into a fixture, and fixtureparams=[...]when the fixture itself is the varying dependency. - Parameter values are passed as-is, not copied. Never mutate shared list/dict parameters; construct fresh values or use immutable case data.
- Apply
pytest.param(..., marks=...)for a single-case skip/xfail. - Use
pytest_generate_tests(metafunc)for collection-time cases from CLI options or checked-in data. Decide and document the empty-case policy.
Use subtests (pytest 9+) when cases are discovered only during test
execution and all failures should be reported in one test. Parametrization is
better when cases should be collected, selected by node ID, rerun individually,
or handled by --last-failed.
Mocking and monkeypatching
Patch the name looked up by the system under test, not necessarily the name in the dependency's original module:
class TestFetchUser:
def test_returns_user_from_http_response(self, mocker):
"""Checks a mocked user response returns Ada, protecting the HTTP boundary contract."""
response = mocker.Mock(status_code=200)
response.json.return_value = {"id": 7, "name": "Ada"}
mocker.patch("myapp.users.requests.get", return_value=response)
assert fetch_user(7)["name"] == "Ada"
class TestLoadSettings:
def test_reads_test_environment(self, monkeypatch):
"""Checks test environment variables configure settings, preventing process-state leakage."""
monkeypatch.setenv("APP_ENV", "test")
monkeypatch.delenv("OPTIONAL_FLAG", raising=False)
assert load_settings().environment == "test"
- Use
pytest-mock'smockerforpatch,spy,stub, and automatic mock cleanup; useunittest.mockwhen the project already standardizes on it. - Use
monkeypatch.setattr/delattr,setitem/delitem,setenv/delenv,syspath_prepend,chdir, andmonkeypatch.context()for reversible local changes.raising=Falseis intentional only when absence is valid. - Prefer dependency injection or a small fake when it makes the boundary
clearer. Use
spec/autospecwhere appropriate so a mock cannot invent an invalid API. Assert important observable calls, not every implementation detail. - For async calls use
AsyncMockand await the system under test; do not make a synchronous mock pretend to be awaitable.
Markers, skips, and xfails
Register custom markers and run with --strict-markers:
[tool.pytest.ini_options]
markers = [
"unit: fast isolated tests",
"integration: uses a real service or local integration runtime",
"slow: takes longer than the normal test budget",
]
import pytest
class TestDatabaseRoundTrip:
@pytest.mark.integration
def test_persists_and_reads_data(self, database):
"""Checks the integration database round trip, protecting multi-component persistence."""
...
class TestGpuPath:
@pytest.mark.skipif(not HAS_GPU, reason="requires GPU")
def test_uses_gpu_when_available(self):
"""Checks GPU-capable setup follows the GPU path, preserving optional hardware support."""
...
class TestKnownBug:
@pytest.mark.xfail(strict=True, raises=KnownBug, reason="issue #123")
def test_exposes_known_bug(self):
"""Exercises the known failing setup, ensuring the tracked defect remains visible."""
...
Use pytest.importorskip("optional_pkg", minversion="...") for optional
dependencies. Use imperative pytest.skip()/pytest.xfail() only when the
condition is known during setup or execution. Make xfails strict in CI when an
unexpected pass should force removal of stale bug metadata. Never use skip or
xfail to conceal an ordinary failing test.
Files, output, logs, and warnings
- Use
tmp_path(pathlib.Path) for per-test files andtmp_path_factoryfor expensive session-shared artifacts. Use--basetemponly with a disposable directory because it is cleared before the run. - Use
capsysfor Python-level text output,capsysbinaryfor bytes,capfdorcapfdbinaryfor subprocess/file-descriptor output, andcapsys.disabled()for a short intentionally-live block. - Use
caplog.at_level()/set_level(),caplog.records,record_tuples, andcaplog.clear()to test logging behavior. Configure live/file logging only when it helps the workflow; do not assert on formatting unless formatting is the contract. - Use
pytest.warns()to assert an expected warning andrecwarnto inspect all warnings. Treat unexpected deprecations as errors in CI, with narrow, documented filters for intentionally legacy behavior.
Async, CLI, doctest, and unittest compatibility
- For async tests, use
pytest-asyncio(0.24+) with explicit loop scoping: configureasyncio_default_fixture_loop_scope = "function"inpyproject.tomland align fixture/testloop_scopeto prevent event loop mismatch errors. Usepytest.RaisesGroupto test concurrent task failures insideasyncio.TaskGroup. - For a CLI, prefer invoking the real entry point in a subprocess when process
boundaries, exit codes, or stdout/stderr matter; use
capsysfor a direct function-level CLI test. - Run docstrings with
--doctest-modulesand text examples with--doctest-glob. Usedoctest_optionflags,doctest_namespace,getfixture('fixture_name'), and--doctest-continue-on-failuredeliberately. - pytest runs
unittest.TestCasesuites, including their setup methods and skips. During migration, retain compatibility first; do not assume normal pytest fixture arguments or parametrization work insideTestCasemethods. New pytest tests should normally use fixtures and plain asserts.
Discovery and command cookbook
# Run and select
python -m pytest # current interpreter; adds cwd to sys.path
pytest tests/ # directory
pytest tests/test_api.py::TestUsers::test_create
pytest 'tests/test_api.py::test_parse[empty]' # quote [] in shells that expand it
pytest -k 'login and not slow'
pytest -m 'unit and not integration'
pytest --pyargs installed_package.tests
pytest @tests-to-run.txt # one path/node/option per line (pytest 8.2+)
# Understand collection/configuration
pytest --version
pytest --collect-only -q
pytest --fixtures
pytest --markers
pytest --trace-config # active plugins and conftest files
pytest --setup-show tests/test_api.py
pytest --setup-plan # inspect fixture plan without running
# Tight feedback loop
pytest -x --maxfail=3
pytest --lf # only last failures; use --lfnf=none if desired
pytest --ff # failures first, then all tests
pytest --nf # newest files first
pytest --cache-show
pytest --cache-clear
pytest --durations=10 --durations-min=1.0
# Debug/report
pytest -x --pdb
pytest --trace
pytest -l --tb=short
pytest --full-trace
pytest -ra --show-capture=all
pytest --junitxml=reports/junit.xml # CI report
-n auto, --cov, --html, --timeout, --reruns, and stepwise options
come from plugins. Confirm the plugin is installed and active before adding
them to project defaults. Use pytest -p no:PLUGIN or
PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 to diagnose plugin interference.
When invoking from Python, call pytest.main([...]) with explicit arguments
and inspect its return value. Avoid calling pytest.main() repeatedly in one
process because imported test modules remain cached.
Configuration baseline
Prefer the project's existing configuration format. A conservative baseline is:
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = ["-ra", "--strict-markers", "--strict-config", "--tb=short"]
markers = [
"unit: fast isolated tests",
"integration: tests using external or integration resources",
"slow: tests outside the normal runtime budget",
]
Add coverage, async, timeout, logging, doctest, or warning policy only when the
corresponding dependency and project policy exist. Do not blindly combine
-v and -q, or make every local run stop at the first failure. Check the
actual configuration source with pytest --help and the session header; pytest
configuration files are not merged arbitrarily.
Plugin and hook work
Use a local conftest.py for project-only fixtures and hooks. Use a package
plugin when behavior is reusable across projects. Register a plugin through a
pytest11 entry point when it must be installable. Test plugins with the
built-in pytester fixture by creating temporary test files and asserting
outcomes.
Safe hook rules:
- Hook argument names are validated and optional arguments can be omitted.
- Most non-test-running hooks should not raise; turn user/config errors into a
clear
pytest.UsageErroror a controlled report. - Use
@pytest.hookimpl(tryfirst=True/trylast=True)only when ordering is part of the design. Hook wrappers are generator functions that yield exactly once. - For custom collection, use public
pytest.File,pytest.Item,pytest.Directory,pytest_collect_file, orpytest_collect_directoryAPIs; implementruntest,repr_failure, andreportinfofor useful failures. - Use
pytest_collection_modifyitemsfor collection-time marking/deselection, but keep policy visible and test it.pytest_assertrepr_compareandpytest.register_assert_rewrite()are advanced tools for shared helpers.
Quality gates and anti-patterns
- Name tests after behavior and keep Arrange–Act–Assert readable.
- Test public behavior and contracts, not private call order.
- Keep tests independent, deterministic, and safe to run in any order or with xdist. Seed or inject randomness and control time rather than sleeping.
- Separate unit/integration/e2e markers and make external-resource tests opt-in or skip with an explicit reason.
- Avoid mutable module globals, broad
except Exception, test ordering, unconditional network calls, hard-coded temp paths, over-mocking, and tests with no meaningful assertion. - Treat retries as a last-resort quarantine for diagnosed environmental flakiness; fix the race or isolation problem instead of masking it.
Official coverage map
The companion reference was refreshed against the current pytest stable docs:
- How-to: invocation, assertions, fixtures, marks, parametrization, subtests, temporary paths, monkeypatch, doctests, cache, failures, output, logging, capture, warnings, skip/xfail, plugins, plugin writing/hooks, existing suites, unittest, xunit setup, and bash completion.
- Examples: failure reports, basic patterns, parametrization, custom marks, collection-aware session fixtures, custom discovery, non-Python collectors, and custom directory collectors.
Source index: https://docs.pytest.org/en/stable/how-to/index.html and https://docs.pytest.org/en/stable/example/index.html.
04 同仓库更多 Skills
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.
book-to-skill
Converts books and documents (PDF, EPUB, DOCX, HTML, Markdown, plain text, RTF, MOBI/AZW with Calibre) into structured agent skills, extracting frameworks, mental models, principles, techniques, and anti-patterns. Use when the user wants to study a document through GitHub Copilot CLI, Amp, Claude Code, or Hermes Agent, apply an author's frameworks while working, or build a reusable knowledge base from a file.
mongodb-search-and-ai
Guides MongoDB users through implementing and optimizing Atlas Search (full-text), Vector Search (semantic), and Hybrid Search solutions. Use this skill when users need to build search functionality for text-based queries (autocomplete, fuzzy matching, faceted search), semantic similarity (embeddings, RAG applications), or combined approaches. Also use when users need text containment, substring matching ('contains', 'includes', 'appears in'), case-insensitive or multi-field text search, or filtering across many fields with variable combinations. Provides workflows for selecting the right search type, creating indexes, constructing queries, and optimizing performance using the MongoDB MCP server.
mongodb-schema-design
MongoDB schema design patterns and anti-patterns. Use when designing data models, reviewing schemas, migrating from SQL, or troubleshooting performance issues caused by schema problems. Triggers on "design schema", "embed vs reference", "MongoDB data model", "schema review", "unbounded arrays", "one-to-many", "tree structure", "16MB limit", "schema validation", "JSON Schema", "time series", "schema migration", "polymorphic", "TTL", "data lifecycle", "archive", "index explosion", "unnecessary indexes", "approximation pattern", "document versioning".
mongodb-query-optimizer
Help with MongoDB query optimization and indexing. Use only when the user asks for optimization or performance: "How do I optimize this query?", "How do I index this?", "Why is this query slow?", "Can you fix my slow queries?", "What are the slow queries on my cluster?", etc. Do not invoke for general MongoDB query writing unless user asks for performance or index help. Prefer indexing as optimization strategy. Use MongoDB MCP when available.
mongodb-pymongo
Use for Python applications that use PyMongo 4.x or BSON: MongoClient or AsyncMongoClient, Database and Collection APIs, CRUD, cursors, aggregation, transactions, change streams, BSON/Extended JSON/ObjectId/UUID/datetime handling, CodecOptions and custom codecs, indexes, Database.command(), Atlas Search APIs, driver monitoring, or Python logging. Invoke whenever implementing, reviewing, debugging, or migrating PyMongo or Motor code, even when the user only mentions a Python MongoDB client, `bson`, or `AsyncMongoClient`. For pool sizing and cross-driver connection tuning, also load `mongodb/connection`; for query-plan/index performance, load `mongodb/query-optimizer`; for Search or Vector Search solution design, load `mongodb/search-and-ai`.