Postman has emerged as an essential tool for developers and testers when it comes to working with APIs. Beyond sending requests and receiving responses, Postman also provides valuable features for writing tests and assertions that allow users to validate that their APIs are functioning correctly. In this guide, we’ll explore how to write tests and assertions in Postman using practical examples, making everything easy to follow.
What Are Tests and Assertions in Postman?
Tests in Postman are scripts that run after a response is received. You can write these scripts in JavaScript. Assertions are the conditions used in these scripts to validate the API responses against expected outcomes. If an assertion passes, it means the API is working as intended; if it fails, it indicates a problem that requires attention.
Setting Up a Test in Postman
Before we dive into writing tests, let’s set up a basic request. Suppose we are testing a simple REST API that returns a list of users. Here’s how to set that up:
-
Create a Request: Open Postman and create a new request. Set the request method to
GET
and enter the API endpoint URL (e.g.,https://jsonplaceholder.typicode.com/users
). -
Send the Request: Click the “Send” button and view the response returned by the API.
Writing Your First Test
Now that we have a request set up and a response to work with, let’s write a test. Click on the “Tests” tab located underneath the request URL. Here’s a simple test that checks if the response status is OK (HTTP status code 200):
pm.test("Status code is 200", function () { pm.response.to.have.status(200); });
In this test:
pm.test
is a function that defines a test case. The first argument is a description, and the second is a function that contains the test logic.pm.response.to.have.status(200)
uses Chai Assertion Library syntax to assert that the response status should be equal to 200.
Adding More Assertions
You can add more tests by simply writing additional pm.test
functions. For example, suppose you want to verify that the response body is an array of users:
pm.test("Response is an array", function () { pm.expect(pm.response.json()).to.be.an('array'); });
This test checks that the response parsed as JSON is indeed an array.
Verifying Specific Data
Let’s say you want to also validate that the response contains specific user data. Here's an example that checks the first user’s name:
pm.test("First user's name is Leanne Graham", function () { const jsonData = pm.response.json(); pm.expect(jsonData[0].name).to.eql("Leanne Graham"); });
In this assertion:
- We first parse the response using
pm.response.json()
. - We then check that the
name
of the first user in the array matches “Leanne Graham.”
Running the Tests
Once you have written your tests, you can run them. Click on the “Send” button again, and you will see the results of your tests displayed in the Test Results tab below the response section. You’ll notice a green tick for passing tests and a red cross for any that failed.
Best Practices for Writing Tests in Postman
- Clear Test Descriptions: Provide meaningful descriptions for your tests to ensure that anyone reading them understands what they validate.
- Organize Tests: Keep your test scripts organized and concise. Group similar tests together.
- Use Variables: Use Postman environment variables to simplify your tests when working with multiple environments.
- Error Handling: Always validate that the response is what you expect to handle cases where the API might return unexpected results.
By following these best practices, you can create effective and maintainable tests for your API in Postman, ensuring that your applications run smoothly and as expected.
Armed with this knowledge, you're ready to dive deeper into the world of API testing! Happy testing!