我正在使用Pytest來測試Flask + SQLAlchemy應用程序。這是tests/contftest.py
RecursionError:Flask Pytest中超出最大遞歸深度
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from flask import _app_ctx_stack
from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery
from package.myapp import create_app
from package.config import DefaultConfig
DbSession = scoped_session(
sessionmaker(),
scopefunc=_app_ctx_stack.__ident_func__
)
@pytest.fixture(scope='session')
def app(request):
_app = create_app()
_app.debug = False
_app.engine = create_engine(_app.config['SQLALCHEMY_DATABASE_URI'], connect_args={"options": "-c timezone=utc"})
global DbSession
DbSession.configure(bind=_app.engine, query_cls=BaseQuery)
# Establish an application context before running the tests.
ctx = _app.app_context()
ctx.push()
@_app.teardown_appcontext
def teardown(exception=None):
ctx.pop()
global DbSession
if DbSession:
DbSession.remove()
request.addfinalizer(teardown)
return _app
當我運行pytest
,我得到這個錯誤信息
___________________ ERROR at teardown of test_create_project ___________________
exception = None
@_app.teardown_appcontext
def teardown(exception=None):
> ctx.pop()
tests/conftest.py:31:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../.virtualenvs/quest-backend/lib/python3.6/site-packages/flask/ctx.py:189: in pop
self.app.do_teardown_appcontext(exc)
../../.virtualenvs/quest-backend/lib/python3.6/site-packages/flask/app.py:1892: in do_teardown_appcontext
func(exc)
tests/conftest.py:31: in teardown
ctx.pop()
E RecursionError: maximum recursion depth exceeded
!!! Recursion detected (same locals & position)