Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过conftest.py(test_client)创建的FlaskClient不在请求上下文中_Python_Unit Testing_Flask_Pytest_Fixtures - Fatal编程技术网

Python 通过conftest.py(test_client)创建的FlaskClient不在请求上下文中

Python 通过conftest.py(test_client)创建的FlaskClient不在请求上下文中,python,unit-testing,flask,pytest,fixtures,Python,Unit Testing,Flask,Pytest,Fixtures,这就是我如何为test_客户机创建fixture的方法 @pytest.fixture(scope='session') def test_client(app): client = app.test_client() ctx = app.app_context() ctx.push() yield client ctx.pop() 这就是我所说的第一次测试: import pytest

这就是我如何为test_客户机创建fixture的方法

    @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.
我很迷茫,因为我遵循了以下文件: 我不需要数据库连接

当我打印test_客户端以查看其是否使用夹具初始化时:

    <FlaskClient <Flask 'my_app_name'>>