diff options
author | Daniel Hahler <git@thequod.de> | 2019-10-23 06:59:18 +0200 |
---|---|---|
committer | Daniel Hahler <git@thequod.de> | 2019-11-06 22:08:10 +0100 |
commit | eb7a4e32ad920b4cdd9c956763535fed194ae8a7 (patch) | |
tree | 3a111410860e9ff9742fdfbe73140bcbf6c4add2 /testing/test_session.py | |
parent | 5be3a9b5cee3ef73b627049272db5bd2e34f5026 (diff) | |
download | pytest-eb7a4e32ad920b4cdd9c956763535fed194ae8a7.tar.gz |
saferepr: handle BaseExceptions
This causes INTERNALERRORs with pytest-django, which uses
`pytest.fail` (derived from `BaseException`) to prevent DB access, when
pytest then tries to e.g. display the `repr()` for a Django `QuerySet`
etc.
Ref: https://github.com/pytest-dev/pytest-django/pull/776
Diffstat (limited to 'testing/test_session.py')
-rw-r--r-- | testing/test_session.py | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/testing/test_session.py b/testing/test_session.py index dbe057376..7b4eb817a 100644 --- a/testing/test_session.py +++ b/testing/test_session.py @@ -102,15 +102,20 @@ class SessionTests: p = testdir.makepyfile( """ import pytest + + class reprexc(BaseException): + def __str__(self): + return "Ha Ha fooled you, I'm a broken repr()." + class BrokenRepr1(object): foo=0 def __repr__(self): - raise Exception("Ha Ha fooled you, I'm a broken repr().") + raise reprexc class TestBrokenClass(object): def test_explicit_bad_repr(self): t = BrokenRepr1() - with pytest.raises(Exception, match="I'm a broken repr"): + with pytest.raises(BaseException, match="broken repr"): repr(t) def test_implicit_bad_repr1(self): @@ -123,12 +128,7 @@ class SessionTests: passed, skipped, failed = reprec.listoutcomes() assert (len(passed), len(skipped), len(failed)) == (1, 0, 1) out = failed[0].longrepr.reprcrash.message - assert ( - out.find( - """[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""" - ) - != -1 - ) + assert out.find("<[reprexc() raised in repr()] BrokenRepr1") != -1 def test_broken_repr_with_showlocals_verbose(self, testdir): p = testdir.makepyfile( @@ -151,7 +151,7 @@ class SessionTests: assert repr_locals.lines assert len(repr_locals.lines) == 1 assert repr_locals.lines[0].startswith( - 'x = <[NotImplementedError("") raised in repr()] ObjWithErrorInRepr' + "x = <[NotImplementedError() raised in repr()] ObjWithErrorInRepr" ) def test_skip_file_by_conftest(self, testdir): |