【问题标题】:Flask testing - "RuntimeError: No application found” when trying to connect with db with SQLAlchemy烧瓶测试 - 尝试使用 SQLAlchemy 连接数据库时出现“RuntimeError:未找到应用程序”
【发布时间】:2018-12-16 11:07:00
【问题描述】:

我正在使用 pytest 编写测试代码,以测试使用 python 3 编写的 API。但我正在努力解决以下错误消息。 python 代码在没有 pytest 的情况下可以正常工作,但只有在执行 pytest 代码时才会出现错误消息。我不认为问题来自低级问题,尽管它似乎是。老实说,这个错误背后的真正含义对我来说没有意义。这次的上下文似乎也与这个问题无关。 (http://flask-sqlalchemy.pocoo.org/contexts/)

谁能给我一些解决方案的提示或提示?

* 我已经尝试根据这个问题('No application found. Either work inside a view function or push an application context.')解决这个问题,但是在这个代码上它不起作用。

可能与问题相关的代码部分。 (test_auth.py)

    app = Flask(__name__)
    app.config.from_object(test)
    db_acs.init_app(app)
    logger = logging.getLogger(__name__)
    init_app(app, db_acs, logger)

错误

=========================================================================================== FAILURES ============================================================================================
____________________________________________________________________________________ test______________________________________________________________________________________

    def test():
        sqlpath = app.config['SQL_PATH']['getid']
        input_id = 'xxx'

>       rows = db_acs.dba(sqlpath, s1=input_id)

test_auth.py:68: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../xxx/xxx/db/dba.py:33: in dba
    connection = db.engine.connect()
../../../.pyenv/versions/3.6.5/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:877: in engine
    return self.get_engine()
../../../.pyenv/versions/3.6.5/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:886: in get_engine
    app = self.get_app(app)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <SQLAlchemy engine=None>, reference_app = None

    def get_app(self, reference_app=None):
        """Helper method that implements the logic to look up an
            application."""

        if reference_app is not None:
            return reference_app

        if current_app:
            return current_app._get_current_object()

        if self.app is not None:
            return self.app

        raise RuntimeError(
>           'No application found. Either work inside a view function or push'
            ' an application context. See'
            ' http://flask-sqlalchemy.pocoo.org/contexts/.'
        )
E       RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

【问题讨论】:

    标签: python unit-testing flask sqlalchemy pytest


    【解决方案1】:

    来自 Flask 文档中的 App Context 页面:

    应用程序上下文在请求、CLI 命令或其他活动期间跟踪应用程序级数据。 […] 推送请求上下文时会推送应用程序上下文。

    您可以通过以下方式推送应用程序上下文: (1) 使用 app.app_context() 手动推送上下文并在 with 块内访问它(正如 huang 已经演示的那样); (2) 或使用test_client() 创建任意请求 - 请参阅下面的示例。

    conftest.py:

    @pytest.fixture
    def client(app):
        """A test client for the app."""
        return app.test_client()
    

    在您的测试文件中(例如 test_auth.py):

    def test_something(client):
        with client:
            client.get("/")  # arbitrary request to push the app context
            # test/access the app context here
    

    我还建议您查看 Flask 文档中的official Flask test examples(test_blog.py 有许多相关示例)和Testing Flask Applications(特别是“访问和修改会话”部分)。

    【讨论】:

      【解决方案2】:

      在文件conftest.py中,可以这样写:

      @pytest.fixture(scope="session")
      def app():
          app = create_app()  # Or app = Flask(__name__)
          return app
      

      在你的 test_function 中:

      def test_f(app):
          with app.app_context():
               # do your test
      

      【讨论】:

        猜你喜欢
        • 2021-09-18
        • 2019-07-19
        • 1970-01-01
        • 2013-08-19
        • 1970-01-01
        • 1970-01-01
        • 2016-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多