python
runtime_error
ai_generated
true
RuntimeError: Working outside of application context.
ID: python/pytest-flask-app-context-missing
80%Fix Rate
89%Confidence
0Evidence
2025-07-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
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.
generic中文
测试尝试访问 Flask 的 current_app 或其他上下文局部对象,但没有活动的应用程序上下文。这通常发生在测试使用 Flask 全局变量的代码时,不在请求或应用上下文中。
Workarounds
-
98% success
def test_something(app): with app.app_context(): # code that uses current_app ... -
95% success
def test_something(app): with app.app_context(): ... -
90% success
def test_request(client): response = client.get('/') ...
Dead Ends
Common approaches that don't work:
-
90% fail
This enables testing mode but does not create an application context.
-
85% fail
The context must be pushed (with app.test_request_context():) to be active.
-
95% fail
This still requires an active context; it just returns the object if one exists.