From 0fc75c962253ed1b712d8ca144a7f6ecc6df1b49 Mon Sep 17 00:00:00 2001 From: Almar Klein Date: Sat, 21 Mar 2015 17:06:24 +0100 Subject: [PATCH] Storing sys.last_traceback: test, docs and changelog --- CHANGELOG | 4 ++++ doc/en/usage.txt | 3 +++ testing/test_runner.py | 15 +++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 20a86f0f1..5fed0265a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,10 @@ 2.7.0.dev (compared to 2.6.4) ----------------------------- +- On failure, the ``sys.last_value``, ``sys.last_type`` and + ``sys.last_traceback`` are set, so that a user can inspect the error + via postmortem debugging. + - fix issue616: conftest.py files and their contained fixutres are now properly considered for visibility, independently from the exact current working directory and test arguments that are used. diff --git a/doc/en/usage.txt b/doc/en/usage.txt index b03b9b9d6..4931e7086 100644 --- a/doc/en/usage.txt +++ b/doc/en/usage.txt @@ -87,6 +87,9 @@ failure situation:: py.test -x --pdb # drop to PDB on first failure, then end test session py.test --pdb --maxfail=3 # drop to PDB for first three failures +Note that on any failure the exception information is stored on +``sys.last_traceback``. In interactive use, this allows one to drop +into postmortem debugging with any debug tool. Setting a breakpoint / aka ``set_trace()`` ---------------------------------------------------- diff --git a/testing/test_runner.py b/testing/test_runner.py index 9a13a6b47..fee1be09b 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -525,3 +525,18 @@ def test_makereport_getsource(testdir): result = testdir.runpytest() assert 'INTERNALERROR' not in result.stdout.str() result.stdout.fnmatch_lines(['*else: assert False*']) + + +def test_store_except_info_on_eror(testdir): + # Simulate item that raises a specific exception + class ItemThatRaises: + def runtest(self): + raise IndexError('TEST') + try: + runner.pytest_runtest_call(ItemThatRaises()) + except IndexError: + pass + # Check that exception info is stored on sys + assert sys.last_type is IndexError + assert sys.last_value.args[0] == 'TEST' + assert sys.last_traceback