首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试Connexion/烧瓶应用程序?

如何测试Connexion/烧瓶应用程序?
EN

Stack Overflow用户
提问于 2017-03-21 17:44:52
回答 2查看 9.5K关注 0票数 24

我正在使用连接子框架为烧瓶构建一个微服务。我想使用py.test为我的应用程序编写测试。

pytest-flask文档中,它说要在conftest.py中创建一个可以创建应用程序的夹具,如下所示:

conftest.py

代码语言:javascript
复制
import pytest

from api.main import create_app


@pytest.fixture
def app():
    app = create_app()
    return app

在我的测试中,我使用的client夹具如下:

test_api.py

代码语言:javascript
复制
def test_api_ping(client):
    res = client.get('/status')
    assert res.status == 200

但是,当我运行py.test时,会收到以下错误消息:

代码语言:javascript
复制
==================================== 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

代码语言:javascript
复制
import connexion


def create_app():
    app = connexion.App(__name__, port=8002,)
    app.add_api('swagger.yaml')
    return app


if __name__ == "__main__":
    create_app().run()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-04 13:59:30

使用夹具

test_api.py

代码语言:javascript
复制
import pytest
import connexion

flask_app = connexion.FlaskApp(__name__)
flask_app.add_api('swagger.yml')


@pytest.fixture(scope='module')
def client():
    with flask_app.app.test_client() as c:
        yield c


def test_health(client):
    response = client.get('/health')
    assert response.status_code == 200

swagger.yml

代码语言:javascript
复制
swagger: '2.0'
info:
  title: My API
  version: '1.0'
consumes:
  - application/json
produces:
  - application/json
schemes:
  - https
paths:
  /health:
    get:
      tags: [Health]
      operationId: api.health
      summary: Health Check
      responses:
        '200':
          description: Status message from server describing current health

api.py

代码语言:javascript
复制
def health():
    return {'msg': 'ok'}, 200

使用Swagger测试仪

使用装腔作势测试器的另一种解决方案

test_api.py

代码语言:javascript
复制
from swagger_tester import swagger_test

authorize_error = {
    'get': {
        '/health': [200],
    }
}

def test_swagger():
    swagger_test('swagger.yml', authorize_error=authorize_error)

这个库的酷之处在于,您可以使用规范中提供的示例。但是我不认为它在connexion.RestyResolver中是开箱即用的:您必须在每个端点指定OperationId。

票数 21
EN

Stack Overflow用户

发布于 2020-04-08 07:27:03

代码语言:javascript
复制
import pytest
from json import JSONEncoder
import pytest
from connexion import App

SWAGGER_PATH = "path_to_directory_that_containes_swagger_file"

@pytest.fixture
def app():
    app = App(__name__, specification_dir=SWAGGER_PATH)
    app.app.json_encoder = JSONEncoder
    app.add_api("swagger.yaml")
    app_client = app.app.test_client()
    return app_client


def test_health(app) -> None:
    """
    :except: success
    """
    response = app.get("/health", content_type='application/json')
    assert response.status_code == 200

有关更多信息,请查看

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42934525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档