Reputation: 1508
I've been trying to test a Flask API, I was able to minimize the boilerplate for each test significantly by inheriting from a template class which covers app and database connections. What I haven't figured out is how to set a session object before each test.
I've seen the example of how to handle test sessions but I'd like to hide it in a decorator or in the unittest class setup if possible.
Unittest class setup:
class TestingTemplate(unittest.TestCase):
@classmethod
def setUpClass(self):
""" Sets up a test database before each set of tests """
setup_db('localhost', 28015, 'TEST',
datasets = test_dataset,
app_tables = test_tables)
self.rdb = rethinkdb.connect(
host = 'localhost',
port = 28015,
db = 'TEST')
self.rdb.use('TEST')
app.config['RDB_DB'] = 'TEST'
self.app = app.test_client()
Failing test class:
def admin_session(fn):
def run_test(self):
with self.app.session_transaction() as sess:
sess['role'] = 'admin'
fn(self)
return run_test
class TestReview(template.TestingTemplate):
""" Tests the API endpoints associated with handling reviews. """
@admin_session
def test_create_success(self):
""" Tests a successful review creation """
# creating review
review = {'company': 'test', 'rating':10}
resp = self.app.post('/review/create/123', data=json.dumps(review))
# testing creation
self.assertEqual(resp.status_code, 201)
resp_data = json.loads(resp.data)
self.assertEqual(resp_data['message'], 'review created')
Error thrown:
======================================================================
ERROR: test_create_success (test_reviews.TestReview)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/vagrant/src/server/testing/test_reviews.py", line 11, in run_test
with self.app.session_transaction() as sess:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/flask/testing.py", line 74, in session_transaction
raise RuntimeError('Session backend did not open a session. '
RuntimeError: Session backend did not open a session. Check the configuration
Any ideas on how to set a session cookie before each test without the double with statement boilerplate?
Upvotes: 2
Views: 2365
Reputation: 14210
I tend to do this sort of thing with a helper method to replace the get/post methods:
class MyTestCase(unittest.TestCase):
def request_with_role(self, path, method='GET', role='admin', *args, **kwargs):
'''
Make an http request with the given role in the session
'''
with self.app.test_client() as c:
with c.session_transaction() as sess:
sess['role'] = role
kwargs['method'] = method
kwargs['path'] = path
return c.open(*args, **kwargs)
def test_my_thing(self):
review = {'company': 'test', 'rating':10}
resp = self.request_with_role(
'/review/create/123',
method='POST',
data=json.dumps(review),
)
....
You could also have something like request_as_user
, which takes your user object and sets up the session correctly for that user:
session['_id'] = user.id
session['role'] = user.role
Upvotes: 2