darrenburns / pytest-clarity

A plugin to improve the output of pytest with colourful unified diffs
MIT License
433 stars 24 forks source link

Assertions in other files are not diffed #25

Closed reergymerej closed 1 year ago

reergymerej commented 1 year ago

Assertions raised from outside the test file are not diffed. Here's an example.

Is this a failure in pytest_assertrepr_compare maybe?

Baseline - beautiful.

def test_diff():
    assert {"1": 1} == {"2": 1}
E         LHS vs RHS shown below
E
E         {'1': 1}
E         {'2': 1}

Assert in non-test function, still beautiful.

def over_here():
    assert {"1": 1} == {"2": 1}

def test_diff():
    over_here()
E         LHS vs RHS shown below
E
E         {'1': 1}
E         {'2': 1}

Assert in another file, dang.

from tests.utils import over_here

def test_diff():
    over_here()
# tests/utils.py
def over_here():
    assert {"1": 1} == {"2": 1}
    def over_here():
>       assert {"1": 1} == {"2": 1}
E       AssertionError

tests/utils/__init__.py:120: AssertionError
reergymerej commented 1 year ago

Nm, found it.

pytest.register_assert_rewrite('tests.utils') # <<<<<<<<<
from tests.utils import over_here

def test_diff():
    over_here()
# tests/utils.py
def over_here():
    assert {"1": 1} == {"2": 1}
E         LHS vs RHS shown below
E
E         {'1': 1}
E         {'1': 2}