Home > Database > Mysql Tutorial > How to Resolve 'RuntimeError: Working Outside of Application Context' in Unit Testing?

How to Resolve 'RuntimeError: Working Outside of Application Context' in Unit Testing?

DDD
Release: 2024-11-16 16:47:03
Original
584 people have browsed it

How to Resolve

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template