通过conftest.py(test_client)创建的FlaskClient在请求上下文之外

时间:2019-07-05 16:59:40

标签: python unit-testing flask pytest fixtures

这就是我为test_client创建固定装置的方式

    @pytest.fixture(scope='session')
    def test_client(app):
        client = app.test_client()
        ctx = app.app_context()
        ctx.push()
        yield client
        ctx.pop()

这就是我所谓的第一个测试:

    import pytest


    @pytest.mark.parametrize('url, status_code', [
        ('/',  200)
    ])
    def test_api_request(url, status_code, test_client):
        res = test_client.get(url)
        assert res.status_code == status_code

我收到此错误:

    name = 'request'

        def _lookup_req_object(name):
            top = _request_ctx_stack.top
            if top is None:
    >           raise RuntimeError(_request_ctx_err_msg)
    E           RuntimeError: Working outside of request context.
    E           
    E           This typically means that you attempted to use functionality that needed
    E           an active HTTP request.  Consult the documentation on testing for
    E           information about how to avoid this problem.

我很迷茫,因为遵循了这份文件: http://flask.pocoo.org/docs/1.0/testing/ 我不需要数据库连接。

当我打印test_client以查看其是否使用固定装置初始化时:

    <FlaskClient <Flask 'my_app_name'>>

0 个答案:

没有答案