怎么测试 errorhandler?为什么测试里 500 处理器有时不执行?异常什么时候抛进测试、什么时候变成 500?蓝图里的处理器管到哪里?这篇用一组真实运行的 pytest 用例把规则一条条验证。
test_custom_exception_handler RESULT custom 402 {'code': 'card_declined', 'error': 'payment'}
test_404_handler RESULT 404 404 {'error': 'not found'}
test_abort_403_no_handler RESULT abort403 403 text/html; charset=utf-8
test_unhandled_exception_propagates_in_testing RESULT propagate raised RuntimeError in the test
test_500_handler_runs_when_not_testing RESULT 500handler 500 {'error': 'server error'}
test_override_propagation_inside_testing RESULT propagate-off 500 {'error': 'server error'}
test_blueprint_handler_scope RESULT bp-inside 400 {'error': 'bp value error'}
RESULT bp-outside raised ValueError (blueprint handler does not apply)
test_handler_error_format_contract RESULT contract ['code', 'error']
============================== 8 passed in 0.06s ===============================上一篇:测试文件上传;下一篇:测试 Blueprint。
第 102 篇讲过统一错误格式的写法,这一篇讲怎么测试它。