<i id='5XmIp'><tr id='5XmIp'><dt id='5XmIp'><q id='5XmIp'><span id='5XmIp'><b id='5XmIp'><form id='5XmIp'><ins id='5XmIp'></ins><ul id='5XmIp'></ul><sub id='5XmIp'></sub></form><legend id='5XmIp'></legend><bdo id='5XmIp'><pre id='5XmIp'><center id='5XmIp'></center></pre></bdo></b><th id='5XmIp'></th></span></q></dt></tr></i><div id='5XmIp'><tfoot id='5XmIp'></tfoot><dl id='5XmIp'><fieldset id='5XmIp'></fieldset></dl></div>

        <legend id='5XmIp'><style id='5XmIp'><dir id='5XmIp'><q id='5XmIp'></q></dir></style></legend>

      1. <small id='5XmIp'></small><noframes id='5XmIp'>

        <tfoot id='5XmIp'></tfoot>
        • <bdo id='5XmIp'></bdo><ul id='5XmIp'></ul>

        如何测试 Connexion/Flask 应用程序?

        时间:2023-09-28

          • <legend id='k5pJE'><style id='k5pJE'><dir id='k5pJE'><q id='k5pJE'></q></dir></style></legend>
              <tbody id='k5pJE'></tbody>

            <tfoot id='k5pJE'></tfoot>
          • <i id='k5pJE'><tr id='k5pJE'><dt id='k5pJE'><q id='k5pJE'><span id='k5pJE'><b id='k5pJE'><form id='k5pJE'><ins id='k5pJE'></ins><ul id='k5pJE'></ul><sub id='k5pJE'></sub></form><legend id='k5pJE'></legend><bdo id='k5pJE'><pre id='k5pJE'><center id='k5pJE'></center></pre></bdo></b><th id='k5pJE'></th></span></q></dt></tr></i><div id='k5pJE'><tfoot id='k5pJE'></tfoot><dl id='k5pJE'><fieldset id='k5pJE'></fieldset></dl></div>

            <small id='k5pJE'></small><noframes id='k5pJE'>

                  <bdo id='k5pJE'></bdo><ul id='k5pJE'></ul>
                  本文介绍了如何测试 Connexion/Flask 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在为 Connexion 框架.org/" rel="noreferrer">Flask 来构建微服务.我想使用 py.test 为我的应用程序编写测试.

                  I'm using the Connexion framework for Flask to build a microservice. I would like to write tests for my application using py.test.

                  pytest-flask 文档中,它说要在 conftest.py 中创建一个固定装置,这样可以创建应用程序:

                  In the pytest-flask doc it says to create a fixture in conftest.py that creates the app like so:

                  import pytest
                  
                  from api.main import create_app
                  
                  
                  @pytest.fixture
                  def app():
                      app = create_app()
                      return app
                  

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

                  In my test I'm using the client fixture like this:

                  def test_api_ping(client):
                      res = client.get('/status')
                      assert res.status == 200
                  

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

                  However when I run py.test I get the following error message:

                  ==================================== 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 函数:

                  How can I make py.test work? Here is my create_app function:

                  import connexion
                  
                  
                  def create_app():
                      app = connexion.App(__name__, port=8002,)
                      app.add_api('swagger.yaml')
                      return app
                  
                  
                  if __name__ == "__main__":
                      create_app().run()
                  

                  推荐答案

                  使用夹具

                  test_api.py

                  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

                  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

                  def health():
                      return {'msg': 'ok'}, 200
                  

                  使用 Swagger 测试器

                  使用 swagger-tester 的另一种解决方案:

                  test_api.py

                  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.

                  Cool thing about this library is that you can use the examples provided in your spec. But I don't think it works out of the box with connexion.RestyResolver: you'll have to specify the OperationId at each endpoint.

                  这篇关于如何测试 Connexion/Flask 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python struct.pack() 用于列表中的单个元素? 下一篇:从 Django REST Swagger 中排除 URL

                  相关文章

                    <bdo id='AvATm'></bdo><ul id='AvATm'></ul>
                  <legend id='AvATm'><style id='AvATm'><dir id='AvATm'><q id='AvATm'></q></dir></style></legend>
                  1. <small id='AvATm'></small><noframes id='AvATm'>

                    <i id='AvATm'><tr id='AvATm'><dt id='AvATm'><q id='AvATm'><span id='AvATm'><b id='AvATm'><form id='AvATm'><ins id='AvATm'></ins><ul id='AvATm'></ul><sub id='AvATm'></sub></form><legend id='AvATm'></legend><bdo id='AvATm'><pre id='AvATm'><center id='AvATm'></center></pre></bdo></b><th id='AvATm'></th></span></q></dt></tr></i><div id='AvATm'><tfoot id='AvATm'></tfoot><dl id='AvATm'><fieldset id='AvATm'></fieldset></dl></div>
                    <tfoot id='AvATm'></tfoot>