changeset 5709:877bcf22bf71

pytest migration: introduce TestControllerPytest In order to allow tests to benefit from pytest specific functionality, like fixtures, they can no longer derive from unittest.TestCase. What's more, while they can derive from any user-defined class, none of the classes involved (the test class itself nor any of the base classes) can have an __init__ method. Converting all tests from unittest-style to pytest-style in one commit is not realistic. Hence, a more gradual approach is needed. Most existing test classes derive from TestController, which in turn derives from BaseTestCase, which derives from unittest.TestCase. Some test classes derive directly from BaseTestCase. Supporting both unittest-style and pytest-style from TestController directly is not possible: pytest-style _cannot_ and unittest-style _must_ derive from unittest.TestCase. Thus, in any case, an extra level in the class hierarchy is needed (TestController deriving from Foo and from unittest.TestCase; pytest-style test classes would then directly derive from Foo). The requirement that pytest-style test classes cannot have an __init__ method anywhere in the class hierarchy imposes another restriction that makes it difficult to support both unittest-style and pytest-style test classes with one class. Any init code needs to be placed in another method than __init__ and be called explicitly when the test class is initialized. For unittest-style test classes this would naturally be done with a setupClass method, but several test classes already use that. Thus, there would need to be explicit 'super' calls from the test classes. This is technically possible but not very nice. A more transparent approach (from the existing test classes point of view), implemented by this patch, works as follows: - the implementation of the existing TestController class is now put under a new class BaseTestController. To accomodate pytest, the __init__ method is renamed init. - contrary to the original TestController, BaseTestController does not derive from BaseTestCase (and neither from unittest.TestCase). Instead, the 'new' TestController derives both from BaseTestCase, which is untouched, and from BaseTestController. - TestController has an __init__ method that calls the base classes' __init__ methods and the renamed 'init' method of BaseTestController. - a new class TestControllerPytest is introduced that derives from BaseTestController but not from BaseTestCase. It uses a pytest fixture to automatically call the setup functionality previously provided by BaseTestCase and also calls 'init' on BaseTestController. This means a little code duplication but is hard to avoid. The app setup fixture is scoped on the test method, which means that the app is recreated for every test (unlike for the unittest-style tests where the app is created per test class). This has the advantage of detecting current inter-test dependencies and thus improve the health of our test suite. This in turn is one step closer to allowing parallel test execution. The unittest-style assert methods (assertEqual, assertIn, ...) do not exist for pytest-style tests. To avoid having to change all existing test cases upfront, provide transitional implementations of these methods. The conversion of the unittest asserts to the pytest/python asserts can happen gradually over time.
author Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
date Wed, 10 Feb 2016 18:28:42 +0100
parents 2cc8d876d1c8
children 9211cc737587
files kallithea/tests/__init__.py
diffstat 1 files changed, 47 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/tests/__init__.py	Thu Feb 11 12:13:50 2016 +0100
+++ b/kallithea/tests/__init__.py	Wed Feb 10 18:28:42 2016 +0100
@@ -60,7 +60,7 @@
 skipif = pytest.mark.skipif
 
 __all__ = [
-    'skipif', 'parameterized', 'environ', 'url', 'TestController',
+    'skipif', 'parameterized', 'environ', 'url', 'TestController', 'TestControllerPytest',
     'ldap_lib_installed', 'pam_lib_installed', 'BaseTestCase', 'init_stack',
     'TESTS_TMP_PATH', 'HG_REPO', 'GIT_REPO', 'NEW_HG_REPO', 'NEW_GIT_REPO',
     'HG_FORK', 'GIT_FORK', 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_ADMIN_PASS',
@@ -170,18 +170,19 @@
     Session().commit()
 
 class BaseTestCase(unittest.TestCase):
+    """Unittest-style base test case. Deprecated in favor of pytest style."""
+
     def __init__(self, *args, **kwargs):
         self.wsgiapp = pylons.test.pylonsapp
         init_stack(self.wsgiapp.config)
         unittest.TestCase.__init__(self, *args, **kwargs)
 
-
-
+class BaseTestController(object):
+    """Base test controller used by pytest and unittest tests controllers."""
 
-class TestController(BaseTestCase):
+    # Note: pytest base classes cannot have an __init__ method
 
-    def __init__(self, *args, **kwargs):
-        BaseTestCase.__init__(self, *args, **kwargs)
+    def init(self):
         self.app = TestApp(self.wsgiapp)
         self.maxDiff = None
         self.index_location = config['app_conf']['index_dir']
@@ -230,3 +231,43 @@
 
     def checkSessionFlashRegex(self, response, regex, skip=0):
         self.checkSessionFlash(response, regex, skip=skip, _matcher=re.search)
+
+class TestController(BaseTestCase, BaseTestController):
+    """Deprecated unittest-style test controller"""
+
+    def __init__(self, *args, **kwargs):
+        super(TestController, self).__init__(*args, **kwargs)
+        self.init()
+
+class TestControllerPytest(BaseTestController):
+    """Pytest-style test controller"""
+
+    # Note: pytest base classes cannot have an __init__ method
+
+    @pytest.fixture(autouse=True)
+    def app_fixture(self):
+        self.wsgiapp = pylons.test.pylonsapp
+        init_stack(self.wsgiapp.config)
+        self.init()
+        return self.app
+
+    # transitional implementations of unittest.TestCase asserts
+    # Users of these should be converted to pytest's single 'assert' call
+    def assertEqual(self, first, second, msg=None):
+        assert first == second
+    def assertNotEqual(self, first, second, msg=None):
+        assert first != second
+    def assertTrue(self, expr, msg=None):
+        assert bool(expr) is True
+    def assertFalse(self, expr, msg=None):
+        assert bool(expr) is False
+    def assertIn(self, first, second, msg=None):
+        assert first in second
+    def assertNotIn(self, first, second, msg=None):
+        assert first not in second
+    def assertSetEqual(self, first, second, msg=None):
+        assert first == second
+    def assertListEqual(self, first, second, msg=None):
+        assert first == second
+    def assertDictEqual(self, first, second, msg=None):
+        assert first == second