RuntimeError: Working Outside of Application Context
When attempting to unit test the before_request and input_info functions from the app.py module in test.py, a RuntimeError is encountered, indicating that the functions are being called outside of the application context.
To resolve this issue, the app.app_context() context manager must be used to create a testing application context within the unit tests. This context manager provides the necessary environment for the before_request and input_info functions to execute correctly.
Here's an example of how to use the app.app_context() context manager in the unit test:
from app import * class Test(unittest.TestCase): def test_connection1(self): with app.app_context(): object = TestMySQL() object.before_request() # Should no longer throw a RuntimeError
In this test, the app.app_context() context manager wraps the call to the before_request function, creating the necessary test application context and resolving the RuntimeError.
The above is the detailed content of How to Resolve "RuntimeError: Working Outside of Application Context" in Unit Testing?. For more information, please follow other related articles on the PHP Chinese website!