Python 通过conftest.py(test_client)创建的FlaskClient不在请求上下文中
这就是我如何为test_客户机创建fixture的方法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
@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'>>