Changeset - 91b89797a775
[Not reviewed]
default
0 1 0
Thomas De Schampheleire - 10 years ago 2016-03-15 17:55:19
thomas.de.schampheleire@gmail.com
pytest migration: add temporary assertRaises wrapper

Similar to the other temporary unittest-assert wrappers, add the missing
assertRaises and forward to pytest.raises.
1 file changed with 3 insertions and 0 deletions:
0 comments (0 inline, 0 general)
kallithea/tests/__init__.py
Show inline comments
 
@@ -179,96 +179,99 @@ class BaseTestCase(unittest.TestCase):
 
        unittest.TestCase.__init__(self, *args, **kwargs)
 

	
 
class BaseTestController(object):
 
    """Base test controller used by pytest and unittest tests controllers."""
 

	
 
    # Note: pytest base classes cannot have an __init__ method
 

	
 
    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:
 
            pytest.fail('could not login using %s %s' % (username, password))
 

	
 
        self.assertEqual(response.status, '302 Found')
 
        self.assert_authenticated_user(response, username)
 

	
 
        response = response.follow()
 
        return response.session['authuser']
 

	
 
    def _get_logged_user(self):
 
        return User.get_by_username(self._logged_username)
 

	
 
    def assert_authenticated_user(self, response, expected_username):
 
        cookie = response.session.get('authuser')
 
        user = cookie and cookie.get('user_id')
 
        user = user and User.get(user)
 
        user = user and user.username
 
        self.assertEqual(user, expected_username)
 

	
 
    def authentication_token(self):
 
        return self.app.get(url('authentication_token')).body
 

	
 
    def checkSessionFlash(self, response, msg=None, skip=0, _matcher=lambda msg, m: msg in m):
 
        if 'flash' not in response.session:
 
            pytest.fail(safe_str(u'msg `%s` not found - session has no flash:\n%s' % (msg, response)))
 
        try:
 
            level, m = response.session['flash'][-1 - skip]
 
            if _matcher(msg, m):
 
                return
 
        except IndexError:
 
            pass
 
        pytest.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
 
    def assertRaises(self, exception, method):
 
        with pytest.raises(exception):
 
            method()
0 comments (0 inline, 0 general)