diff options
author | Bruno Oliveira <nicoddemus@gmail.com> | 2016-10-03 21:46:44 -0300 |
---|---|---|
committer | Bruno Oliveira <nicoddemus@gmail.com> | 2016-10-03 21:46:44 -0300 |
commit | a1d446b8e809f1e6488ce183ace93ab6693f827c (patch) | |
tree | 56f5653d5e6b9531257feac0e87089b2191f508b /_pytest | |
parent | 7d66e4eae1cc5989df07e140f2ff6949416c2c18 (diff) | |
download | pytest-a1d446b8e809f1e6488ce183ace93ab6693f827c.tar.gz |
Display traceback from Import errors using pytest's short representation
Also omit pytest's own modules and internal libraries (py and pluggy) in low verbosity
Fix #1976
Diffstat (limited to '_pytest')
-rw-r--r-- | _pytest/python.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/_pytest/python.py b/_pytest/python.py index 74bb4de43..62d2896ea 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -22,11 +22,16 @@ from _pytest.compat import ( getlocation, enum, ) -cutdir2 = py.path.local(_pytest.__file__).dirpath() cutdir1 = py.path.local(pluggy.__file__.rstrip("oc")) +cutdir2 = py.path.local(_pytest.__file__).dirpath() +cutdir3 = py.path.local(py.__file__).dirpath() def filter_traceback(entry): + """Return True if a TracebackEntry instance should be removed from tracebacks: + * dynamically generated code (no code to show up for it); + * internal traceback from pytest or its internal libraries, py and pluggy. + """ # entry.path might sometimes return a str object when the entry # points to dynamically generated code # see https://bitbucket.org/pytest-dev/py/issues/71 @@ -37,7 +42,7 @@ def filter_traceback(entry): # entry.path might point to an inexisting file, in which case it will # alsso return a str object. see #1133 p = py.path.local(entry.path) - return p != cutdir1 and not p.relto(cutdir2) + return p != cutdir1 and not p.relto(cutdir2) and not p.relto(cutdir3) @@ -424,14 +429,16 @@ class Module(pytest.File, PyCollector): % e.args ) except ImportError: - import traceback - stream = py.io.TextIO() - traceback.print_exc(file=stream) - formatted_tb = stream.getvalue() + from _pytest._code.code import ExceptionInfo + exc_info = ExceptionInfo() + if self.config.getoption('verbose') < 2: + exc_info.traceback = exc_info.traceback.filter(filter_traceback) + exc_repr = exc_info.getrepr(style='short') if exc_info.traceback else exc_info.exconly() + formatted_tb = py._builtin._totext(exc_repr) raise self.CollectError( "ImportError while importing test module '{fspath}'.\n" "Hint: make sure your test modules/packages have valid Python names.\n" - "Original traceback:\n" + "Traceback:\n" "{traceback}".format(fspath=self.fspath, traceback=formatted_tb) ) except _pytest.runner.Skipped as e: |