Skip to content

pytest >=8.1.1 displays no diff for AssertionError with --import-mode=importlib #12659

@devanubis

Description

@devanubis

Description

It seems that from pytest 8.1.1 onwards, using --import-mode=importlib causes AssertionError to not render a diff in the shell output.

8.0.2 worked just fine (as did 7.x).

To reproduce, the test has to be within a package with a __init__.py present (which our tests are, for reasons).

Reproduction

A clean environment:

python -m venv .venv
source .venv/bin/activate
pip install pytest==8.1.1
mkdir tests
touch tests/__init__.py

And then put a simple test file in the tests dir:

def test():
    assert "four lights" == "five lights"

And then run: pytest --import-mode=importlib tests/test.py

Outputs:

=========================================== test session starts ============================================
platform linux -- Python 3.11.2, pytest-8.1.1, pluggy-1.5.0
rootdir: /home/.../pytest-diff-reproduction
collected 1 item                                                                                           

tests/test.py F                                                                                      [100%]

================================================= FAILURES =================================================
___________________________________________________ test ___________________________________________________

    def test():
>       assert "four lights" == "five lights"
E       AssertionError

tests/test.py:2: AssertionError
========================================= short test summary info ==========================================
FAILED tests/test.py::test - AssertionError
============================================ 1 failed in 0.01s =============================================

Without a diff after the AssertionError

If ran without --import-mode=importlib on 8.0.2 the output shows the diff:

=========================================== test session starts ============================================
platform linux -- Python 3.11.2, pytest-8.1.1, pluggy-1.5.0
rootdir: /home/.../pytest-diff-reproduction
collected 1 item                                                                                           

tests/test.py F                                                                                      [100%]

================================================= FAILURES =================================================
___________________________________________________ test ___________________________________________________

    def test():
>       assert "four lights" == "five lights"
E       AssertionError: assert 'four lights' == 'five lights'
E         
E         - five lights
E         + four lights

tests/test.py:2: AssertionError
========================================= short test summary info ==========================================
FAILED tests/test.py::test - AssertionError: assert 'four lights' == 'five lights'
============================================ 1 failed in 0.02s =============================================

Verbosity has no effect, no other packages need to be present (although setuptools etc from the default venv are).

Environment

Debian 12, python 3.11 (but also reproduced on 3.12 too)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions