title: How to apply integration tests to a Flask RESTful API
body: <p><em>[As per <a href="https://stackoverflow.com/a/46369945/1021819">https://stackoverflow.com/a/46369945/1021819</a>, the title should refer to integration tests rather than unit tests]</em></p>
  
  <p>Suppose I'd like to test the following Flask API (from <a href="http://flask-restful-cn.readthedocs.io/en/0.3.5/quickstart.html#a-minimal-api" rel="noreferrer">here</a>):</p>
  
  <pre><code>import flask
  import flask_restful
  
  app = flask.Flask(__name__)
  api = flask_restful.Api(app)
  
  class HelloWorld(flask_restful.Resource):
      def get(self):
          return {'hello': 'world'}
  
  api.add_resource(HelloWorld, '/')
  
  if __name__ == "__main__":
      app.run(debug=True)
  </code></pre>
  
  <p>Having saved this as <code>flaskapi.py</code> and run it, in the same directory I run the script <code>test_flaskapi.py</code>:</p>
  
  <pre><code>import unittest
  import flaskapi
  import requests
  
  class TestFlaskApiUsingRequests(unittest.TestCase):
      def test_hello_world(self):
          response = requests.get('http://localhost:5000')
          self.assertEqual(response.json(), {'hello': 'world'})
  
  
  class TestFlaskApi(unittest.TestCase):
      def setUp(self):
          self.app = flaskapi.app.test_client()
  
      def test_hello_world(self):
          response = self.app.get('/')
  
  if __name__ == "__main__":
      unittest.main()
  </code></pre>
  
  <p>Both the tests pass, but for the second test (defined in the <code>TestFlaskApi</code>) class I haven't yet figured out how to assert that the JSON response is as expected (namely, <code>{'hello': 'world'}</code>). This is because it is an instance of <code>flask.wrappers.Response</code> (which is probably essentially a Werkzeug Response object (cf. <a href="http://werkzeug.pocoo.org/docs/0.11/wrappers/" rel="noreferrer">http://werkzeug.pocoo.org/docs/0.11/wrappers/</a>)), and I haven't been able to find an equivalent of the <code>json()</code> method for <code>requests</code> <a href="http://docs.python-requests.org/en/master/api/#requests.Response" rel="noreferrer">Response</a> object.</p>
  
  <p>How can I make assertions on the JSON content of the second <code>response</code>?</p>