Back to Repositories

Testing Rich Console Output Functionality in Textualize/rich

A comprehensive test suite for Rich’s console printing functionality, focusing on output validation, JSON handling, and console configuration. The tests ensure proper text formatting, JSON serialization, and console manipulation capabilities.

Test Coverage Overview

The test suite provides thorough coverage of Rich’s console printing capabilities.

  • Console instance creation and configuration
  • Basic text printing with various formatting
  • JSON printing with indentation and validation
  • Large data handling and truncation prevention
  • Console output capture and verification

Implementation Analysis

The testing approach utilizes pytest’s framework features with Rich’s console capture mechanism.

Key patterns include:
  • Console state management with backup/restore
  • StringIO for output capture
  • JSON roundtrip validation
  • Edge case handling for large numbers and strings

Technical Details

Testing infrastructure includes:

  • pytest as the testing framework
  • Rich’s Console class for output management
  • StringIO for stream capture
  • JSON module for data validation
  • Console capture context manager for isolated testing

Best Practices Demonstrated

The test suite exemplifies robust testing practices:

  • Proper resource cleanup with try/finally blocks
  • Isolated test cases with minimal dependencies
  • Comprehensive edge case coverage
  • Clear test case organization and naming
  • Effective use of assertions for validation

textualize/rich

tests/test_rich_print.py

            
import io
import json

import rich
from rich.console import Console


def test_get_console():
    console = rich.get_console()
    assert isinstance(console, Console)


def test_reconfigure_console():
    rich.reconfigure(width=100)
    assert rich.get_console().width == 100


def test_rich_print():
    console = rich.get_console()
    output = io.StringIO()
    backup_file = console.file
    try:
        console.file = output
        rich.print("foo", "bar")
        rich.print("foo
")
        rich.print("foo

")
        assert output.getvalue() == "foo bar
foo

foo


"
    finally:
        console.file = backup_file


def test_rich_print_json():
    console = rich.get_console()
    with console.capture() as capture:
        rich.print_json('[false, true, null, "foo"]', indent=4)
    result = capture.get()
    print(repr(result))
    expected = '[
    false,
    true,
    null,
    "foo"
]
'
    assert result == expected


def test_rich_print_json_round_trip():
    data = ["x" * 100, 2e128]
    console = rich.get_console()
    with console.capture() as capture:
        rich.print_json(data=data, indent=4)
    result = capture.get()
    print(repr(result))
    result_data = json.loads(result)
    assert result_data == data


def test_rich_print_json_no_truncation():
    console = rich.get_console()
    with console.capture() as capture:
        rich.print_json(f'["{"x" * 100}", {int(2e128)}]', indent=4)
    result = capture.get()
    print(repr(result))
    assert ("x" * 100) in result
    assert str(int(2e128)) in result


def test_rich_print_X():
    console = rich.get_console()
    output = io.StringIO()
    backup_file = console.file
    try:
        console.file = output
        rich.print("foo")
        rich.print("fooX")
        rich.print("fooXX")
        assert output.getvalue() == "foo
fooX
fooXX
"
    finally:
        console.file = backup_file