Flask单元测试会议装饰

时间:2013-11-28 17:45:12

标签: python unit-testing session testing flask

我一直在尝试测试Flask API,通过继承涵盖应用和数据库连接的模板类,我能够显着减少每个测试的样板。我没想到的是如何在每次测试之前设置会话对象。

我已经看过how to handle test sessions的示例,但如果可能的话,我想将其隐藏在装饰器或unittest类设置中。

Unittest类设置:

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()

测试类失败:

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: 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

关于如何在每次测试之前设置会话cookie而没有double with语句样板的任何想法?

1 个答案:

答案 0 :(得分:2)

我倾向于使用辅助方法来替换get / post方法:

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),
        )
        ....

您还可以使用request_as_user之类的内容,它会获取您的用户对象并为该用户正确设置会话:

session['_id'] = user.id
session['role'] = user.role