summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorholger krekel <holger@merlinux.eu>2010-11-24 16:17:49 +0100
committerholger krekel <holger@merlinux.eu>2010-11-24 16:17:49 +0100
commitc36b20b137d37ef433b8cc765881293b50159183 (patch)
tree234cf191e33e2a69512e72249de440350175e44b
parent10d4544267254a2bb881e8d54ea2436602e74d3d (diff)
downloadpytest-c36b20b137d37ef433b8cc765881293b50159183.tar.gz
allow setup_method/teardown_method to be mixed into unittest cases, reshuffle tests a bit
-rw-r--r--_pytest/pytester.py4
-rw-r--r--_pytest/unittest.py16
-rw-r--r--pytest.py2
-rw-r--r--setup.py2
-rw-r--r--testing/test_unittest.py114
5 files changed, 76 insertions, 62 deletions
diff --git a/_pytest/pytester.py b/_pytest/pytester.py
index c172948b0..f32b0414c 100644
--- a/_pytest/pytester.py
+++ b/_pytest/pytester.py
@@ -552,10 +552,12 @@ class ReportRecorder(object):
def getreports(self, names="pytest_runtest_logreport pytest_collectreport"):
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 """
l = []
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("::"):
l.append(rep)
if not l:
diff --git a/_pytest/unittest.py b/_pytest/unittest.py
index 4ad7f5e28..93b1a14af 100644
--- a/_pytest/unittest.py
+++ b/_pytest/unittest.py
@@ -36,10 +36,17 @@ class UnitTestCase(pytest.Class):
class TestCaseFunction(pytest.Function):
_excinfo = None
+
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):
- pass
+ if hasattr(self._testcase, 'teardown_method'):
+ self._testcase.teardown_method(self._obj)
+
def startTest(self, testcase):
pass
@@ -75,13 +82,12 @@ class TestCaseFunction(pytest.Function):
def stopTest(self, testcase):
pass
def runtest(self):
- testcase = self.parent.obj(self.name)
- testcase(result=self)
+ self._testcase(result=self)
@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call):
if isinstance(item, TestCaseFunction):
- if hasattr(item, '_excinfo') and item._excinfo:
+ if item._excinfo:
call.excinfo = item._excinfo.pop(0)
del call.result
diff --git a/pytest.py b/pytest.py
index 1612dd640..f72c6068f 100644
--- a/pytest.py
+++ b/pytest.py
@@ -5,7 +5,7 @@ see http://pytest.org for documentation and details
(c) Holger Krekel and others, 2004-2010
"""
-__version__ = '2.0.0.dev37'
+__version__ = '2.0.0.dev38'
__all__ = ['main']
from _pytest.core import main, UsageError, _preloadplugins
diff --git a/setup.py b/setup.py
index 9ed4c5e64..0c38a2057 100644
--- a/setup.py
+++ b/setup.py
@@ -22,7 +22,7 @@ def main():
name='pytest',
description='py.test: simple powerful testing with Python',
long_description = long_description,
- version='2.0.0.dev37',
+ version='2.0.0.dev38',
url='http://pytest.org',
license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],
diff --git a/testing/test_unittest.py b/testing/test_unittest.py
index f048700f8..a043217be 100644
--- a/testing/test_unittest.py
+++ b/testing/test_unittest.py
@@ -26,17 +26,24 @@ def test_isclasscheck_issue53(testdir):
assert result.ret == 0
def test_setup(testdir):
- testpath = testdir.makepyfile(test_two="""
+ testpath = testdir.makepyfile("""
import unittest
class MyTestCase(unittest.TestCase):
def setUp(self):
self.foo = 1
- def test_setUp(self):
+ def setup_method(self, method):
+ self.foo2 = 1
+ def test_both(self):
self.assertEquals(1, self.foo)
+ assert self.foo2 == 1
+ def teardown_method(self, method):
+ assert 0, "42"
+
""")
- reprec = testdir.inline_run(testpath)
- rep = reprec.matchreport("test_setUp")
- assert rep.passed
+ reprec = testdir.inline_run("-s", testpath)
+ assert reprec.matchreport("test_both", when="call").passed
+ rep = reprec.matchreport("test_both", when="teardown")
+ assert rep.failed and '42' in str(rep.longrepr)
def test_new_instances(testdir):
testpath = testdir.makepyfile("""
@@ -53,7 +60,6 @@ def test_new_instances(testdir):
def test_teardown(testdir):
testpath = testdir.makepyfile("""
import unittest
- pytest_plugins = "pytest_unittest" # XXX
class MyTestCase(unittest.TestCase):
l = []
def test_one(self):
@@ -70,17 +76,44 @@ def test_teardown(testdir):
assert passed == 2
assert passed + skipped + failed == 2
-def test_module_level_pytestmark(testdir):
- testpath = testdir.makepyfile("""
+def test_method_and_teardown_failing_reporting(testdir):
+ 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 pytest
- pytestmark = pytest.mark.xfail
- class MyTestCase(unittest.TestCase):
- def test_func1(self):
- assert 0
+ class TC(unittest.TestCase):
+ def setUp(self):
+ assert 0, "down1"
+ def test_method(self):
+ print ("never42")
+ xyz
""")
- reprec = testdir.inline_run(testpath, "-s")
- reprec.assertoutcome(skipped=1)
+ 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_setup_setUpClass(testdir):
testpath = testdir.makepyfile("""
@@ -186,6 +219,19 @@ def test_testcase_totally_incompatible_exception_info(testdir):
excinfo = item._excinfo.pop(0)
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:
def setup_class(cls):
pytest.importorskip("twisted.trial.unittest")
@@ -275,46 +321,6 @@ class TestTrialUnittest:
child.expect("hellopdb")
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):
# contributed from Morten Breekevold
testdir.makepyfile("""