Advanced pytest patterns including custom markers, plugins, hooks, parallel execution, and pytest-xdist. Use when implementing custom test infrastructure, optimizing test execution, or building reusable test utilities.
View on GitHubyonatangross/orchestkit
ork
January 25, 2026
Select agents to install to:
npx add-skill https://github.com/yonatangross/orchestkit/blob/main/plugins/ork/skills/pytest-advanced/SKILL.md -a claude-code --skill pytest-advancedInstallation paths:
.claude/skills/pytest-advanced/# Advanced Pytest Patterns
Master pytest's advanced features for scalable, maintainable test suites.
## Overview
- Building custom test markers for categorization
- Writing pytest plugins and hooks
- Configuring parallel test execution with pytest-xdist
- Creating reusable fixture patterns
- Optimizing test collection and execution
## Quick Reference
### Custom Markers
```toml
# pyproject.toml
[tool.pytest.ini_options]
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
"integration: marks tests requiring external services",
"smoke: critical path tests for CI/CD",
]
```
```python
import pytest
@pytest.mark.slow
def test_complex_analysis():
result = perform_complex_analysis(large_dataset)
assert result.is_valid
# Run: pytest -m "not slow" # Skip slow tests
# Run: pytest -m smoke # Only smoke tests
```
See [custom-plugins.md](references/custom-plugins.md) for plugin development.
### Parallel Execution (pytest-xdist)
```toml
[tool.pytest.ini_options]
addopts = ["-n", "auto", "--dist", "loadscope"]
```
```python
@pytest.fixture(scope="session")
def db_engine(worker_id):
"""Isolate database per worker."""
db_name = "test_db" if worker_id == "master" else f"test_db_{worker_id}"
engine = create_engine(f"postgresql://localhost/{db_name}")
yield engine
```
See [xdist-parallel.md](references/xdist-parallel.md) for distribution modes.
### Factory Fixtures
```python
@pytest.fixture
def user_factory(db_session) -> Callable[..., User]:
"""Factory fixture for creating users."""
created = []
def _create(**kwargs) -> User:
user = User(**{"email": f"u{len(created)}@test.com", **kwargs})
db_session.add(user)
created.append(user)
return user
yield _create
for u in created:
db_session.delete(u)
```
## Key Decisions
| Decision | Recommendation |
|----------|----------------|
| Parallel execution | pytest-xdist with `--dist loadscope` |
| Marker