How to apply integration tests to a Flask RESTful API How to apply integration tests to a Flask RESTful API python python

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 appfrom unittest import TestCaseclass 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


I've found that I can get the JSON data by applying json.loads() to the output of the get_data() method:

import unittestimport flaskapiimport requestsimport jsonimport sysclass 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.019sOK[Finished in 0.3s]


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.