🧪 API Testing with Postman and Newman
Testing APIs has become an essential part of modern software development.
Before connecting the backend with the user interface, we need to make sure every endpoint behaves as expected — returning the correct data, handling errors properly, and performing under different conditions.
In this guide, we’ll explore how to apply Postman and Newman to automate API testing through a simple and realistic example.
💡 Understanding the Purpose
APIs are like bridges between different systems.
When one part of an app sends a request, the API is responsible for sending the correct response back.
If something fails here, the entire application can break.
API testing ensures that:
- Each endpoint returns the right HTTP status code.
- The JSON data structure is consistent.
- Response times stay within acceptable limits.
- Security tokens and authentication mechanisms work as intended.
⚙️ Tools Overview
🧩 Postman
A graphical tool that lets you design, test, and document APIs easily.
You can manually send requests or create automated test scripts.
🧭 Newman
A command-line companion for Postman.
It allows developers to run Postman collections automatically, which is ideal for continuous integration (CI/CD) pipelines.
🌍 Example Scenario
Let’s work with a public REST API called JSONPlaceholder, commonly used for learning and testing.
Endpoint to test:
GET https://jsonplaceholder.typicode.com/users/1
This API returns user information.
We’ll create tests to verify that:
- The request is successful.
- The response contains valid user data.
- The user ID matches the expected value.
🧪 Writing Tests in Postman
Once you create the request in Postman, go to the Tests tab and paste this script:
// Check if response is OK
pm.test("Status code should be 200", () => {
pm.response.to.have.status(200);
});
// Validate JSON response structure
pm.test("Response contains user details", () => {
const data = pm.response.json();
pm.expect(data).to.have.property("id");
pm.expect(data).to.have.property("name");
pm.expect(data).to.have.property("email");
});
// Check that the correct user is returned
pm.test("User ID matches expected value", () => {
const data = pm.response.json();
pm.expect(data.id).to.eql(1);
});
After sending the request, you’ll see green checkmarks ✅ in the test results panel, confirming that all validations passed.
🧭 Running Tests Automatically with Newman
Once your collection is ready, export it from Postman:
File → Export → Collection → JSON file
Now open your terminal and run:
newman run MyCollection.json
Newman will execute every test automatically and display detailed output directly in the console:
→ GET https://jsonplaceholder.typicode.com/users/1
✓ Status code should be 200
✓ Response contains user details
✓ User ID matches expected value
---------------------------------------------------
Total tests: 3 | Passed: 3 | Failed: 0
This workflow helps teams include API testing in their deployment pipelines — for example with GitHub Actions, Jenkins, or Azure DevOps.
📈 Test Output Example
When all tests pass successfully, you might see something like this:
Collection run complete.
✔ 3 tests passed.
0 tests failed.
Average response time: 410ms
If something goes wrong, Newman highlights the failed test and shows the expected vs actual values.
🧩 Key Takeaways
- Postman makes it easy to design and execute API tests visually.
- Newman adds automation and integration capabilities for professional workflows.
- Using both together allows you to:
- Detect bugs early before integration.
- Maintain consistent API behavior across environments.
- Automate test runs after each deployment.
By including API testing as part of the development process, you build more reliable, stable, and maintainable systems.
📚 References
✍️ Author
Christian Dennis Hinojosa Mucho
Top comments (0)