Writing API Tests with Postman (With Examples)

πŸš€ Why API Testing Matters

API testing is a must-have skill for modern QA professionals. It helps you test the logic and functionality behind the user interface β€” often before the UI is even built.

Postman is one of the easiest tools to start with. Whether you’re a manual tester or learning automation, Postman helps you send requests, validate responses, and even write test scripts β€” all without needing to write full code.


πŸ”§ What Is Postman?

Postman is a free API client that lets you:

  • Send requests to REST APIs
  • Inspect responses (status, body, headers)
  • Write basic tests using JavaScript
  • Group requests into collections and environments

βœ… Step-by-Step: Writing Your First API Test in Postman

Let’s go through an example using a public API:
πŸ”— https://jsonplaceholder.typicode.com


Step 1: Set Up Postman

  • Download Postman from postman.com
  • Open the app and create a free account

Step 2: Make a Simple GET Request

Let’s get a list of fake posts from JSONPlaceholder:

URL:

arduinoCopyEdithttps://jsonplaceholder.typicode.com/posts

Method:
GET

Click Send β€” you’ll see a JSON response containing posts.


Step 3: Write a Test to Validate the Response

Scroll down to the Tests tab and paste this code:

javascriptCopyEditpm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response has at least one post", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.length).to.be.above(0);
});

πŸ” What this does:

  • Asserts that the status code is 200
  • Checks that the response contains at least one post

Step 4: Test a POST Request

Let’s send a POST request to create a new post.

URL:

arduinoCopyEdithttps://jsonplaceholder.typicode.com/posts

Method:
POST

Body β†’ raw β†’ JSON:

jsonCopyEdit{
  "title": "QA Playbook",
  "body": "Learning API testing with Postman",
  "userId": 1
}

Tests Tab:

javascriptCopyEditpm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});

pm.test("Post contains correct title", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.title).to.eql("QA Playbook");
});

βœ… This validates the response from a simulated post creation.


πŸ§ͺ More Test Snippets for Beginners

βœ… Check Response Time

javascriptCopyEditpm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});

βœ… Check Content Type Header

javascriptCopyEditpm.test("Content-Type is application/json", function () {
    pm.response.to.have.header("Content-Type");
});

βœ… Validate a Property in the Response

javascriptCopyEditpm.test("User ID is 1", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.userId).to.eql(1);
});

πŸ’‘ Bonus Tips

  • Use Environment Variables for dynamic data (e.g., {{base_url}})
  • Save your requests to Collections to reuse them
  • Export your tests and share with your team
  • Integrate with Newman CLI to run Postman tests from the command line

πŸ“ Common Use Cases for Postman in QA

Use CasePostman Feature
Validate API response formatTests with pm.response.json()
Check auth/login APIsUse Authorization tab
Automate test runsUse Newman CLI + scripts
Mock APIs for frontend devsPostman’s Mock Server feature

🎯 Final Thoughts

API testing doesn’t have to be overwhelming. Postman makes it simple to:

  • Explore and understand how APIs work
  • Validate backend logic without waiting for the UI
  • Build confidence in testing more than just interfaces

Leave a Reply

Your email address will not be published. Required fields are marked *