Python:pytest 访问 Flask 会话和请求上下文变量
作者:互联网
上述帖子中的测试客户端夹具:
@pytest.fixture(scope='module')
def test_client( app ):
"""
Creates a test client.
app.test_client() is able to submit HTTP requests.
The app argument is the app() fixture above.
"""
with app.test_client() as testing_client:
yield testing_client # Return to caller.
在测试访问Flask 的上下文变量 session的代码时,上述夹具将不起作用。要访问这个变量,官方文档指出:
如果您想在发出请求之前访问或设置会话中的值,请在with语句中使用客户端的session_transaction()方法。它返回一个会话对象,并在块结束后保存会话。
https://flask.palletsprojects.com/en/2.2.x/testing/
我试过了,但它不起作用……这是一个“流行”的问题。它已经存在了几年。上面有一些帖子,但是,大多数建议对我不起作用。
测试 #69引发此问题时,会话为空,用户russmac提出了一个解决方案:
@pytest.fixture(scope='module')
def test_client( app ):
"""
Creates a test client.
app.test_client() is able to submit HTTP requests.
The app argument is the app() fixture above.
"""
with app.test_client() as testing_client:
"""
See: https://github.com/pytest-dev/pytest-flask/issues/69
Sessions are empty when testing #69
"""
with testing_client.session_transaction() as session:
session['Authorization'] = 'redacted'
yield testing_client # Return to caller.
这对我有用。但是,要访问可变会话,我的测试代码必须在:
with app.test_request_context():
否则,它会引发错误RuntimeError: Working outside of request context。此外,如果没有上述调用,正确的代码将无法在测试期间访问请求变量。
以下是我正在处理的项目的正确测试方法:
@pytest.mark.timesheet_bro
def test_close( app ):
bro_obj = TimesheetBRO( 1 )
#
# RuntimeError: Working outside of request context.
#
# session[ 'user_id' ] = 100
#
with app.test_request_context( '?searchType={}'.format(UNRESTRICT_SEARCH_TYPE) ):
assert request.args[ 'searchType' ] == UNRESTRICT_SEARCH_TYPE
assert request.values.get( 'searchType' ) == UNRESTRICT_SEARCH_TYPE
session[ 'user_id' ] = 1
data = bro_obj.close( 326 )
assert bro_obj.last_message == ''
assert data['status']['code'] == HTTPStatus.OK.value
请注意,在正确的代码中,会话['user_id']是在用户成功登录后设置的。在测试代码中设置此值以为正在测试的代码创建正确的先决条件。另请注意,request.values.get('searchType')也用于正在测试的代码中。
@pytest.fixture(scope='module')
def test_client( app ):
"""
Creates a test client.
app.test_client() is able to submit HTTP requests.
The app argument is the app() fixture above.
"""
with app.test_client() as testing_client:
yield testing_client # Return to caller.
在测试访问Flask 的上下文变量 session的代码时,上述夹具将不起作用。要访问这个变量,官方文档指出:
如果您想在发出请求之前访问或设置会话中的值,请在with语句中使用客户端的session_transaction()方法。它返回一个会话对象,并在块结束后保存会话。
https://flask.palletsprojects.com/en/2.2.x/testing/
我试过了,但它不起作用……这是一个“流行”的问题。它已经存在了几年。上面有一些帖子,但是,大多数建议对我不起作用。
测试 #69引发此问题时,会话为空,用户russmac提出了一个解决方案:
@pytest.fixture(scope='module')
def test_client( app ):
"""
Creates a test client.
app.test_client() is able to submit HTTP requests.
The app argument is the app() fixture above.
"""
with app.test_client() as testing_client:
"""
See: https://github.com/pytest-dev/pytest-flask/issues/69
Sessions are empty when testing #69
"""
with testing_client.session_transaction() as session:
session['Authorization'] = 'redacted'
yield testing_client # Return to caller.
这对我有用。但是,要访问可变会话,我的测试代码必须在:
with app.test_request_context():
否则,它会引发错误RuntimeError: Working outside of request context。此外,如果没有上述调用,正确的代码将无法在测试期间访问请求变量。
以下是我正在处理的项目的正确测试方法:
@pytest.mark.timesheet_bro
def test_close( app ):
bro_obj = TimesheetBRO( 1 )
#
# RuntimeError: Working outside of request context.
#
# session[ 'user_id' ] = 100
#
with app.test_request_context( '?searchType={}'.format(UNRESTRICT_SEARCH_TYPE) ):
assert request.args[ 'searchType' ] == UNRESTRICT_SEARCH_TYPE
assert request.values.get( 'searchType' ) == UNRESTRICT_SEARCH_TYPE
session[ 'user_id' ] = 1
data = bro_obj.close( 326 )
assert bro_obj.last_message == ''
assert data['status']['code'] == HTTPStatus.OK.value
请注意,在正确的代码中,会话['user_id']是在用户成功登录后设置的。在测试代码中设置此值以为正在测试的代码创建正确的先决条件。另请注意,request.values.get('searchType')也用于正在测试的代码中。