allow setup_method/teardown_method to be mixed into unittest cases, reshuffle tests a bit

This commit is contained in:
holger krekel 2010-11-24 16:17:49 +01:00
parent 10d4544267
commit c36b20b137
5 changed files with 76 additions and 62 deletions

View File

@ -552,10 +552,12 @@ class ReportRecorder(object):
def getreports(self, names="pytest_runtest_logreport pytest_collectreport"): def getreports(self, names="pytest_runtest_logreport pytest_collectreport"):
return [x.report for x in self.getcalls(names)] return [x.report for x in self.getcalls(names)]
def matchreport(self, inamepart="", names="pytest_runtest_logreport pytest_collectreport"): def matchreport(self, inamepart="", names="pytest_runtest_logreport pytest_collectreport", when=None):
""" return a testreport whose dotted import path matches """ """ return a testreport whose dotted import path matches """
l = [] l = []
for rep in self.getreports(names=names): for rep in self.getreports(names=names):
if when and getattr(rep, 'when', None) != when:
continue
if not inamepart or inamepart in rep.nodeid.split("::"): if not inamepart or inamepart in rep.nodeid.split("::"):
l.append(rep) l.append(rep)
if not l: if not l:

View File

@ -36,10 +36,17 @@ class UnitTestCase(pytest.Class):
class TestCaseFunction(pytest.Function): class TestCaseFunction(pytest.Function):
_excinfo = None _excinfo = None
def setup(self): def setup(self):
pass self._testcase = self.parent.obj(self.name)
self._obj = getattr(self._testcase, self.name)
if hasattr(self._testcase, 'setup_method'):
self._testcase.setup_method(self._obj)
def teardown(self): def teardown(self):
pass if hasattr(self._testcase, 'teardown_method'):
self._testcase.teardown_method(self._obj)
def startTest(self, testcase): def startTest(self, testcase):
pass pass
@ -75,13 +82,12 @@ class TestCaseFunction(pytest.Function):
def stopTest(self, testcase): def stopTest(self, testcase):
pass pass
def runtest(self): def runtest(self):
testcase = self.parent.obj(self.name) self._testcase(result=self)
testcase(result=self)
@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 hasattr(item, '_excinfo') and item._excinfo: if item._excinfo:
call.excinfo = item._excinfo.pop(0) call.excinfo = item._excinfo.pop(0)
del call.result del call.result

View File

@ -5,7 +5,7 @@ see http://pytest.org for documentation and details
(c) Holger Krekel and others, 2004-2010 (c) Holger Krekel and others, 2004-2010
""" """
__version__ = '2.0.0.dev37' __version__ = '2.0.0.dev38'
__all__ = ['main'] __all__ = ['main']
from _pytest.core import main, UsageError, _preloadplugins from _pytest.core import main, UsageError, _preloadplugins

View File

@ -22,7 +22,7 @@ def main():
name='pytest', name='pytest',
description='py.test: simple powerful testing with Python', description='py.test: simple powerful testing with Python',
long_description = long_description, long_description = long_description,
version='2.0.0.dev37', version='2.0.0.dev38',
url='http://pytest.org', url='http://pytest.org',
license='MIT license', license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -26,17 +26,24 @@ def test_isclasscheck_issue53(testdir):
assert result.ret == 0 assert result.ret == 0
def test_setup(testdir): def test_setup(testdir):
testpath = testdir.makepyfile(test_two=""" testpath = testdir.makepyfile("""
import unittest import unittest
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.foo = 1 self.foo = 1
def test_setUp(self): def setup_method(self, method):
self.foo2 = 1
def test_both(self):
self.assertEquals(1, self.foo) self.assertEquals(1, self.foo)
assert self.foo2 == 1
def teardown_method(self, method):
assert 0, "42"
""") """)
reprec = testdir.inline_run(testpath) reprec = testdir.inline_run("-s", testpath)
rep = reprec.matchreport("test_setUp") assert reprec.matchreport("test_both", when="call").passed
assert rep.passed rep = reprec.matchreport("test_both", when="teardown")
assert rep.failed and '42' in str(rep.longrepr)
def test_new_instances(testdir): def test_new_instances(testdir):
testpath = testdir.makepyfile(""" testpath = testdir.makepyfile("""
@ -53,7 +60,6 @@ def test_new_instances(testdir):
def test_teardown(testdir): def test_teardown(testdir):
testpath = testdir.makepyfile(""" testpath = testdir.makepyfile("""
import unittest import unittest
pytest_plugins = "pytest_unittest" # XXX
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
l = [] l = []
def test_one(self): def test_one(self):
@ -70,17 +76,44 @@ def test_teardown(testdir):
assert passed == 2 assert passed == 2
assert passed + skipped + failed == 2 assert passed + skipped + failed == 2
def test_module_level_pytestmark(testdir): def test_method_and_teardown_failing_reporting(testdir):
testpath = testdir.makepyfile(""" testdir.makepyfile("""
import unittest, 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_setup_failure_is_shown(testdir):
testdir.makepyfile("""
import unittest import unittest
import pytest import pytest
pytestmark = pytest.mark.xfail class TC(unittest.TestCase):
class MyTestCase(unittest.TestCase): def setUp(self):
def test_func1(self): assert 0, "down1"
assert 0 def test_method(self):
print ("never42")
xyz
""") """)
reprec = testdir.inline_run(testpath, "-s") result = testdir.runpytest("-s")
reprec.assertoutcome(skipped=1) assert result.ret == 1
result.stdout.fnmatch_lines([
"*setUp*",
"*assert 0*down1*",
"*1 failed*",
])
assert 'never42' not in result.stdout.str()
def test_setup_setUpClass(testdir): def test_setup_setUpClass(testdir):
testpath = testdir.makepyfile(""" testpath = testdir.makepyfile("""
@ -186,6 +219,19 @@ def test_testcase_totally_incompatible_exception_info(testdir):
excinfo = item._excinfo.pop(0) excinfo = item._excinfo.pop(0)
assert 'ERROR: Unknown Incompatible' in str(excinfo.getrepr()) assert 'ERROR: Unknown Incompatible' in str(excinfo.getrepr())
def test_module_level_pytestmark(testdir):
testpath = testdir.makepyfile("""
import unittest
import pytest
pytestmark = pytest.mark.xfail
class MyTestCase(unittest.TestCase):
def test_func1(self):
assert 0
""")
reprec = testdir.inline_run(testpath, "-s")
reprec.assertoutcome(skipped=1)
class TestTrialUnittest: class TestTrialUnittest:
def setup_class(cls): def setup_class(cls):
pytest.importorskip("twisted.trial.unittest") pytest.importorskip("twisted.trial.unittest")
@ -275,46 +321,6 @@ 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("""