Skip to content

please don't suppress tracebacks on ImportError during test collection #1976

Closed
@cwitty

Description

@cwitty

This chunk of code in _pytest/python.py (from pytest-3.0.3):

    except ImportError:
        exc_class, exc, _ = sys.exc_info()
        raise self.CollectError(
            "ImportError while importing test module '%s'.\n"
            "Original error message:\n'%s'\n"
            "Make sure your test modules/packages have valid Python names."
            % (self.fspath, exc or exc_class)
        )

ignores the traceback. Please print at least the line and file number of the original failing import.

(I was just refactoring my code, and moved some code around in a way that I knew would cause import errors. But I don't know everywhere this code is imported from, without searching my whole code base or letting my test suite tell me. And if my test suite won't tell me more than "it's broken", then I'm back to searching the code base.)

Minimal example:

(venv) cwitty@helix:/tmp/pttest$ touch a.py
(venv) cwitty@helix:/tmp/pttest$ echo 'from a import anything' > b.py
(venv) cwitty@helix:/tmp/pttest$ echo 'import b' > test_b.py
(venv) cwitty@helix:/tmp/pttest$ pytest

with result:

_________________________ ERROR collecting test_b.py __________________________
ImportError while importing test module '/tmp/pttest/test_b.py'.
Original error message:
'cannot import name 'anything''
Make sure your test modules/packages have valid Python names.

Note that the error message doesn't even mention b.py, the file with the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy issue that is friendly to new contributortype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions