python
runtime_error
ai_generated
true
RuntimeError: 在应用程序上下文之外工作。
RuntimeError: Working outside of application context.
ID: python/pytest-flask-app-context-missing
80%修复率
89%置信度
0证据数
2025-07-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
测试尝试访问 Flask 的 current_app 或其他上下文局部对象,但没有活动的应用程序上下文。这通常发生在测试使用 Flask 全局变量的代码时,不在请求或应用上下文中。
English
A test tries to access Flask's current_app or other context-local objects without an active application context. This often occurs when testing code that uses Flask globals outside a request or app context.
解决方案
-
98% 成功率
def test_something(app): with app.app_context(): # code that uses current_app ... -
95% 成功率
def test_something(app): with app.app_context(): ... -
90% 成功率
def test_request(client): response = client.get('/') ...
无效尝试
常见但无效的做法:
-
90% 失败
This enables testing mode but does not create an application context.
-
85% 失败
The context must be pushed (with app.test_request_context():) to be active.
-
95% 失败
This still requires an active context; it just returns the object if one exists.