pytest configuration: conftest.py, pyproject.toml, markers, plugins. Use when: setting up test infrastructure, configuring pytest, adding markers, organizing conftest files.
pyproject.toml [tool.pytest.ini_options]conftest.py[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "app.settings_dev"
python_files = ["tests.py", "test_*.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = "--strict-markers --tb=short -q"
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
"integration: marks integration tests requiring external services",
]
filterwarnings = [
"ignore::DeprecationWarning:factory.*",
]
conftest.py # Root: Django settings, shared fixtures
apps/firmwares/tests/
conftest.py # App-level: app-specific factories, fixtures
test_models.py
test_views.py
import pytest
from django.test import RequestFactory
@pytest.fixture
def request_factory():
return RequestFactory()
@pytest.fixture
def staff_user(db):
from apps.users.models import User
return User.objects.create_user(
username="staff", email="[email protected]",
password="testpass123", is_staff=True,
)
# All tests with coverage
& .\.venv\Scripts\python.exe -m pytest --cov=apps --cov-report=term-missing
# Single app
& .\.venv\Scripts\python.exe -m pytest apps/firmwares/tests/ -v
# By keyword
& .\.venv\Scripts\python.exe -m pytest -k "test_download"
# Exclude slow
& .\.venv\Scripts\python.exe -m pytest -m "not slow"
DJANGO_SETTINGS_MODULE in pyproject.toml — tests use wrong settingsdb or django_db marker — DatabaseError--strict-markers — typos in marker names silently pass& .\.venv\Scripts\python.exe -m ruff check . --fix
& .\.venv\Scripts\python.exe -m ruff format .
& .\.venv\Scripts\python.exe manage.py check --settings=app.settings_dev