Skip to content
All notes
26API TestingAPI TestingTools

How do you use Postman for API Testing?

Postman is a popular API testing tool that allows you to create, test, and document APIs through an intuitive interface.

Key Features

  1. Request Building: Select HTTP method, enter URL, add headers/body
  2. Collections: Group related API requests together
  3. Environment Variables: Store values like base URL, tokens
  4. Tests/Assertions: Write JavaScript tests to validate responses
  5. Pre-request Scripts: Execute code before sending request
  6. Collection Runner: Run multiple requests in sequence
  7. Mock Servers: Create mock API responses
  8. Documentation: Auto-generate API documentation

Common Test Examples

// Check status code
pm.test("Status code is 200", () => {
    pm.response.to.have.status(200);
});

// Validate response time
pm.test("Response time < 500ms", () => {
    pm.expect(pm.response.responseTime).to.be.below(500);
});

// Check JSON response
pm.test("Response has user data", () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('name');
    pm.expect(jsonData.name).to.eql('John Doe');
});

// Set environment variable
pm.environment.set("userId", pm.response.json().id);

Best Practices

  • Use environment variables for different environments (dev, staging, prod)
  • Organize requests into collections
  • Write comprehensive tests for each request
  • Use pre-request scripts for dynamic data
  • Integrate with CI/CD using Newman (Postman CLI)