How to apply integration tests (rather than unit tests) to a Flask RESTful API

后端 未结 4 1616
不知归路
不知归路 2020-12-14 01:36

[As per https://stackoverflow.com/a/46369945/1021819, the title should refer to integration tests rather than unit tests]

Suppose I\'d like to test the foll

相关标签:
4条回答
  • 2020-12-14 02:19

    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]
    
    0 讨论(0)
  • 2020-12-14 02:20

    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.

    0 讨论(0)
  • 2020-12-14 02:26

    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

    0 讨论(0)
  • 2020-12-14 02:33

    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.

    0 讨论(0)
提交回复
热议问题