Home→Reliability→API Testing
πŸ”—API Contract Testing

Contract Testing with Postman + Newman

Repeatable regression and smoke testing for REST APIs. Validate contracts, catch breaking changes, and ensure API stability across deployments.

Newman CLI Execution Flow

1
Load Collection & Environment
newman run text2sql-api-collection.json -e production.env.json
2
Execute Test Suite
βœ“ POST /api/query [200 OK, 645ms]
βœ“ GET /api/schema/:tableId [200 OK, 87ms]
βœ“ POST /api/validate-sql [200 OK, 156ms]
3
Generate Reports
--reporters cli,html,junit --reporter-html-export report.html

What is Contract Testing?

Contract testing validates that APIs honor their interface agreements. Instead of testing implementation details, you verify that the contract (request/response schema, status codes, headers) remains consistent.

I use Postman for collection authoring and Newman for CI/CD execution. This provides:

Schema Validation

Ensure response bodies match expected JSON schemas. Catch breaking changes like missing fields or type mismatches.

pm.expect(jsonData).to.have.property('id');
pm.expect(jsonData.status).to.be.a('string');

Environment-Driven Tests

Run the same collection against dev, staging, and production environments using different variable sets.

{{base_url}}/api/v1/resource
Authorization: Bearer {{api_token}}
πŸ”—

Real Use Case: Text2SQL Query API Validation

How contract testing caught a breaking response format change before production deployment

Challenge: The Text2SQL Query API had multiple consumers (BI dashboards, Slack integrations, internal analytics tools). A response format change could break all downstream integrations.

Solution: Created a comprehensive Postman collection covering all endpoints:

  • Natural language query to SQL translation
  • Schema discovery and table metadata
  • SQL validation and execution
  • Error handling for ambiguous queries

Discovery: During an optimization, a developer changed the generatedSQL field to sql and renamed queryConfidence to confidence. Newman tests failed in CI:

AssertionError: expected undefined to exist (generatedSQL)
at Object.eval (test-script.js:15)

Impact: Caught the breaking change before merging. The team added backward-compatible aliases and versioned the API response format.

How It Helps

πŸ›‘οΈ

Regression Prevention

Detect breaking changes before they reach production. Every deployment is validated against the contract.

πŸ“‹

Living Documentation

Postman collections serve as executable documentation. New developers can see how the API works by running tests.

πŸ”„

CI/CD Integration

Newman runs in pipelines, blocking deployments if contracts are violated. Automated enforcement with zero manual intervention.