teach trial support code to throw separate errors/failures for setup/call/teardown

This commit is contained in:
holger krekel 2010-11-24 14:35:04 +01:00
parent ff27d299cc
commit 10d4544267
2 changed files with 48 additions and 8 deletions

View File

@ -47,7 +47,7 @@ class TestCaseFunction(pytest.Function):
# unwrap potential exception info (see twisted trial support below) # unwrap potential exception info (see twisted trial support below)
rawexcinfo = getattr(rawexcinfo, '_rawexcinfo', rawexcinfo) rawexcinfo = getattr(rawexcinfo, '_rawexcinfo', rawexcinfo)
try: try:
self._excinfo = py.code.ExceptionInfo(rawexcinfo) excinfo = py.code.ExceptionInfo(rawexcinfo)
except TypeError: except TypeError:
try: try:
try: try:
@ -63,7 +63,8 @@ class TestCaseFunction(pytest.Function):
except KeyboardInterrupt: except KeyboardInterrupt:
raise raise
except pytest.fail.Exception: except pytest.fail.Exception:
self._excinfo = py.code.ExceptionInfo() excinfo = py.code.ExceptionInfo()
self.__dict__.setdefault('_excinfo', []).append(excinfo)
def addError(self, testcase, rawexcinfo): def addError(self, testcase, rawexcinfo):
self._addexcinfo(rawexcinfo) self._addexcinfo(rawexcinfo)
@ -80,9 +81,8 @@ class TestCaseFunction(pytest.Function):
@pytest.mark.tryfirst @pytest.mark.tryfirst
def pytest_runtest_makereport(item, call): def pytest_runtest_makereport(item, call):
if isinstance(item, TestCaseFunction): if isinstance(item, TestCaseFunction):
if item._excinfo: if hasattr(item, '_excinfo') and item._excinfo:
call.excinfo = item._excinfo call.excinfo = item._excinfo.pop(0)
item._excinfo = None
del call.result del call.result
# twisted trial support # twisted trial support

View File

@ -183,11 +183,9 @@ def test_testcase_totally_incompatible_exception_info(testdir):
pass pass
""") """)
item.addError(None, 42) item.addError(None, 42)
excinfo = item._excinfo excinfo = item._excinfo.pop(0)
assert 'ERROR: Unknown Incompatible' in str(excinfo.getrepr()) assert 'ERROR: Unknown Incompatible' in str(excinfo.getrepr())
class TestTrialUnittest: class TestTrialUnittest:
def setup_class(cls): def setup_class(cls):
pytest.importorskip("twisted.trial.unittest") pytest.importorskip("twisted.trial.unittest")
@ -225,6 +223,7 @@ class TestTrialUnittest:
"*3 skipped*2 xfail*", "*3 skipped*2 xfail*",
]) ])
@pytest.mark.xfail(reason="fijal needs add checks")
def test_trial_error(self, testdir): def test_trial_error(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
from twisted.trial.unittest import TestCase from twisted.trial.unittest import TestCase
@ -262,6 +261,7 @@ class TestTrialUnittest:
# will crash both at test time and at teardown # will crash both at test time and at teardown
""") """)
result = testdir.runpytest() result = testdir.runpytest()
assert 0
def test_trial_pdb(self, testdir): def test_trial_pdb(self, testdir):
p = testdir.makepyfile(""" p = testdir.makepyfile("""
@ -275,6 +275,46 @@ class TestTrialUnittest:
child.expect("hellopdb") child.expect("hellopdb")
child.sendeof() child.sendeof()
def test_trial_setup_failure_is_shown(self, testdir):
testdir.makepyfile("""
from twisted.trial import unittest
import pytest
class TC(unittest.TestCase):
def setUp(self):
assert 0, "down1"
def test_method(self):
print ("never42")
xyz
""")
result = testdir.runpytest("-s")
assert result.ret == 1
result.stdout.fnmatch_lines([
"*setUp*",
"*assert 0*down1*",
"*1 failed*",
])
assert 'never42' not in result.stdout.str()
def test_trial_teardown_and_test_failure(self, testdir):
testdir.makepyfile("""
from twisted.trial import unittest
import pytest
class TC(unittest.TestCase):
def tearDown(self):
assert 0, "down1"
def test_method(self):
assert False, "down2"
""")
result = testdir.runpytest("-s")
assert result.ret == 1
result.stdout.fnmatch_lines([
"*tearDown*",
"*assert 0*",
"*test_method*",
"*assert False*",
"*1 failed*1 error*",
])
def test_djangolike_testcase(testdir): def test_djangolike_testcase(testdir):
# contributed from Morten Breekevold # contributed from Morten Breekevold
testdir.makepyfile(""" testdir.makepyfile("""