Popular Fixture Plugins
The pytest ecosystem offers powerful plugins that extend fixture capabilities. These plugins save you from writing boilerplate code for common testing patterns.
pytest-mock
Provides a mocker fixture that wraps unittest.mock with automatic cleanup:
pip install pytest-mock
def test_with_mocker(mocker):
# Mock a function
mock_func = mocker.patch("my_module.external_api")
mock_func.return_value = {"status": "ok"}
result = my_function()
assert result == {"status": "ok"}
mock_func.assert_called_once()
# No need for context managers or manual cleanup
pytest-lazy-fixture
Lets you use fixtures as test parameters in @pytest.mark.parametrize:
pip install pytest-lazy-fixture
import pytest
@pytest.fixture
def admin_user():
return {"role": "admin", "name": "Alice"}
@pytest.fixture
def regular_user():
return {"role": "user", "name": "Bob"}
@pytest.mark.parametrize("user", [
pytest.lazy_fixture("admin_user"),
pytest.lazy_fixture("regular_user"),
])
def test_user_dashboard(user):
assert "role" in user
pytest-env
Sets environment variables before tests run:
pip install pytest-env
[pytest]
env =
DJANGO_SETTINGS_MODULE=myproject.settings
DATABASE_URL=sqlite:///test.db
pytest-xdist
Runs tests in parallel across multiple CPU cores:
pip install pytest-xdist
# Run tests with auto-detected CPU count
pytest -n auto
# Run with 4 workers
pytest -n 4