Pytest on Flask: Performing Tests
Learn how to perform tests on a Flask application.
We'll cover the following
Just as before, the testing is done with assertions. The first step in testing the API is to make a request. The response from the request can be converted to a Python object using json.loads
. Since the API operates on bytes and Python uses Unicode, the response data must first be decoded. Note that these tests are not checking the content of the data, only that they’re a list and that each list item is a dictionary with the three correct keys.
Testing the GET
method
Similarly, we can use the test_app.get
to check a route like /my-api/<int:id>
that includes a parameter.
Get hands-on with 1200+ tech skills courses.