Back to Repositories

Testing Jupyter Console Integration in Textualize/rich

This test suite validates the Jupyter integration functionality in Rich’s Console class, focusing on console dimensions and environment variable handling. The tests ensure proper initialization and configuration of the console when running in Jupyter environments.

Test Coverage Overview

The test suite provides comprehensive coverage of Jupyter-specific console behavior in Rich.

Key areas tested include:
  • Default console dimensions in Jupyter mode
  • Color system configuration
  • JUPYTER_COLUMNS environment variable handling
  • JUPYTER_LINES environment variable processing
  • Precedence of explicit width/height settings over environment variables

Implementation Analysis

The testing approach uses direct instantiation of the Console class with various configuration parameters and environment variable combinations. Tests verify both successful cases and error handling scenarios, employing pytest’s assertion framework to validate expected behaviors.

Key patterns include:
  • Environment variable mocking using _environ parameter
  • Force Jupyter mode testing
  • Dimension override verification

Technical Details

Testing tools and configuration:
  • pytest as the testing framework
  • Rich’s Console class with force_jupyter parameter
  • Environment variable simulation through _environ dictionary
  • Assertion-based verification
  • Error condition handling for malformed environment variables

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Isolated test cases for specific functionality
  • Clear test method naming conventions
  • Comprehensive edge case coverage
  • Environment variable handling patterns
  • Graceful error handling verification

textualize/rich

tests/test_jupyter.py

            
from rich.console import Console


def test_jupyter():
    console = Console(force_jupyter=True)
    assert console.width == 115
    assert console.height == 100
    assert console.color_system == "truecolor"


def test_jupyter_columns_env():
    console = Console(_environ={"JUPYTER_COLUMNS": "314"}, force_jupyter=True)
    assert console.width == 314
    # width take precedence
    console = Console(width=40, _environ={"JUPYTER_COLUMNS": "314"}, force_jupyter=True)
    assert console.width == 40
    # Should not fail
    console = Console(
        width=40, _environ={"JUPYTER_COLUMNS": "broken"}, force_jupyter=True
    )


def test_jupyter_lines_env():
    console = Console(_environ={"JUPYTER_LINES": "220"}, force_jupyter=True)
    assert console.height == 220
    # height take precedence
    console = Console(height=40, _environ={"JUPYTER_LINES": "220"}, force_jupyter=True)
    assert console.height == 40
    # Should not fail
    console = Console(
        width=40, _environ={"JUPYTER_LINES": "broken"}, force_jupyter=True
    )