Skip to content
IRC-CodingIRC-Coding
API TestingUnit TestsIntegration TestsContract TestsTest StrategyConsumer Driven Contracts

API Testing: Unit, Integration & Contract Tests

Master API testing: unit tests, integration tests, contract tests, strategies, tools and best practices for reliable APIs.

S

schutzgeist

6 min read
API Testing: Unit, Integration & Contract Tests

API Testing: Unit, Integration, and Contract Tests

Reliable APIs require a thoughtful testing strategy that combines unit tests, integration tests, and contract tests to catch errors early and protect interface contracts.

Overview

API testing encompasses all activities that verify the correctness, security, performance, and stability of an API. Unit tests examine isolated functions, controllers, or handlers without external dependencies. Integration tests verify how multiple components work together—such as the API, database, and external services. Contract tests ensure that both the API provider and consumers honor the same contract, whether defined through OpenAPI, JSON Schema, or Pact. A solid testing strategy covers the API at multiple layers, runs automatically, and integrates into your CI/CD pipeline. The goal is to catch bugs early, prevent regressions, and reliably document and protect the contract between your API and its clients.

Key Components

Unit Tests for APIs

Unit tests verify small, isolated pieces of your API—handlers, controllers, validation logic, or data mappers. External dependencies like databases or HTTP clients are replaced with mocks or stubs. Unit tests run quickly and provide fast feedback when you make changes.

Integration Tests

Integration tests verify how your API collaborates with real or containerized dependencies. This includes database access, message queues, external APIs, and authentication services. Test containers let you spin up databases and other services in your test environment without manual setup.

End-to-End Tests

End-to-End tests simulate your API from the client’s perspective. They send real HTTP requests and verify the complete response. Tools like Postman, REST Assured, or Supertest are commonly used. E2E tests are slower but cover your entire stack.

Contract Tests

Contract tests verify that the API provider and consumer both uphold their agreed contract. The contract may be defined through OpenAPI, JSON Schema, or frameworks like Pact. Consumer-driven contracts allow consumers to communicate their expectations to the provider.

Consumer-Driven Contracts

In consumer-driven contract testing, clients define their expectations as a contract. The provider must fulfill this contract. Pact is a popular framework for this approach. It prevents API changes from silently breaking client applications.

The Test Pyramid

The test pyramid says you should have many fast unit tests, fewer integration tests, and only a few slow E2E tests. Contract tests fill the gap between unit and integration levels. They help you efficiently verify interface contracts.

Test Data and Fixtures

Test data should be reproducible and isolated. Fixtures, factories, or setup methods generate the data you need before each test. For database tests, use transactions or fresh databases to isolate state between tests.

Mocking and Stubbing

Mocking replaces external dependencies with controlled objects. Stubbing provides predefined responses. Both techniques are important for unit tests, but not for integration tests, where you use real dependencies.

Fuzzing and Negative Tests

Negative tests check how your API handles invalid inputs, missing fields, or unexpected data types. Fuzzing automatically generates many random or semi-random inputs to find weaknesses.

Performance Tests

Performance tests check response times, throughput, and behavior under load. Tools like k6, JMeter, or Gatling are commonly used. They complement functional tests and are critical for APIs with high availability and scalability requirements.

CI/CD Integration

API tests should run automatically in your CI/CD pipeline. Unit tests run on every build, integration tests before merge, contract tests when the API changes, and E2E tests before deployment. Failures stop the build early.

Practical Example

A Node.js team tests an order API at multiple levels.

Unit test for order validation:

describe('Order validation', () => {
  test('rejects negative quantity', () => {
    const result = validateOrder({ customerId: 1, items: [{ productId: 5, quantity: -1 }] });
    expect(result.valid).toBe(false);
    expect(result.errors).toContain('quantity must be positive');
  });
});

Integration test with test containers:

describe('POST /orders', () => {
  test('creates an order and persists it', async () => {
    const response = await request(app)
      .post('/orders')
      .send({ customerId: 1, items: [{ productId: 5, quantity: 2 }] })
      .expect(201);

    expect(response.body.id).toBeDefined();
    expect(response.body.status).toBe('created');

    const order = await db.query('SELECT * FROM orders WHERE id = ?', [response.body.id]);
    expect(order).toHaveLength(1);
  });
});

Contract test with Pact:

const pact = new Pact({
  consumer: 'web-shop',
  provider: 'order-service',
});

await pact.addInteraction({
  state: 'order can be created',
  uponReceiving: 'a request to create an order',
  withRequest: {
    method: 'POST',
    path: '/orders',
    body: { customerId: 1, items: [{ productId: 5, quantity: 2 }] }
  },
  willRespondWith: {
    status: 201,
    body: { id: 1, status: 'created' }
  }
});

By combining all three levels, the team tests the API thoroughly and catches issues early.

FAQ: API Testing

1. What is API Testing?

API testing encompasses all activities that verify the correctness, security, performance, and stability of an API. It includes unit, integration, contract, and end-to-end tests.

2. What is a Unit Test for APIs?

A unit test for APIs verifies isolated functions such as handlers, controllers, or validation logic. External dependencies are replaced with mocks.

3. What is an Integration Test?

An integration test verifies how multiple components work together—such as the API, database, external services, or message queues. Real or containerized dependencies are used.

4. What is a Contract Test?

A contract test ensures that both the API provider and consumer uphold the same contract. The contract may be defined through OpenAPI, JSON Schema, or Pact.

5. What is Consumer-Driven Contract Testing?

In consumer-driven contract testing, clients define their expectations as a contract. The provider must fulfill this contract to avoid silently breaking client applications with changes.

6. What is Pact?

Pact is a framework for consumer-driven contract testing. It generates contracts from consumer tests and verifies them later against the provider.

7. What is the Test Pyramid?

The test pyramid recommends many fast unit tests, fewer integration tests, and only a few slow E2E tests. Contract tests complement the pyramid, filling the gap between unit and integration levels.

8. What is Mocking?

Mocking replaces external dependencies with controlled objects that provide predefined responses. It is primarily used in unit tests.

9. What are Test Containers?

Test containers are containerized dependencies such as databases, message queues, or caches that spin up during tests. They enable realistic integration tests without manual infrastructure setup.

10. What is an End-to-End Test?

An end-to-end test sends real HTTP requests to your API and verifies the complete response. It covers your entire stack but is slower and more maintenance-intensive than unit tests.

11. What are Negative Tests?

Negative tests check how your API handles invalid or incomplete inputs. They ensure that errors are properly handled and reported.

12. What is Fuzzing?

Fuzzing automatically generates many random or semi-random inputs to find weaknesses, crashes, or unexpected behavior in your API.

13. What are Performance Tests for APIs?

Performance tests check response times, throughput, and behavior under load. Tools like k6, JMeter, or Gatling help identify bottlenecks.

14. Why should API Tests Run in CI/CD?

Automated tests in CI/CD catch errors before they reach production. They safeguard refactoring, prevent regressions, and ensure consistent quality.

15. What are Best Practices for API Testing?

Best practices include a balanced test pyramid, isolated and reproducible test data, clear responsibility for each test level, contract tests for interfaces, automated CI/CD execution, and regular performance tests.

Next in the API Learning Path

The next article in the API learning path covers API Rate Limiting Implementation — implementing rate limiting in APIs using token bucket, sliding window, and Redis.

References

  1. https://martinfowler.com/articles/consumerDrivenContracts.html
  2. https://docs.pact.io/
  3. https://www.testcontainers.org/

To deepen your knowledge of API testing, test automation, and software quality, consider these books:

API Development

Books about API design, REST, GraphQL, OpenAPI and API architecture

Designing Data-Intensive Applications von Martin Kleppmann

Designing Data-Intensive Applications von Martin Kleppmann

Bei Amazon ansehen

Affiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.

API Design Patterns von JJ Geewax

API Design Patterns von JJ Geewax

Bei Amazon ansehen

Affiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.

Back to Blog
Share:

Nächster Artikel in API Development

Weiterlesen
API Versioning 2026: REST, GraphQL & gRPC

Related Posts