Steps to Set Up a Postman Test
Now that we’ve explored the different approaches to comparing JSON responses, let’s walk through the steps of setting up a Postman test.
Step 1. Open Postman and create a new request or select an existing one.
- Choose the request type (e.g., GET, POST, etc.) from the dropdown menu.
- Enter the request URL in the address bar and any necessary headers.
Step 2. Enter Request Details in the ‘Body’ Tab:
- If the request requires a body (e.g., for POST or PUT requests), navigate to the ‘Body’ tab within the request editor.
- Enter the necessary details such as JSON, form data, or raw text according to your API’s requirements.
Step 3. Go to the ‘Tests’ tab and write your test script using one of the methods described above.
Step 4. Click ‘Send’ to execute the request and run the test.
- Once you’ve written your test script, click the blue ‘Send’ button located at the top-right corner to execute the request and run the tests.
Step 5. The test results will appear in the ‘Test Results’ section.
- After the request has been sent and the tests have run, the test results will appear in the ‘Test Results’ section at the bottom of the Postman window.
- Review the test results to see if each test passed or failed.
Example: To illustrate, let’s create a Postman test using the pm.expect method. Suppose we have an API that returns user details, and we want to validate that the username and email are as expected.
Our test script in Postman would look something like this:
pm.test("Check user details", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.username).to.eql("michael");
pm.expect(jsonData.email).to.eql("michael@example.com");
});
After running the request, the test results will confirm whether the username and email in the response match our expectations.
How to Write Postman Test to Compare the Response JSON against another JSON?
In the era of API-driven development, testing the API responses to ensure their correctness is of paramount importance. One such test includes comparing the JSON response of an API against a predefined JSON. In this article, we will delve into how to write a Postman test for this purpose.
Table of Content
- What is Postman?
- JSON and its Role in APIs
- Why Compare JSON Responses?
- Approaches to Comparing JSON Responses in Postman
- Steps to Set Up a Postman Test
- Conclusion
Contact Us