How to create a unit test to check the response of an API made in Flask?

Using jsonify() fixes the error 'dict' object is not callable

from flask import jsonify
@app.route("/dummy")
def dummy(): 
    return jsonify({"dummy":"dummy-value"})

And for the test, you'll have to pull the JSON out of the HTTP response

import json

class MyAppCase(unittest.TestCase):
    def setUp(self):
        my_app.app.config['TESTING'] = True
        self.app = my_app.app.test_client()

    def test_dummy(self):
        response = self.app.get("/dummy")
        data = json.loads(response.get_data(as_text=True))

        self.assertEqual(data['dummy'], "dummy-value")

This now runs for me.

Tags:

Python

Flask