In API Testing, we think we’re trying to answer a simple question: Does the API work?
Let’s look at this API call. It creates a user with the details.
![API Testing: A POST Request](https://i0.wp.com/www.everydayunittesting.com/wp-content/uploads/2024/07/image.png?resize=653%2C358&ssl=1)
Let’s say it returned 200 (OK). Yes, we all wanted a 201 (CREATED), but you get what you pay for. Since I didn’t pay, I also don’t get a response, just the status code.
Did it work? Yes it returned a succes status code, but do we trust the answer? Can I get to that user data that it claimed to save? Not with that empty response.
Ok, let’s change the response to include an ID. Like this.
![API Testing: A response with just an ID](https://i0.wp.com/www.everydayunittesting.com/wp-content/uploads/2024/07/short-response.png?resize=451%2C247)
Looks better. Can I say it works now? I’ve got a bit more confidence, but I feel it needs to try harder.
More Reflection, Please
How about, if the response to my API call, the response included the data I sent?
![](https://i0.wp.com/www.everydayunittesting.com/wp-content/uploads/2024/07/full-response.png?resize=639%2C377&ssl=1)
It’s starting to look better. With the last two responses, I feel more confident about the “create user” API. The API response has shown me, that it has indeed listened to me.
But did it work? I don’t trust it yet. Let’s look for more proof.
If I can get to the user’s data back, and if I had a GET API, and I used the ID I got back, and if the right data is returned, I’d be more at ease.
![API Testing: A GetById Response with theh created user data.](https://i0.wp.com/www.everydayunittesting.com/wp-content/uploads/2024/07/get-by-id-response.png?resize=649%2C487&ssl=1)
I also expect it to appear in the “all users” list, so if I had an API to request that list, I expect to see the details there.
![API Testing: A Get All Users Request containing the created data](https://i0.wp.com/www.everydayunittesting.com/wp-content/uploads/2024/07/get-all-response.png?resize=750%2C601&ssl=1)
And to lower my anxiety even more, I want to be able to Delete the user that I created, successfully. If it’s there.
![API Testing: A Delete Request - succesful for existing user.](https://i0.wp.com/www.everydayunittesting.com/wp-content/uploads/2024/07/image-1.png?resize=544%2C199&ssl=1)
And then, after deleting it for the first time, if I try to delete it again, I expect an error. Something like this:
![API Testing: A Delete Request - failure for missing user.](https://i0.wp.com/www.everydayunittesting.com/wp-content/uploads/2024/07/image-2.png?resize=623%2C204&ssl=1)
API Testing Is not about testing APIs
Let’s get back to the question: How do you I if the API works?
Well, this is not the question we actually want to answer. What we’re really after, is feature behavior, not a specific API behavior. And features, in the API world, are really spread around multiple endpoints.
In order to check if features work, (or even work-work), we need the APIs to respond with enough information, that we can collect and boost our confidence that the features work. The more proof, the better.
To learn more about API testing – I have workshops for that. For developers. And testers.
0 Comments