test_assertrewrite.py


Overview

The [test_assertrewrite.py](/projects/286/67351) file is a comprehensive test suite dedicated to verifying the behavior, correctness, and robustness of **pytest's assertion rewriting mechanism**. This mechanism enhances Python's built-in `assert` statements by rewriting them at import time to provide detailed introspection on assertion failures, including expression values and context. This file:

This file is vital for maintaining the correctness and user-friendliness of pytest's powerful assertion introspection feature.


Detailed Explanation of Key Components

Functions

rewrite(src: str) -> ast.Module

Parses and rewrites all `assert` statements in the provided source code string.


getmsg(f, extra_ns: Mapping[str, object] | None = None, *, must_pass: bool = False) -> str | None

Rewrites assertions in function `f`, executes it, and captures the assertion failure message, if any.


Class: TestAssertionRewrite

This class contains numerous test methods verifying the correctness and behavior of pytest's assertion rewriting on source code and functions.


Class: TestRewriteOnImport

Tests that assertion rewriting works correctly during module import, including:


Class: TestAssertionRewriteHookDetails

Focused on low-level internals of the assertion rewriting import hook:


Class: TestEarlyRewriteBailout

Tests optimizations in the assertion rewriting hook to avoid unnecessary import work:


Class: TestAssertionPass

Tests the experimental pytest hook `pytest_assertion_pass` that is called on successful assertions if enabled:


Other Test Classes


Important Implementation Details and Algorithms


Interactions with Other Parts of the System


Visual Diagram

Below is a class diagram summarizing the key test classes and their main responsibilities in this file:

classDiagram
    class TestAssertionRewrite {
        +test_place_initial_imports()
        +test_location_is_set()
        +test_positions_are_preserved()
        +test_dont_rewrite()
        +test_boolop()
        +test_call()
        +test_comparisons()
        +test_assertion_message()
    }
    class TestRewriteOnImport {
        +test_pycache_is_a_file()
        +test_zipfile()
        +test_readonly()
        +test_dont_write_bytecode()
        +test_cached_pyc_includes_pytest_version()
    }
    class TestAssertionRewriteHookDetails {
        +test_sys_meta_path_munged()
        +test_write_pyc()
        +test_read_pyc()
        +test_reload_is_same_and_reloads()
        +test_get_data_support()
    }
    class TestEarlyRewriteBailout {
        +test_basic()
        +test_pattern_contains_subdirectories()
        +test_cwd_changed()
    }
    class TestAssertionPass {
        +test_hook_call()
        +test_hook_call_with_parens()
        +test_hook_not_called_without_hookimpl()
    }
    class TestPyCacheDir {
        +test_get_cache_dir()
        +test_sys_pycache_prefix_integration()
    }
    class TestReprSizeVerbosity {
        +test_get_maxsize_for_saferepr()
        +test_default_verbosity()
        +test_increased_verbosity()
    }

    TestAssertionRewrite <|-- TestRewriteOnImport
    TestRewriteOnImport <|-- TestAssertionRewriteHookDetails
    TestAssertionRewriteHookDetails <|-- TestEarlyRewriteBailout
    TestEarlyRewriteBailout <|-- TestAssertionPass
    TestPyCacheDir <|-- TestReprSizeVerbosity

Summary

[test_assertrewrite.py](/projects/286/67351) rigorously validates pytest's assertion rewriting mechanism, ensuring that Python `assert` statements provide rich, informative failure messages that aid debugging. It covers rewriting correctness, import-time rewriting behavior, caching, compatibility with language features like the walrus operator, and integration with pytest's plugin and fixture systems. The tests in this file safeguard one of pytest's most celebrated features, allowing users to write succinct tests with powerful failure introspection.


End of Documentation for test_assertrewrite.py