Mercurial > hg > ltpdarepo
changeset 73:3d5850d65603
Factor out test case setup code for tests requiring the application request context.
author | Daniele Nicolodi <daniele@grinta.net> |
---|---|
date | Mon, 15 Aug 2011 20:06:25 +0200 |
parents | 90c1fa07f6a7 |
children | b8c1ce741745 |
files | src/ltpdarepo/tests/test_users.py src/ltpdarepo/tests/utils.py |
diffstat | 2 files changed, 28 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ltpdarepo/tests/test_users.py Mon Aug 15 20:05:59 2011 +0200 +++ b/src/ltpdarepo/tests/test_users.py Mon Aug 15 20:06:25 2011 +0200 @@ -1,6 +1,7 @@ import unittest import MySQLdb as mysql +from ltpdarepo.tests.utils import RequestContextTestCase from ltpdarepo.user import User, _generate_password @@ -31,27 +32,17 @@ self.assertEqual(u['name'], 'Foo') -class DatabaseTestCase(unittest.TestCase): +class DatabaseTestCase(RequestContextTestCase): def setUp(self): from ltpdarepo.admin import wipe, install, useradd wipe() install() useradd('u1', 'u1') - - from ltpdarepo import app - self.app = app - self.app.config.update(HOSTNAME='localhost') - # fake request - self.ctx = self.app.test_request_context() - self.ctx.push() - # execute before request handlers - self.app.preprocess_request() + super(DatabaseTestCase, self).setUp() def tearDown(self): - # execute after request handlers - self.app.process_response(self.app.response_class()) - self.ctx.pop() + super(DatabaseTestCase, self).tearDown() from ltpdarepo.admin import wipe wipe()
--- a/src/ltpdarepo/tests/utils.py Mon Aug 15 20:05:59 2011 +0200 +++ b/src/ltpdarepo/tests/utils.py Mon Aug 15 20:06:25 2011 +0200 @@ -1,5 +1,23 @@ +import unittest2 as unittest import zope.testbrowser.wsgi +class RequestContextTestCase(unittest.TestCase): + + def setUp(self): + # application + from ltpdarepo import app + self.app = app + # fake request + self.ctx = self.app.test_request_context() + self.ctx.push() + # execute before request handlers + self.app.preprocess_request() + + def tearDown(self): + # execute after request handlers + self.app.process_response(self.app.response_class()) + self.ctx.pop() + class Browser(zope.testbrowser.wsgi.Browser): def __init__(self, url='http://localhost/'): @@ -13,3 +31,9 @@ app.logger.addHandler(handler) super(Browser, self).__init__(url, wsgi_app=app) + + def login(self, username, password): + self.open('/login') + self.getControl(name='username').value = username + self.getControl(name='password').value = password + self.getControl(name='login').click()