[Pytest-commit] commit/pytest: magopian: refs #322: setUpClass and tearDownClass as autouse fixture and finalizer

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Thu Aug 1 23:55:31 CEST 2013


1 new commit in pytest:

https://bitbucket.org/hpk42/pytest/commits/4f2b83a6082c/
Changeset:   4f2b83a6082c
User:        magopian
Date:        2013-08-01 23:48:40
Summary:     refs #322: setUpClass and tearDownClass as autouse fixture and finalizer
Affected #:  4 files

diff -r 9125613a1821649a71162cc7f3635d63d3be3e4b -r 4f2b83a6082cffe3ddec003703480611b9f5062b _pytest/pytester.py
--- a/_pytest/pytester.py
+++ b/_pytest/pytester.py
@@ -11,6 +11,12 @@
 from py.builtin import print_
 from _pytest.core import HookRelay
 
+
+def get_public_names(l):
+    """Only return names from iterator l without a leading underscore."""
+    return [x for x in l if x[0] != "_"]
+
+
 def pytest_addoption(parser):
     group = parser.getgroup("pylib")
     group.addoption('--no-tools-on-path',

diff -r 9125613a1821649a71162cc7f3635d63d3be3e4b -r 4f2b83a6082cffe3ddec003703480611b9f5062b _pytest/unittest.py
--- a/_pytest/unittest.py
+++ b/_pytest/unittest.py
@@ -5,19 +5,38 @@
 # for transfering markers
 from _pytest.python import transfer_markers
 
-def pytest_pycollect_makeitem(collector, name, obj):
+
+def is_unittest(obj):
+    """Is obj a subclass of unittest.TestCase?"""
     unittest = sys.modules.get('unittest')
     if unittest is None:
-        return # nobody can have derived unittest.TestCase
+        return  # nobody can have derived unittest.TestCase
     try:
-        isunit = issubclass(obj, unittest.TestCase)
+        return issubclass(obj, unittest.TestCase)
     except KeyboardInterrupt:
         raise
-    except Exception:
-        pass
-    else:
-        if isunit:
-            return UnitTestCase(name, parent=collector)
+    except:
+        return False
+
+
+ at pytest.fixture(scope='class', autouse=True)
+def _xunit_setUpClass(request):
+    """Add support for unittest.TestCase setUpClass and tearDownClass."""
+    if not is_unittest(request.cls):
+        return  # only support setUpClass / tearDownClass for unittest.TestCase
+    if getattr(request.cls, '__unittest_skip__', False):
+        return  # skipped
+    setup = getattr(request.cls, 'setUpClass', None)
+    teardown = getattr(request.cls, 'tearDownClass', None)
+    setup()
+    if teardown is not None:
+        request.addfinalizer(teardown)
+
+
+def pytest_pycollect_makeitem(collector, name, obj):
+    if is_unittest(obj):
+        return UnitTestCase(name, parent=collector)
+
 
 class UnitTestCase(pytest.Class):
     nofuncargs = True  # marker for fixturemanger.getfixtureinfo()
@@ -45,21 +64,7 @@
                 if ut is None or runtest != ut.TestCase.runTest:
                     yield TestCaseFunction('runTest', parent=self)
 
-    def setup(self):
-        if getattr(self.obj, '__unittest_skip__', False):
-            return
-        meth = getattr(self.obj, 'setUpClass', None)
-        if meth is not None:
-            meth()
-        super(UnitTestCase, self).setup()
 
-    def teardown(self):
-        if getattr(self.obj, '__unittest_skip__', False):
-            return
-        meth = getattr(self.obj, 'tearDownClass', None)
-        if meth is not None:
-            meth()
-        super(UnitTestCase, self).teardown()
 
 class TestCaseFunction(pytest.Function):
     _excinfo = None

diff -r 9125613a1821649a71162cc7f3635d63d3be3e4b -r 4f2b83a6082cffe3ddec003703480611b9f5062b testing/python/fixture.py
--- a/testing/python/fixture.py
+++ b/testing/python/fixture.py
@@ -1,6 +1,8 @@
 import pytest, py, sys
 from _pytest import python as funcargs
 from _pytest.python import FixtureLookupError
+from _pytest.pytester import get_public_names
+
 
 def test_getfuncargnames():
     def f(): pass
@@ -50,7 +52,7 @@
         """)
         funcargs.fillfixtures(item)
         del item.funcargs["request"]
-        assert len(item.funcargs) == 2
+        assert len(get_public_names(item.funcargs)) == 2
         assert item.funcargs['some'] == "test_func"
         assert item.funcargs['other'] == 42
 
@@ -334,7 +336,7 @@
         assert val2 == 2
         pytest._fillfuncargs(item)
         assert item.funcargs["something"] == 1
-        assert len(item.funcargs) == 2
+        assert len(get_public_names(item.funcargs)) == 2
         assert "request" in item.funcargs
         #assert item.funcargs == {'something': 1, "other": 2}
 
@@ -412,6 +414,7 @@
     def test_request_fixturenames(self, testdir):
         testdir.makepyfile("""
             import pytest
+            from _pytest.pytester import get_public_names
             @pytest.fixture()
             def arg1():
                 pass
@@ -422,7 +425,7 @@
             def sarg(tmpdir):
                 pass
             def test_function(request, farg):
-                assert set(request.fixturenames) == \
+                assert set(get_public_names(request.fixturenames)) == \
                        set(["tmpdir", "sarg", "arg1", "request", "farg"])
         """)
         reprec = testdir.inline_run()
@@ -831,6 +834,8 @@
         l = reprec.getfailedcollections()
         assert len(l) == 1
 
+    @pytest.mark.xfail(reason="unclear if it should be supported at all, "
+                              "currently broken")
     def test_request_can_be_overridden(self, testdir):
         testdir.makepyfile("""
             import pytest
@@ -995,9 +1000,10 @@
 
     def test_parsefactories_conftest(self, testdir):
         testdir.makepyfile("""
+            from _pytest.pytester import get_public_names
             def test_check_setup(item, fm):
                 autousenames = fm._getautousenames(item.nodeid)
-                assert len(autousenames) == 2
+                assert len(get_public_names(autousenames)) == 2
                 assert "perfunction2" in autousenames
                 assert "perfunction" in autousenames
         """)

diff -r 9125613a1821649a71162cc7f3635d63d3be3e4b -r 4f2b83a6082cffe3ddec003703480611b9f5062b testing/test_unittest.py
--- a/testing/test_unittest.py
+++ b/testing/test_unittest.py
@@ -118,7 +118,7 @@
     assert passed == 2
     assert passed + skipped + failed == 2
 
- at pytest.mark.skipif("sys.version_info < (3,1)")
+ at pytest.mark.skipif("sys.version_info < (2,7)")
 def test_unittest_skip_issue148(testdir):
     testpath = testdir.makepyfile("""
         import unittest
@@ -586,3 +586,53 @@
     """)
     result = testdir.runpytest()
     result.stdout.fnmatch_lines("*3 passed*")
+
+
+def test_non_unittest_no_setupclass_support(testdir):
+    testpath = testdir.makepyfile("""
+        class TestFoo:
+            x = 0
+
+            @classmethod
+            def setUpClass(cls):
+                cls.x = 1
+
+            def test_method1(self):
+                assert self.x == 0
+
+            @classmethod
+            def tearDownClass(cls):
+                cls.x = 1
+
+        def test_not_teareddown():
+            assert TestFoo.x == 0
+
+    """)
+    reprec = testdir.inline_run(testpath)
+    reprec.assertoutcome(passed=2)
+
+
+def test_no_teardown_if_setupclass_failed(testdir):
+    testpath = testdir.makepyfile("""
+        import unittest
+
+        class MyTestCase(unittest.TestCase):
+            x = 0
+
+            @classmethod
+            def setUpClass(cls):
+                cls.x = 1
+                assert False
+
+            def test_func1(self):
+                cls.x = 10
+
+            @classmethod
+            def tearDownClass(cls):
+                cls.x = 100
+
+        def test_notTornDown():
+            assert MyTestCase.x == 1
+    """)
+    reprec = testdir.inline_run(testpath)
+    reprec.assertoutcome(passed=1, failed=1)

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the pytest-commit mailing list