Week 2: API Quality - Functional,
Schema & Contract Testing
QE Training - 2 hour session
Freshers Program
What is an API?
• API = Application Programming Interface.
• Allows communication between two systems.
• Example: Weather app fetching temperature from OpenWeather API.
• Types of APIs: REST, SOAP, GraphQL.
REST vs SOAP APIs
• REST: Lightweight, uses JSON, stateless, scalable.
• SOAP: XML-based, strict contracts, heavier overhead.
• Modern systems mostly use REST (~80%).
• GraphQL: Flexible querying, newer standard.
Anatomy of an API Request
• Endpoint (URL): e.g., [Link]/v1/temp.
• Method: GET, POST, PUT, DELETE.
• Headers: Authentication, Content-Type.
• Body: Payload for POST/PUT requests.
Common API Tools
• Postman: GUI tool for testing APIs.
• Newman: Run Postman collections via CLI.
• cURL: Quick API testing via command line.
• Swagger/OpenAPI: Documentation and schema definitions.
What is Functional API Testing?
• Ensures API endpoints work as expected.
• Validates response data and status codes.
• Includes positive, negative, and edge cases.
• Critical for ensuring reliable integrations.
Validating Endpoints
• Positive case: GET /users/123 returns user details.
• Negative case: GET /users/999 returns 404 Not Found.
• Edge case: Empty inputs, large payloads.
• Helps ensure robust API behavior.
HTTP Methods & Use Cases
• GET: Retrieve data.
• POST: Create data.
• PUT: Update existing data.
• DELETE: Remove data.
Status Codes
• 200: OK (success).
• 201: Created.
• 400: Bad Request.
• 401: Unauthorized.
• 500: Internal Server Error.
Automating Functional Tests
• Postman allows writing JS-based tests.
• Example:
• [Link]("Status code is 200", function () {
• [Link](200);
• });
• Automation improves reliability and speed.
What is Schema Validation?
• Schema defines expected structure of request/response.
• Validation ensures API always returns correct fields.
• Prevents breaking changes in production.
• Maintains backward compatibility for consumers.
Example JSON Schema
• {
• "type": "object",
• "properties": {
• "id": {"type": "integer"},
• "name": {"type": "string"},
• "email": {"type": "string"}
• },
• "required": ["id", "name", "email"]
• }
Tools for Schema Validation
• Postman schema validation feature.
• AJV (Another JSON Validator).
• Swagger/OpenAPI schema checks.
• Integration with CI/CD pipelines.
Common Schema Issues
• Missing required fields in response.
• Wrong data type returned (string vs number).
• Unexpected new fields breaking consumers.
• Backward compatibility not maintained.
What is Contract Testing?
• Validates agreement between provider and consumer.
• Detects breaking changes early in development.
• Critical for microservices environments.
• Focuses on API expectations, not UI behavior.
Provider vs Consumer
• Provider: Service exposing the API (e.g., Payment API).
• Consumer: Service using the API (e.g., Shopping app).
• Example: Provider changes field 'amount' → 'totalAmount'.
• Consumer fails unless contract ensures consistency.
Pact Framework
• Consumer-driven contract testing tool.
• Consumer defines expected requests/responses.
• Contract shared with provider.
• Provider verifies API matches expectations.
Benefits of Contract Testing
• Detects breaking changes early.
• Reduces reliance on fragile integration tests.
• Improves confidence in microservice deployments.
• Speeds up development cycles.
Example Contract Snippet (Pact)
• {
• "request": {"method": "GET", "path": "/users/123"},
• "response": {
• "status": 200,
• "body": {"id": 123, "name": "Alice"}
• }
• }
Hands-On Part 1: Functional Tests
• 1. Open Postman.
• 2. Send GET request to: [Link]
• 3. Validate status code = 200.
• 4. Check response body contains correct user details.
Hands-On Part 2: Schema
Validation
• 1. Define JSON schema for user object.
• 2. Validate response schema in Postman.
• 3. Introduce error (remove field) and observe failure.
• 4. Discuss why schema validation is important.
Hands-On Part 3: Contract Testing
• 1. Install Pact or review demo repo.
• 2. Write a consumer test expecting GET /users/{id}.
• 3. Generate contract JSON file.
• 4. Run provider verification against contract.
Recap & Q/A
• APIs are backbone of microservices.
• Functional testing ensures correctness.
• Schema validation prevents breaking changes.
• Contract testing aligns provider and consumer.
• Next Week: Modern UI Automation & Accessibility.