pytest-print
pytest-print is a pytest plugin that provides fixtures like `printer` and `pprint` for writing messages directly to the pytest runner terminal, bypassing pytest's default output capture. It is actively maintained, with version 1.2.2 requiring Python >=3.10, and sees regular patch and minor releases.
Common errors
-
fixture 'printer' not found
cause The `pytest-print` plugin is not installed in the active Python environment or pytest is not discovering plugins correctly.fixEnsure `pytest-print` is installed via `pip install pytest-print` in the same environment where `pytest` is being run. Verify `pytest` itself is correctly installed and accessible. -
My `print()` statements aren't showing up during tests, but `printer()` output does.
cause pytest captures standard output and error by default. The `printer` and `pprint` fixtures explicitly bypass this capture, which is their intended behavior.fixIf you want standard `print()` statements to appear during test execution, run `pytest` with the `-s` flag (e.g., `pytest -s`). Alternatively, use the `printer` fixture for all direct terminal output you wish to always see.
Warnings
- breaking pytest-print v1.2.0 dropped support for Python 3.8 and 3.9. Subsequent versions, including 1.2.2, require Python >=3.10.
- gotcha The `printer` and `pprint` fixtures bypass pytest's standard stdout/stderr capture mechanisms to ensure output is always visible in the terminal. This can be confusing if you expect all `print()` statements to behave similarly.
- breaking Version 1.2.1 included internal modernizations for Python 3.10+ and public pytest APIs. While not typically affecting end-user tests directly, this may subtly break compatibility for advanced users or other plugins relying on specific internal behaviors of `pytest-print` prior to this version.
Install
-
pip install pytest-print
Imports
- printer
from pytest_print import printer
def test_example(printer):
- pprint
from pytest_print import pprint
def test_example(pprint):
- printer_factory
from pytest_print import printer_factory
def test_example(printer_factory):
Quickstart
import pytest
def test_printer_fixture(printer):
# The 'printer' fixture writes directly to the terminal, bypassing pytest's stdout capture.
printer("Hello from pytest-print! This message should always appear.")
print("This is a standard print. It will be captured unless pytest is run with -s.")
def test_pprint_fixture(pprint):
data = {'name': 'Alice', 'age': 30, 'cities': ['New York', 'London']}
pprint(data)
pprint([1, 2, 3, {'a': 'b', 'c': 'd'}])
# To run this test:
# 1. Save as a Python file (e.g., test_my_app.py)
# 2. Run from your terminal in the same directory: pytest -v -s test_my_app.py