I'm trying to test a JSON API I implemented in Flask
Here's my view function
@app.route("/dummy")
def dummy():
return {"dummy":"dummy-value"}
And in my Unittest I'm testing using
def setUp(self):
self.app = my_app.app.test_client()
def test_dummy(self):
response = self.app.get("/dummy")
self.assertEqual(response['dummy'], "dummy-value")
However, when I ran it, I get the error TypeError: 'dict' object is not callable
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With