【问题标题】:Flask unittesting session decorator烧瓶单元测试会话装饰器
【发布时间】:2013-11-28 17:45:12
【问题描述】:

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

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

单元测试类设置:

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 语句样板的任何想法?

【问题讨论】:

  • 建议发布不起作用的完整测试代码。不仅仅是装饰器和异常字符串。
  • 谢谢,我太仓促发了。我添加了与此测试相关的所有代码

标签: python unit-testing session testing flask


【解决方案1】:

我倾向于用辅助方法来代替 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-24
    • 2013-04-23
    • 2021-11-25
    • 1970-01-01
    • 2012-07-14
    • 2016-12-30
    • 2015-03-15
    • 2011-03-02
    相关资源
    最近更新 更多