title: How to test a Connexion/Flask app?
body: <p>I'm using the <a href="https://github.com/zalando/connexion" rel="noreferrer">Connexion</a> framework for <a href="http://flask.pocoo.org/" rel="noreferrer">Flask</a> to build a microservice. I would like to write tests for my application using <code>py.test</code>.</p>
  <p>In the <code>pytest-flask</code> doc it says to create a fixture in <code>conftest.py</code> that creates the app like so:</p>
  <h3><code>conftest.py</code></h3>
  <pre><code>import pytest
  
  from api.main import create_app
  
  
  @pytest.fixture
  def app():
      app = create_app()
      return app
  </code></pre>
  <p>In my test I'm using the <code>client</code> fixture like this:</p>
  <h3><code>test_api.py</code></h3>
  <pre><code>def test_api_ping(client):
      res = client.get('/status')
      assert res.status == 200
  </code></pre>
  <p>However when I run <code>py.test</code> I get the following error message:</p>
  <pre><code>==================================== ERRORS ====================================
  _______________________ ERROR at setup of test_api_ping ________________________
  
  request = &lt;SubRequest '_monkeypatch_response_class' for &lt;Function 'test_api_ping'&gt;&gt;
  monkeypatch = &lt;_pytest.monkeypatch.MonkeyPatch instance at 0x7f9f76b76518&gt;
  
      @pytest.fixture(autouse=True)
      def _monkeypatch_response_class(request, monkeypatch):
          &quot;&quot;&quot;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'}
      
          &quot;&quot;&quot;
          if 'app' not in request.fixturenames:
              return
      
          app = request.getfuncargvalue('app')
          monkeypatch.setattr(app, 'response_class',
  &gt;                           _make_test_response_class(app.response_class))
  E       AttributeError: 'App' object has no attribute 'response_class'
  </code></pre>
  <p>How can I make <code>py.test</code> work? Here is my <code>create_app</code> function:</p>
  <h3><code>main.py</code></h3>
  <pre><code>import connexion
  
  
  def create_app():
      app = connexion.App(__name__, port=8002,)
      app.add_api('swagger.yaml')
      return app
  
  
  if __name__ == &quot;__main__&quot;:
      create_app().run()
  </code></pre>