Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pytest编写的测试代码在尝试使用SQLAlchemy连接db时不起作用_Python_Unit Testing_Flask_Sqlalchemy_Pytest - Fatal编程技术网

Python 使用pytest编写的测试代码在尝试使用SQLAlchemy连接db时不起作用

Python 使用pytest编写的测试代码在尝试使用SQLAlchemy连接db时不起作用,python,unit-testing,flask,sqlalchemy,pytest,Python,Unit Testing,Flask,Sqlalchemy,Pytest,我正在用pytest编写测试代码,以测试用python3编写的API。但是我正在努力处理下面的错误信息。python代码在没有pytest的情况下正常工作,但只有在执行pytest代码时才会显示错误消息。我不认为这些问题来自于低层次的问题,尽管看起来是这样。老实说,这个错误背后的真正含义对我来说毫无意义。这一次,上下文似乎也与这个问题无关。() 有没有人能给我一些解决方法的提示 *我已经尝试基于此问题()解决问题,但在这段代码中无效 可能与问题相关的代码部分。 (测试授权) 错误 =======

我正在用pytest编写测试代码,以测试用python3编写的API。但是我正在努力处理下面的错误信息。python代码在没有pytest的情况下正常工作,但只有在执行pytest代码时才会显示错误消息。我不认为这些问题来自于低层次的问题,尽管看起来是这样。老实说,这个错误背后的真正含义对我来说毫无意义。这一次,上下文似乎也与这个问题无关。()

有没有人能给我一些解决方法的提示

*我已经尝试基于此问题()解决问题,但在这段代码中无效

可能与问题相关的代码部分。 (测试授权)

错误

===================================================================================================================================================失败============================================================================================
____________________________________________________________________________________试验______________________________________________________________________________________
def test():
sqlpath=app.config['SQL\u PATH']['getid']
输入_id='xxx'
>rows=db\u acs.dba(sqlpath,s1=input\u id)
测试授权副本:68:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../xxx/xxx/db/dba.py:33:以dba为单位
connection=db.engine.connect()
../../../...pyenv/versions/3.6.5/lib/python3.6/site-packages/flask\u-sqlalchemy/\u_-init\u_;.py:877:在引擎中
返回self.get_引擎()
../../../...pyenv/versions/3.6.5/lib/python3.6/site packages/flask\u sqlalchemy/\u_init\u_.py:886:in get\u引擎
app=self.get\u app(app)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self=,reference_app=无
def get_应用程序(自身、参考_应用程序=无):
“”“Helper方法,该方法实现查找
应用程序。”“”
如果reference_app不是None:
返回参考应用程序
如果当前应用程序:
返回当前应用程序。\u获取\u当前对象()
如果self.app不是None:
返回self.app
引发运行时错误(
>'未找到应用程序。在视图函数内工作或推送'
'应用程序上下文。请参阅'
' http://flask-sqlalchemy.pocoo.org/contexts/.'
)
E运行时错误:找不到应用程序。在视图函数内部工作或推送应用程序上下文。看见http://flask-sqlalchemy.pocoo.org/contexts/.

在文件
conftest.py
中,您可以编写以下内容:

@pytest.fixture(scope="session")
def app():
    app = create_app()  # Or app = Flask(__name__)
    return app
在测试功能中:

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

在文件
conftest.py
中,您可以编写以下内容:

@pytest.fixture(scope="session")
def app():
    app = create_app()  # Or app = Flask(__name__)
    return app
在测试功能中:

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