我正在尝试编写用于测试中止方法的测试代码,但找不到解决方案。如何访问来自werkzeug的错误消息?有人可以告诉我解决此问题的提示吗?
.flask编写的.py代码
def check_access_token(func):
@wraps(func)
def wrapper(*args, **kwargs):
...
...
if not rows_all:
abort(401, {"error_message": "access token is ineffective."})
...
...
...
使用pytest测试代码
with app.test_request_context(path, method="POST", headers=headers, data=payload) as req:
def mock():
return jsonify(["dummy"]), 200
func = check_access_token(mock)
import pdb;
pdb.set_trace()
...
...
...
调试结果
(Pdb) p func(req.request)
[]
*** werkzeug.exceptions.Unauthorized: 401 Unauthorized: {'error_message': 'access token is ineffective.'}
(Pdb)