Applying Postman for API Testing: Real-World Examples
API testing is a critical part of modern software development, ensuring that APIs are reliable, performant, and secure. Among the top tools for API testing in 2022, Postman stands out for its user-friendly interface and robust features, making it ideal for both beginners and experienced testers. In this article, we’ll explore how to apply Postman as an API testing framework, with real-world code examples to demonstrate its power. This is inspired by insights from Alice Aldaine’s article on API testing tools.
Why Postman?
Postman is a versatile tool that supports REST API testing without requiring extensive coding knowledge. Its key features include:
- Ease of Use: A rich interface for creating and managing API requests.
- Automation: Supports automated testing with JavaScript-based scripts.
- Integrations: Works with Swagger, RAML, and CI/CD pipelines.
- Collaboration: Allows teams to share collections of requests and responses.
Whether you’re testing a public API or building your own, Postman simplifies the process. Let’s dive into real-world examples to see it in action.
Setting Up Postman
To get started, download Postman from getpostman.com and install it on your system (Mac, Windows, or Linux). The free tier is sufficient for most testing needs, though paid plans ($12/user/month) offer advanced collaboration features.
Once installed, create a new Collection to organize your API requests. For this article, we’ll test the JSONPlaceholder API, a free fake API for testing.
Real-World Example 1: Testing a GET Request
Let’s test a GET request to retrieve a list of users from JSONPlaceholder.
-
Create a Request:
- In Postman, click New > Request.
- Name it “Get Users” and save it to your collection.
- Set the request URL to
https://jsonplaceholder.typicode.com/users
.
-
Send the Request:
- Click Send to execute the GET request.
- You should see a JSON response with a list of user objects.
Add Tests:
Postman allows you to write JavaScript test scripts to validate responses. In the Tests tab of the request, add the following script:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response contains users", function () {
var jsonData = pm.response.json();
pm.expect(jsonData).to.be.an('array').that.is.not.empty;
pm.expect(jsonData[0]).to.have.property('name');
});
This script checks:
- The response status is 200 (OK).
- The response is a non-empty array.
- The first user object has a
name
property.
-
Run the Test:
- Send the request again. The Test Results tab will show the test outcomes (e.g., “2/2 tests passed”).
This example is typical in real-world scenarios where you verify that an API returns expected data structures, such as user lists in a social media app.
Real-World Example 2: Testing a POST Request
Now, let’s test creating a new post, simulating a scenario where a user submits content to a blog platform.
-
Create a POST Request:
- Create a new request named “Create Post”.
- Set the method to POST and the URL to
https://jsonplaceholder.typicode.com/posts
. - In the Body tab, select raw and JSON, then add:
{
"title": "My New Post",
"body": "This is a test post created via Postman.",
"userId": 1
}
- Add Tests: In the Tests tab, add:
pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
pm.test("Response contains post details", function () {
var jsonData = pm.response.json();
pm.expect(jsonData).to.have.property('id');
pm.expect(jsonData.title).to.equal("My New Post");
pm.expect(jsonData.body).to.equal("This is a test post created via Postman.");
pm.expect(jsonData.userId).to.equal(1);
});
This validates that the API creates a new post with the correct data and returns a 201 (Created) status.
-
Run and Verify:
- Send the request and check the Test Results. The API will return the posted data with a new
id
.
- Send the request and check the Test Results. The API will return the posted data with a new
This mirrors real-world use cases like submitting user-generated content to a CMS or e-commerce platform.
Real-World Example 3: Automating Tests with Collections
In a CI/CD pipeline, you might want to automate multiple API tests. Postman’s Collection Runner makes this easy.
-
Create a Collection:
- Add the “Get Users” and “Create Post” requests to a collection named “JSONPlaceholder Tests”.
-
Run the Collection:
- Open the Collection Runner, select your collection, and click Run.
- Postman executes all requests sequentially and displays test results.
-
Export for CI/CD:
- Export the collection as a JSON file.
- Use Newman (Postman’s CLI tool) to run it in a CI pipeline. Install Newman via npm:
npm install -g newman
- Run the collection:
newman run JSONPlaceholder_Tests.json
This is invaluable for teams integrating API tests into Jenkins or GitHub Actions, ensuring APIs are tested with every code change.
Embedding Rich Content
To visualize the power of Postman, check out this quick demo:
This YouTube video shows Postman’s interface and basic request setup, complementing our examples.
Conclusion
Postman is a powerful, accessible framework for API testing, suitable for both manual and automated workflows. Its scripting capabilities, collection management, and CI/CD integration make it a go-to choice for real-world API testing. As noted in Alice Aldaine’s article, Postman’s ease of use and lack of required coding knowledge make it ideal for diverse teams.
Try Postman with your own APIs, and explore its advanced features like mock servers and monitoring to take your testing to the next level. Happy testing!
Top comments (0)