Skip to content

How To Write Test Cases Testing Api Using Postman Manually

API Testing Blog

Writing Test Cases for API Testing with Postman: A Manual Approach

Postman, a popular tool for API testing, offers a user-friendly interface for manually crafting and executing test cases. Let’s delve into the process, starting with an overview of the key components involved:

Understanding API Testing with Postman

API testing involves verifying the functionality, performance, and security of your application’s APIs. Postman streamlines this process by allowing you to:

  • Send requests: Craft HTTP requests with various methods (GET, POST, PUT, DELETE, etc.).
  • Inspect responses: Examine the server’s response, including status codes, headers, and body content.
  • Define test scripts: Write code to assert specific conditions within the response, ensuring expected behavior.

Step-by-Step Guide to Writing Manual API Test Cases in Postman

1. Setting Up Your Workspace

  • Create a Collection: Organize your test cases into a collection for easy management. A collection acts as a folder for your requests and tests.
  • Add Requests: Add individual API requests to your collection, defining the following:
    • Method: The HTTP method (GET, POST, PUT, DELETE, etc.).
    • URL: The endpoint of the API you’re targeting.
    • Headers: Any necessary headers, like authentication tokens.
    • Body: The data being sent to the API, if applicable (e.g., JSON, XML).

2. Writing Test Scripts with Postman’s Testing Features

  • Test Tab: Within each request, use the “Tests” tab to define your assertions.
  • Code Snippets: Postman provides helpful code snippets to speed up test creation:
    • Status Code Validation: pm.test("Status code is 200", function () { pm.response.to.have.status(200); });
    • Body Content Validation: pm.test("Body matches expected value", function () { pm.expect(pm.response.text()).to.be.equal('Hello, world!'); });
    • Response Time: pm.test("Response time is less than 2 seconds", function () { pm.expect(pm.response.responseTime).to.be.below(2000); });

3. Practical Example: Validating a User Registration API

Request:

  • Method: POST
  • URL: /users
  • Headers: Content-Type: application/json
  • Body:
{
"username": "testuser",
"email": "test@example.com",
"password": "testpassword"
}

Test Scripts:

pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
pm.test("Response body contains user ID", function () {
pm.expect(pm.response.json().id).to.be.a('number');
});
pm.test("Response body contains username", function () {
pm.expect(pm.response.json().username).to.be.equal('testuser');
});

4. Running and Analyzing Tests

  • Run Collection: Execute all requests in your collection using the “Send” button or the collection runner.
  • Test Results: Postman provides a comprehensive report of your test results, highlighting successful and failing assertions.

5. Advanced Testing Techniques

  • Data-Driven Testing: Use variables and collections to parameterize requests and test cases, ensuring you cover a range of inputs.
  • Environment Variables: Securely store sensitive information like API keys and base URLs.
  • Pre-request Scripts: Execute code before sending each request to set up variables or customize headers.

6. Best Practices for Writing Effective Test Cases

  • Focus on User Stories: Link your test cases to specific user stories or functionalities.
  • Positive and Negative Scenarios: Test both successful and error conditions.
  • Modularize Tests: Break down complex tests into smaller, reusable components.
  • Maintain Consistency: Use clear naming conventions and follow coding best practices.

In Conclusion

By integrating test scripts into your Postman workflows, you can effectively test your APIs manually, ensuring quality and reliability throughout your development cycle. Remember to focus on clear expectations, comprehensive coverage, and ongoing maintenance to reap maximum benefits from your API testing efforts.

API Testing Blog