RuntimeError: working outside of application context

后端 未结 2 2040
情歌与酒
情歌与酒 2020-12-01 10:03

app.py

from flask import Flask, render_template, request,jsonify,json,g
import mysql.connector

app = Flask(__name__)
**class TestMySQL():**         


        
相关标签:
2条回答
  • 2020-12-01 10:36

    I followed @brenns10 's answer when I ran into a similar problem when using pytest.

    I followed the suggestion of putting it into test setup, this works:

    import pytest
    from src.app import app
    
    
    @pytest.fixture
    def app_context():
        with app.app_context():
            yield
    
    
    def some_test(app_context):
        <test code that needs the app context>
    
    0 讨论(0)
  • 2020-12-01 10:45

    Flask has an Application Context, and it seems like you'll need to do something like:

    def test_connection(self):
        with app.app_context():
            #test code
    

    You can probably also shove the app.app_context() call into a test setup method as well. Hope this helps.

    0 讨论(0)
提交回复
热议问题