QA Automation 101 for Backend Devs
Build API Test Automation for a Movies API using Postman
Ready for a spicy take? ๐ถ๏ธ
"You don't need to 'graduate to Dev' to make QA a fulfilling career choice. In fact, many devs would enjoy a QA career."
QA is awesome. Testing stuff, breaking stuff, writing code that tests old stuff so you can test new stuff. And let's not forget the exciting moment when you find a hard-to-reproduce bug and figure out the precise root cause!
Ah yes, this is a dream career for the critical thinkers and the detail-obsessed.
Is that you?
Well, if you're a Backend Developer right now, I've got good news:
โจ You Have Transferable Skills! โจ
Coding
Writing automated [unit & integration] tests
Calling APIs
Debugging server-side issues
QA Automation for APIs is going to look very familiar to you. It's just a little bit different, but that's because QA Automation lives on the end-to-end tier of the test pyramid!
Let's take a look at an example of how you can run an end-to-end test using Postman. This may be review for you, but we're getting to the good stuff soon, so hang tight.
Part 1: The Manual Test We'll Automate
Step 1: Create A GET Request in Postman
Go download Postman's desktop app from the internet
Open it up, log in & create a new workspace
It'll ask you if you want to use an existing template, so choose the "API testing" one if it's there.
No worries if you don't see the "API Testing" option. Just copy the way we make the request in a moment.
At this point, your screen may look something like this, if you went into your workspace after choosing the API Testing template...and expanded the "Regression Testing" folder.
Let's edit that "Response body verification" test case.
We're going to test an endpoint on the Movie Database Alternative API on RapidAPI.
How about...GET By Search
.
To start testing, we'll need to click Subscribe to Test and choose the FREE plan.
Yes, they'll still ask for your card info in case you go over their limit, which is quite difficult to do for what we're doing here. Just don't send 1001 requests in a day and you'll avoid getting charged a penny.
Make sure you click "Pay Now" even though it's $0.00 because you won't be subscribed if you don't.
And then this will happen later:
Once you're subscribed to the free, plan, go back to the Endpoints tab.
Let's take a look at that GET By Search endpoint's Code Snippet tab on the right-hand column...
...except for my API key, of course ๐
By the way, this is the default Node.js snippet for this endpoint as of August 26, 2023. If it changed, use what's there, not this (they call me Captain Obvious).
Let's transform this into a Postman request we can use...let's go back to the Regression Testing request template we have in our workspace:
Nothing exciting to see here. Do the following with me:
Update the method to GET
Update the URL to the one from the snippet
Add a
s
key to the Params table (like in the snippet)Add a
r
key to the Params table (like in the snippet)Add a
pages
key to the Params table (like in the snippet)Use the default values it gives you for each of the params (in the snippet)
Now it should look like this:
Before you click Send on the right there, go do the following:
In the "Body" tab, set the request to an empty object
{}
(there is no payload for this request)In the "Headers" tab, set your
X-RapidAPI-Key
to the one you see in your code snippetIn the "Headers" tab, set your
X-RapidAPI-Host
tomovie-database-alternative.p.rapidapi.com
(or the one you see in your code snippet if it's different)
If you don't do those steps, you'll end up with a message like this one:
Here's a final snapshot of that Headers section:
And the Body section:
Step 2: Send It ๐
When you send it NOW...the Response Body in the pane below should look like this:
Step 3: Manually Verify the Response Body
This is the full response body we get back:
{
"Search": [
{
"Title": "Avengers: Endgame",
"Year": "2019",
"imdbID": "tt4154796",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BMTc5MDE2ODcwNV5BMl5BanBnXkFtZTgwMzI2NzQ2NzM@._V1_SX300.jpg"
},
{
"Title": "Marvel Studios' Avengers: Endgame LIVE Red Carpet World Premiere",
"Year": "2019",
"imdbID": "tt10240638",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BNThjZDgwZTYtMjdmYy00ZmUyLTk4NTUtMzdjZmExODQ3ZmY4XkEyXkFqcGdeQXVyMjkzMDgyNTg@._V1_SX300.jpg"
},
{
"Title": "Avengers: Endgame and the Latest Captain Marvel Outrage!!",
"Year": "2019",
"imdbID": "tt10025738",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BZjg2ZTM3OTgtY2ExMS00OGM4LTg3NDEtNjQ0MjJiZDFmMGFkXkEyXkFqcGdeQXVyMDY3OTcyOQ@@._V1_SX300.jpg"
},
{
"Title": "Avengers Endgame: the Butt Plan",
"Year": "2019",
"imdbID": "tt10399328",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BNTQ1OWQzODktMTY3Zi00OTQxLWExOTYtZTNjZjY5ZTY4M2UyXkEyXkFqcGdeQXVyMTAzMzk0NjAy._V1_SX300.jpg"
},
{
"Title": "Avengers: Endgame (2019)",
"Year": "2019",
"imdbID": "tt16416424",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BY2I4YzU4YTYtZDc3OC00NjcwLTlhM2ItNjlmYjNiMjFkMTRiXkEyXkFqcGdeQXVyMTE4NTEwNTky._V1_SX300.jpg"
},
{
"Title": "Avengers: Endgame (2019) - Spoiler Full Review",
"Year": "2019",
"imdbID": "tt17978032",
"Type": "movie",
"Poster": "N/A"
}
],
"totalResults": "6",
"Response": "True"
}
And this is the response schema RapidAPI says we should expect to see in their "Example Responses" tab on the right-hand column:
With RapidAPI, you can actually send this request on their website using the "Test Endpoint" button.
If you do that, you can check the "Results" tab afterward and compare against what you got in Postman.
Yep, we're good.
There you have it. MANUAL TESTING. Oh joy! I'm sure you totally didn't skip past this part hoping to see how we'd automate this.
So let's see how we'll use Postman to automate this test.
Part 2: The Automated Test
If your Postman "API testing" template was as cool as mine was, you'll have these tests pre-written for you for the response body you already deleted.
Why didn't I ask you to delete these too? Because they're a good example, idk.
But also because we're going to use our friend, AI, to make quick work of this for us.
Step 1: Let AI Generate Your Tests
I'm going to use Bard because I like Bard. Use whichever AI chatbot you're comfortable with.
The Prompt:
Based on a given example of Postman tests, generate unit tests for a given response body.
Postman tests example:
<copy/paste the Postman tests you have in your template>
Response body:
<copy/paste your response body>
If you don't have the Postman tests already in your workspace template, you can probably just tell it to generate Postman tests based on your response body and leave it at that. It's pretty smart. I just used the example tests as a way to improve the test syntax it would generate.
When you run the prompt, it'll spit out some tests you can copy/paste into your Postman request's "Tests" tab.
Step 2: Fix the AI-Generated Tests
There were a few problems with it, however, because AI is imperfect.
Let's correct them:
const getData = () => pm.response.json()
.data;(the 'data' property doesn't exist)We need to be checking for a string data type for these fields, not numbers
There we go! All green.
Step 3: Add the Human Element
Okay, do we really want to check if the response is a JSON object?
Probably not. If it's not, we've got bigger problems.
This is where AI can be a little silly.
Now that you've got an example of the test structure in Postman, you can decide which tests are valuable and which are not.
For example, maybe we want to make sure we have only 1 search result that's an exact match for the s
parameter we sent in the request.
AI can help you figure out how to do that, too, but here's a Stack Overflow post explaining it for old time's sake:
I won't go generating a list of tests for you. For that, you have:
Your imagination
AI/Docs/StackOverflow
Conclusion
To sum up what we've covered here:
You can generate a Postman request & hit a public API for FREE QA Automation practice
You can add test automation to your Postman request that runs after you get a response back
You can use AI to generate those tests if you want to (if you're just lazy like me, or if you simply don't care to learn Postman's JavaScript test syntax)
You can use the Console & Test Results tabs in Postman to debug & fix your tests
This should give you enough to get started writing API automation in Postman.
This isn't the only way to automate API testing, of course.
For what it's worth, Postman has Newman to help you run these kinds of automated tests from the CLI or in the cloud via CI/CD pipelines if you want to:
But there are many ways to automate API testing that extend beyond Postman.
Here are some popular API Automation frameworks for you to check out:
Happy Testing! ๐งช