【发布时间】:2017-03-21 17:44:52
【问题描述】:
我正在使用Connexion 框架为Flask 构建一个微服务。我想使用 py.test 为我的应用程序编写测试。
在pytest-flask 文档中,它说要在conftest.py 中创建一个固定装置,这样可以创建应用程序:
conftest.py
import pytest
from api.main import create_app
@pytest.fixture
def app():
app = create_app()
return app
在我的测试中,我使用了这样的client 夹具:
test_api.py
def test_api_ping(client):
res = client.get('/status')
assert res.status == 200
但是,当我运行 py.test 时,我收到以下错误消息:
==================================== ERRORS ====================================
_______________________ ERROR at setup of test_api_ping ________________________
request = <SubRequest '_monkeypatch_response_class' for <Function 'test_api_ping'>>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch instance at 0x7f9f76b76518>
@pytest.fixture(autouse=True)
def _monkeypatch_response_class(request, monkeypatch):
"""Set custom response class before test suite and restore the original
after. Custom response has `json` property to easily test JSON responses::
@app.route('/ping')
def ping():
return jsonify(ping='pong')
def test_json(client):
res = client.get(url_for('ping'))
assert res.json == {'ping': 'pong'}
"""
if 'app' not in request.fixturenames:
return
app = request.getfuncargvalue('app')
monkeypatch.setattr(app, 'response_class',
> _make_test_response_class(app.response_class))
E AttributeError: 'App' object has no attribute 'response_class'
我怎样才能使py.test 工作?这是我的create_app 函数:
main.py
import connexion
def create_app():
app = connexion.App(__name__, port=8002,)
app.add_api('swagger.yaml')
return app
if __name__ == "__main__":
create_app().run()
【问题讨论】:
标签: python flask swagger pytest connexion