[py-svn] pytest commit ac86d3a188b1: majorly changing the unittest compatibility code, calling TestCase(name)(result)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Mon Nov 1 23:08:47 CET 2010


# HG changeset patch -- Bitbucket.org
# Project pytest
# URL http://bitbucket.org/hpk42/pytest/overview
# User holger krekel <holger at merlinux.eu>
# Date 1288649296 -3600
# Node ID ac86d3a188b1da11c3df0fe6054805e7cb625a83
# Parent  7d2db9cfe9216159f9b41347e659fffca6095564
majorly changing the unittest compatibility code, calling TestCase(name)(result)

--- a/pytest/plugin/python.py
+++ b/pytest/plugin/python.py
@@ -383,7 +383,8 @@ class Function(FunctionMixin, pytest.col
             config=config, collection=collection)
         self._args = args
         if self._isyieldedfunction():
-            assert not callspec, "yielded functions (deprecated) cannot have funcargs"
+            assert not callspec, (
+                "yielded functions (deprecated) cannot have funcargs")
         else:
             if callspec is not None:
                 self.funcargs = callspec.funcargs or {}

--- a/pytest/__init__.py
+++ b/pytest/__init__.py
@@ -5,7 +5,7 @@ see http://pytest.org for documentation 
 
 (c) Holger Krekel and others, 2004-2010
 """
-__version__ = '2.0.0.dev17'
+__version__ = '2.0.0.dev18'
 
 __all__ = ['config', 'cmdline']
 

--- a/CHANGELOG
+++ b/CHANGELOG
@@ -2,6 +2,8 @@ Changes between 1.3.4 and 2.0.0dev0
 ----------------------------------------------
 
 - pytest-2.0 is now its own package and depends on pylib-2.0
+- try harder to run unittest test suites in a more compatible manner
+  by deferring setup/teardown semantics to the unittest package. 
 - introduce a new way to set config options via ini-style files,
   by default setup.cfg and tox.ini files are searched.  The old
   ways (certain environment variables, dynamic conftest.py reading 

--- a/testing/plugin/test_unittest.py
+++ b/testing/plugin/test_unittest.py
@@ -81,3 +81,25 @@ def test_module_level_pytestmark(testdir
     """)
     reprec = testdir.inline_run(testpath, "-s")
     reprec.assertoutcome(skipped=1)
+
+def test_class_setup(testdir):
+    testpath = testdir.makepyfile("""
+        import unittest
+        import py
+        class MyTestCase(unittest.TestCase):
+            x = 0
+            @classmethod
+            def setUpClass(cls):
+                cls.x += 1
+            def test_func1(self):
+                assert self.x == 1
+            def test_func2(self):
+                assert self.x == 1
+            @classmethod
+            def tearDownClass(cls):
+                cls.x -= 1
+        def test_teareddown():
+            assert MyTestCase.x == 0
+    """)
+    reprec = testdir.inline_run(testpath)
+    reprec.assertoutcome(passed=3)

--- a/pytest/plugin/unittest.py
+++ b/pytest/plugin/unittest.py
@@ -19,51 +19,31 @@ def pytest_pycollect_makeitem(collector,
 
 class UnitTestCase(py.test.collect.Class):
     def collect(self):
-        return [UnitTestCaseInstance("()", self)]
+        loader = py.std.unittest.TestLoader()
+        for name in loader.getTestCaseNames(self.obj):
+            yield TestCaseFunction(name, parent=self)
 
     def setup(self):
-        pass
+        meth = getattr(self.obj, 'setUpClass', None)
+        if meth is not None:
+            meth()
 
     def teardown(self):
+        meth = getattr(self.obj, 'tearDownClass', None)
+        if meth is not None:
+            meth()
+
+class TestCaseFunction(py.test.collect.Function):
+    def startTest(self, testcase):
         pass
-
-_dummy = object()
-class UnitTestCaseInstance(py.test.collect.Instance):
-    def collect(self):
-        loader = py.std.unittest.TestLoader()
-        names = loader.getTestCaseNames(self.obj.__class__)
-        l = []
-        for name in names:
-            callobj = getattr(self.obj, name)
-            if py.builtin.callable(callobj):
-                l.append(UnitTestFunction(name, parent=self))
-        return l
-
-    def _getobj(self):
-        x = self.parent.obj
-        return self.parent.obj(methodName='run')
-
-class UnitTestFunction(py.test.collect.Function):
-    def __init__(self, name, parent, args=(), obj=_dummy, sort_value=None):
-        super(UnitTestFunction, self).__init__(name, parent)
-        self._args = args
-        if obj is not _dummy:
-            self._obj = obj
-        self._sort_value = sort_value
-        if hasattr(self.parent, 'newinstance'):
-            self.parent.newinstance()
-            self.obj = self._getobj()
-
+    def addError(self, testcase, rawexcinfo):
+        py.builtin._reraise(*rawexcinfo)
+    def addFailure(self, testcase, rawexcinfo):
+        py.builtin._reraise(*rawexcinfo)
+    def addSuccess(self, testcase):
+        pass
+    def stopTest(self, testcase):
+        pass
     def runtest(self):
-        target = self.obj
-        args = self._args
-        target(*args)
-
-    def setup(self):
-        instance = py.builtin._getimself(self.obj)
-        instance.setUp()
-
-    def teardown(self):
-        instance = py.builtin._getimself(self.obj)
-        instance.tearDown()
-
+        testcase = self.parent.obj(self.name)
+        testcase(result=self)

--- 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.dev17',
+        version='2.0.0.dev18',
         url='http://pytest.org',
         license='MIT license',
         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],



More information about the pytest-commit mailing list