[py-svn] pytest commit 06270cb47dad: allow setup_method/teardown_method to be mixed into unittest cases, reshuffle tests a bit

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Nov 24 16:18:03 CET 2010


# HG changeset patch -- Bitbucket.org
# Project pytest
# URL http://bitbucket.org/hpk42/pytest/overview
# User holger krekel <holger at merlinux.eu>
# Date 1290611869 -3600
# Node ID 06270cb47dadcbff1bb45fb3da8f7d0cb877ae0a
# Parent  84ce63c04007afe18d1491742bc3c56794a22658
allow setup_method/teardown_method to be mixed into unittest cases, reshuffle tests a bit

--- 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
 

--- 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'],

--- 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:

--- 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_e
     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("""

--- a/pytest.py
+++ b/pytest.py
@@ -5,7 +5,7 @@ see http://pytest.org for documentation 
 
 (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



More information about the pytest-commit mailing list