pytest-rich

0.2.0 · active · verified Fri Apr 17

pytest-rich is a pytest plugin that integrates the rich library to provide enhanced, colorful, and highly readable output for test sessions. It improves the default pytest terminal output with features like better traceback formatting, progress bars, and syntax highlighting. As of version 0.2.0, it is actively maintained with a relatively stable release cadence, primarily focusing on bug fixes and compatibility updates.

Common errors

Warnings

Install

Quickstart

This quickstart demonstrates basic usage of `pytest-rich` by creating a few simple tests (success, failure, error, skipped, warning) and configuring `pytest` via `pyproject.toml` to enable rich output, tracebacks, and help messages. Run `pytest` in the terminal to see the enhanced output.

import pytest
import os

# Create a dummy test file
with open("test_example.py", "w") as f:
    f.write(
"""import pytest

def test_success():
    assert True

def test_failure():
    assert False

def test_error_division_by_zero():
    1 / 0

@pytest.mark.skip(reason="demonstrate skip")
def test_skipped():
    pass

def test_warning():
    pytest.warn("This is a test warning.")

"""
    )

# Create a pyproject.toml to enable rich output
with open("pyproject.toml", "w") as f:
    f.write(
"""[tool.pytest.ini_options]
addopts = [
    "--rich",
    "--rich-tracebacks",
    "--rich-help",
    "--strict-markers",
]
"""
    )

print("Created test_example.py and pyproject.toml")
print("To run, navigate to this directory in your terminal and execute: pytest")

view raw JSON →