API testing is essential for good software, and Postman has made it easier by letting us use JavaScript to check our work. In this article, we'll see how using JavaScript in Postman helps us make sure our software is working right, saving us time and making our testing process smoother.
Goal
Our goal is to enhance the effectiveness of API testing by leveraging Postman capabilities for writing JavaScript tests. Specifically, we aim to optimise the process of asserting results in response bodies, so we don’t have to do it manually ever again. This not only contributes to faster testing cycles, but also ensures that the validation of critical data within API responses is robust and thorough, since we almost completely remove the human factor in the assertion process.
Challenge
Traditional API testing methodologies often fall short when it comes to handling complex responses. Manual inspection and validation of every data point within an API response can be time-consuming, error-prone, and hinder the scalability of our testing efforts. Additionally, as applications evolve, keeping up with changing response structures becomes a challenging task.
Solution
To overcome these challenges, we turn to Postman, a tool that already has a strong position amongst QA Engineers.
Let’s try to come up with a strategic approach to implement JavaScript tests in Postman and optimise the assertions.
First of all, for API tests we need… an actual API we can write our tests against!
Let’s take the Simple Books API as our target for testing purposes. Simple Books API is an API that allows us to check which books are available and then check their detailed information and order them.
The first request we should check is the ”GET /status” endpoint that enables us to check if the API is up and running, so we can proceed with the rest of our tests:
When it comes to responses like the above-mentioned status check, we don’t necessarily need to write tests for them as they are pretty simple and it might be redundant to do so, right? Well, yes and no! Let’s assume there was a feature or a bugfix deployed by the developer that broke the response of the ”GET /status” call and we are now getting a HTTP 200 OK response code, but the body of the response says “status”: “OFFLINE” .
If we have no tests written for this request, we might overlook a potential bug here. We can enhance the test by writing two simple tests in JavaScript.
Let’s head to the Tests tab in the very same request and do it straight away:
We have written two tests, the first one checks for the HTTP 200 OK status code and will fail if it’s any different and the second test checks whether the “status” property contains the “OK” string. This way we can remove the user error while checking the status manually next time, so when the ”GET /status” request is sent again, software tester can be sure that the results from assertions written in the Tests tab will tell him whether everything is okay (tests passed) or something is wrong (tests failed).
Let’s now check another endpoint’s response, which could be very time consuming to check manually:GET /books?type=fiction
.
This endpoint will return all available books that have the fiction type assigned to them:
We only have 4 results here, so again the JavaScript test might seem unreasonable , but let’s assume that another 5000 fiction books are added to the API – it is no longer that easy to check if this request with the ”?type=fiction” query parameter returns only results with the "type": "fiction”
key-value pair.
For that we can write a test that will check whether the “type”: “fiction” key-value pair is present in every returned object:
We’ve also added a counter of total available objects in the response, so we have a better view on what’s going on in the response. Now we can remove the query parameter from the endpoint and check if the test will fail:
Now we will always be getting additional information about the responses, so the software tester sending these requests has to check test results instead of huge response bodies by himself. We have only touched the tip of the API iceberg here. Now imagine how well this works if your API responses have 30 properties and a lot of these properties have to be checked every time the test is run. That’s why it’s a good idea to optimise your work using JavaScript in Postman, so no time is wasted and tests are much more efficient overall.
Here are a few key takeaways from the applied solution:
- JavaScript takes Postman to the next level when it comes to time-saving
Understanding the complexity of JavaScript in Postman is the first step. The ability to write JavaScript tests directly within Postman gives us the power to automate our validation processes, like in test automation.
- Crafting precise assertions will make regression testing as easy as it can be
Postman provides a powerful set of assertions that allow us to inspect every aspect of an API response. Crafting precise assertions ensures that our tests are not only comprehensive but also resilient to changes in the API.
- Test automation integration
When our Postman collection is full of assertions for various tests, we can then integrate our Postman tests into the broader test automation strategy. By incorporating these tests into continuous integration pipelines, we ensure that our assertions are executed automatically, providing swift feedback on the health of the API with each build.
Outcome
Implementing JavaScript tests in Postman gives us a ton of benefits in the long run:
- Time efficiency
Assertion automation significantly reduces the time spent on manual testing. With Postman, we can delegate the execution of test cases, allowing QA teams to allocate time to more strategic testing efforts.
- Precision in validation
Postman’s assertion capabilities empower us to validate responses with precision. This not only improves the accuracy of our tests but also increases confidence in the reliability of the API.
- Comprehensive test coverage
Automating the assertion of responses enhances overall test coverage. In addition, by inspecting critical data points, we contribute to a more thorough evaluation of the API functionality and integrity.
In conclusion, integrating JavaScript tests into our Postman workflows is a smart and strategic move towards achieving faster, more precise, and adaptable API testing. Harnessing the capabilities of Postman does not only optimise our testing processes, but also fortifies our QA practices, ensuring the delivery of high-quality software which we strive for.
Popular posts
From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.
Remember the multimedia craze in the early 2000s? It was everywhere, but did it truly revolutionize our lives? Probably not. Today, it feels like every piece of software is labeled "AI-powered." It's easy to dismiss AI chatbots in construction as just another tech fad.
Read moreFears surrounding external support. How to address concerns about outsourcing software development?
Whether you’ve had bad experiences in the past or no experience at all, there will always be fears underlying your decision to outsource software development.
Read moreWhat do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget
Let’s make it clear: if the capabilities are there, a project is best delivered internally. Sometimes, however, we are missing certain capabilities that are required to deliver said project in a realistic timeline. These may be related to skills (e.g. technical expertise, domain experience), budget (hiring locally is too expensive) or just capacity (not enough manpower). What are good reasons for outsourcing software development?
Read more