| name | pytest |
| description | Comprehensive pytest testing framework skill for Python. Use when working with pytest tests, fixtures, parametrization, markers, assertions, configuration, plugins, or test discovery. Covers pytest 9.x with full API reference, how-to guides, best practices, and examples for writing, running, and debugging Python tests. |
pytest Testing Framework
pytest is a mature, full-featured Python testing framework that makes it easy to write small, readable tests that can scale to support complex functional testing.
Quick Start
Installation
pip install -U pytest
pytest --version
Basic Test
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
Run with pytest - it auto-discovers test_*.py and *_test.py files.
Test Discovery Rules
- Files:
test_*.py or *_test.py
- Functions:
test_ prefix outside classes
- Classes:
Test prefix (no __init__)
- Methods:
test_ prefix in Test classes
Core Concepts
Fixtures
Fixtures provide fixed baseline for tests:
import pytest
@pytest.fixture
def smtp_connection():
return smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
def test_ehlo(smtp_connection):
response, msg = smtp_connection.ehlo()
assert response == 250
Scopes: function (default), class, module, package, session
Parametrization
Run same test with multiple inputs:
@pytest.mark.parametrize("input,expected", [
("3+5", 8),
("2+4", 6),
pytest.param("6*9", 42, marks=pytest.mark.xfail),
])
def test_eval(input, expected):
assert eval(input) == expected
Markers
@pytest.mark.skip(reason="not implemented")
@pytest.mark.skipif(sys.platform == "win32", reason="linux only")
@pytest.mark.xfail(reason="known bug")
@pytest.mark.slow
def test_something():
pass
Run by marker: pytest -m slow
Assertions
assert func(3) == 5
with pytest.raises(ValueError, match="invalid"):
raise ValueError("invalid input")
assert 0.1 + 0.2 == pytest.approx(0.3)
with pytest.warns(UserWarning):
warnings.warn("hello", UserWarning)
CLI Reference
pytest
pytest test_mod.py
pytest tests/
pytest -k "MyClass and not method"
pytest -m slow
pytest -x
pytest -v
pytest -s
pytest --tb=short
pytest --fixtures
pytest --collect-only
pytest -n 4
Documentation Index
Getting Started
How-To Guides
Testing Basics:
- how-to/usage.md - Invoking pytest, selecting tests, CLI options
- how-to/assert.md - Assertions, exceptions, warnings, approx comparisons
- how-to/fixtures.md - Complete fixture guide: scopes, teardown, parametrization, factories
- how-to/parametrize.md - Parametrizing tests and fixtures
- how-to/mark.md - Custom markers, registering marks
- how-to/skipping.md - skip, skipif, xfail markers
Test Organization:
Output & Capture:
Integration:
Plugins:
Other:
Reference
Explanation
Examples
Common Patterns
conftest.py
Share fixtures across test files:
import pytest
@pytest.fixture
def database():
db = Database()
yield db
db.close()
Yield Fixture (Teardown)
@pytest.fixture
def resource():
r = acquire_resource()
yield r
release_resource(r)
Fixture Factories
@pytest.fixture
def make_customer():
created = []
def _make(name):
c = Customer(name)
created.append(c)
return c
yield _make
for c in created:
c.delete()
Autouse Fixtures
@pytest.fixture(autouse=True)
def clean_db(db):
yield
db.rollback()
Configuration
pyproject.toml
[pytest]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = "-v --tb=short"
markers = [
"slow: marks tests as slow",
"integration: integration tests",
]
pytest.ini
[pytest]
testpaths = tests
addopts = -v
Official Resources