From 61e605f60ba430fc557edbbe908f4ddadd995be6 Mon Sep 17 00:00:00 2001 From: Javier Domingo Cansino Date: Tue, 5 Jul 2016 09:56:54 +0100 Subject: [PATCH 1/2] Making conftest import failures easier to debug --- AUTHORS | 1 + CHANGELOG.rst | 2 +- _pytest/config.py | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index fd543b145..c1dec8150 100644 --- a/AUTHORS +++ b/AUTHORS @@ -59,6 +59,7 @@ Jaap Broekhuizen Jan Balster Janne Vanhala Jason R. Coombs +Javier Domingo Cansino John Towler Joshua Bronson Jurko Gospodnetić diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e67cffc70..6d754d929 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,7 +14,7 @@ level an error will be raised during collection. Thanks `@omarkohl`_ for the complete PR (`#1519`_). -* +* Fix (`1516`_): ConftestImportFailure now shows the traceback **Incompatible changes** diff --git a/_pytest/config.py b/_pytest/config.py index 463d8f04f..577425d6a 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -25,6 +25,12 @@ class ConftestImportFailure(Exception): self.path = path self.excinfo = excinfo + def __str__(self): + etype, evalue, etb = self.excinfo + formatted = traceback.format_tb(etb) + # The level of the tracebacks we want to print is hand crafted :( + return repr(evalue) + '\n' + ''.join(formatted[2:]) + def main(args=None, plugins=None): """ return exit code, after performing an in-process test run. From 0171cfa30f188dd1e9a2475c47dd654042e4669e Mon Sep 17 00:00:00 2001 From: Javier Domingo Cansino Date: Tue, 5 Jul 2016 10:39:12 +0100 Subject: [PATCH 2/2] Fixing link to issue and creating testcase that shows that it finds the line in the stderr lines --- CHANGELOG.rst | 3 ++- testing/test_conftest.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6d754d929..2dc03d693 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,7 +14,7 @@ level an error will be raised during collection. Thanks `@omarkohl`_ for the complete PR (`#1519`_). -* Fix (`1516`_): ConftestImportFailure now shows the traceback +* Fix (`#1516`_): ConftestImportFailure now shows the traceback **Incompatible changes** @@ -30,6 +30,7 @@ * removed support code for python 3 < 3.3 addressing (`#1627`_) .. _#607: https://github.com/pytest-dev/pytest/issues/607 +.. _#1516: https://github.com/pytest-dev/pytest/issues/1516 .. _#1519: https://github.com/pytest-dev/pytest/pull/1519 .. _#1664: https://github.com/pytest-dev/pytest/pull/1664 .. _#1627: https://github.com/pytest-dev/pytest/pull/1627 diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 6f5e77f6d..54a7d4009 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -407,3 +407,16 @@ def test_issue1073_conftest_special_objects(testdir): """) res = testdir.runpytest() assert res.ret == 0 + + +def test_conftest_exception_handling(testdir): + testdir.makeconftest(''' + raise ValueError() + ''') + testdir.makepyfile(""" + def test_some(): + pass + """) + res = testdir.runpytest() + assert res.ret == 4 + assert 'raise ValueError()' in [line.strip() for line in res.errlines]