Changeset - 877bcf22bf71
[Not reviewed]
default
0 1 0
Thomas De Schampheleire - 10 years ago 2016-02-10 18:28:42
thomas.de.schampheleire@gmail.com
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.
1 file changed with 47 insertions and 6 deletions:
0 comments (0 inline, 0 general)
kallithea/tests/__init__.py
Show inline comments
 
@@ -51,25 +51,25 @@ from kallithea.tests.parameterized impor
 
from kallithea.lib.utils2 import safe_str
 

	
 

	
 
os.environ['TZ'] = 'UTC'
 
if not is_windows:
 
    time.tzset()
 

	
 
log = logging.getLogger(__name__)
 

	
 
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',
 
    'TEST_USER_ADMIN_EMAIL', 'TEST_USER_REGULAR_LOGIN', 'TEST_USER_REGULAR_PASS',
 
    'TEST_USER_REGULAR_EMAIL', 'TEST_USER_REGULAR2_LOGIN',
 
    'TEST_USER_REGULAR2_PASS', 'TEST_USER_REGULAR2_EMAIL', 'TEST_HG_REPO',
 
    'TEST_HG_REPO_CLONE', 'TEST_HG_REPO_PULL', 'TEST_GIT_REPO',
 
    'TEST_GIT_REPO_CLONE', 'TEST_GIT_REPO_PULL', 'HG_REMOTE_REPO',
 
    'GIT_REMOTE_REPO', 'SCM_TESTS', 'remove_all_notifications',
 
]
 

	
 
# Invoke websetup with the current config file
 
@@ -161,36 +161,37 @@ def init_stack(config=None):
 
    logging.getLogger("kallithea").addHandler(h)
 

	
 
def remove_all_notifications():
 
    Notification.query().delete()
 

	
 
    # Because query().delete() does not (by default) trigger cascades.
 
    # http://docs.sqlalchemy.org/en/rel_0_7/orm/collections.html#passive-deletes
 
    UserNotification.query().delete()
 

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

	
 
    def log_user(self, username=TEST_USER_ADMIN_LOGIN,
 
                 password=TEST_USER_ADMIN_PASS):
 
        self._logged_username = username
 
        response = self.app.post(url(controller='login', action='index'),
 
                                 {'username': username,
 
                                  'password': password})
 

	
 
        if 'Invalid username or password' in response.body:
 
@@ -221,12 +222,52 @@ class TestController(BaseTestCase):
 
        try:
 
            level, m = response.session['flash'][-1 - skip]
 
            if _matcher(msg, m):
 
                return
 
        except IndexError:
 
            pass
 
        self.fail(safe_str(u'msg `%s` not found in session flash (skipping %s): %s' %
 
                           (msg, skip,
 
                            ', '.join('`%s`' % m for level, m in response.session['flash']))))
 

	
 
    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
0 comments (0 inline, 0 general)