Why Register Custom Marks in pytest.ini
Registering custom marks in pytest.ini provides strict
validation, improves test suite maintainability, and prevents silent
execution failures in Python projects. This article explains the
technical purpose of defining markers in configuration files, how it
prevents typos, enables selective test execution, and enforces
consistent test organization across development teams and CI/CD
pipelines.
Preventing Typos and Warnings
When you use a custom marker such as @pytest.mark.smoke
or @pytest.mark.integration without prior registration,
pytest raises a PytestUnknownMarkWarning. A simple
typo—such as writing @pytest.mark.smok instead of
@pytest.mark.smoke—will not trigger a syntax error by
default. Pytest will simply apply the misspelled marker, causing the
test to be unintentionally skipped when running targeted commands like
pytest -m smoke. Registering valid markers in
pytest.ini allows pytest to distinguish between legitimate
tags and accidental typos.
Enforcing Strict Mark Usage in CI/CD
To make test suites robust, teams often pair mark registration with
the --strict-markers flag (or
addopts = --strict-markers in pytest.ini).
Under this setting, any test tagged with an unregistered marker causes
pytest to fail immediately with an error rather than emitting a
ignorable warning. This ensures that:
- Every marker used across the codebase is intentional and documented.
- Developers do not introduce ad-hoc, uncoordinated tags.
- Pull requests with misspelled markers fail fast in automated pipelines before merging.
Selective and Dynamic Test Execution
Markers act as metadata used to slice large test suites into functional subsets. Registering marks provides an organized taxonomy for running specific groups of tests depending on the environment:
- Smoke Tests: Quickly validating critical paths
(
pytest -m smoke). - Performance/Slow Tests: Isolating long-running
tests (
pytest -m "not slow") for fast local feedback while running the full suite on nightly builds. - External Dependencies: Tagging tests that rely on external APIs, databases, or third-party services.
Discoverability and Documentation
Registering marks centralizes test metadata in a readable format.
Each entry in pytest.ini allows you to attach a
human-readable description:
[pytest]
markers =
smoke: Quick core feature validation tests.
slow: Long-running tests excluded from standard PR runs.
integration: Tests requiring external services or databases.Running the command pytest --markers parses this
configuration and outputs a clean list of all available markers
alongside their descriptions. This provides built-in documentation for
new team members, eliminating guesswork about which tags exist and what
they represent.