How to apply integration tests to a Flask RESTful API
Flask provides a test_client you can use in your tests:
from source.api import app
from unittest import TestCase
class TestIntegrations(TestCase):
def setUp(self):
self.app = app.test_client()
def test_thing(self):
response = self.app.get('/')
assert <make your assertion here>
Flask Testing Docs
With Python3, I got the error TypeError: the JSON object must be str, not bytes
. It is required to decode:
# in TestFlaskApi.test_hello_world
self.assertEqual(json.loads(response.get_data().decode()), {'hello': 'world'})
This question gives an explanation.
What you're doing there is not unit testing. In every case, when using the requests library or the flask client, you're doing integration testing as you make actual http calls to the endpoints and test the interaction.
Either the title of the question or the approach is not accurate.
I've found that I can get the JSON data by applying json.loads()
to the output of the get_data()
method:
import unittest
import flaskapi
import requests
import json
import sys
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('/')
self.assertEqual(
json.loads(response.get_data().decode(sys.getdefaultencoding())),
{'hello': 'world'}
)
if __name__ == "__main__":
unittest.main()
Both tests pass as desired:
..
----------------------------------------------------------------------
Ran 2 tests in 0.019s
OK
[Finished in 0.3s]