QA Interview Hub · Knowledge base · Quizzes · Українська

API › Interview questions in brief

How do you build an API automation framework and which layers does it have?

An API test framework consists of layers: tests, a client, models with schemas, data builders, configuration and reporting. Tests describe only the scenario and the assertions, the client knows addresses, headers and authentication, and builders give valid data with one field changed. So a change to an endpoint touches one place, not a hundred tests.

API · Automation

One field changed in one endpoint and 140 tests failed. Where is the wrong boundary?

A team writes API tests like this: every test assembles the address, sets the token, builds the body and compares JSON itself. After one field is renamed most of the suite fails, and the cause has to be searched for in every file. This lesson is about the layers of an API automation framework, what each layer knows and what it must not know.

How it works and why. Splitting by responsibility gives every change a single place. What changes for different reasons (an address, a response schema, data, an environment) lives in different layers:

LayerWhat it knowsWhat it must not know
Teststhe scenario, the expected resultaddresses, headers, tokens
Endpoint clientspaths, methods, serializationbusiness assertions
Transportbase URL, headers, authentication, timeoutsspecific endpoints
Models and schemasthe shape of requests and responseshow to send them
Data buildersa valid object by defaultthe test scenario
Configurationenvironment, secrets, flagstest code
Reportingrequest, response, the reason for a failureassertion logic

Authentication belongs in the transport: the token is obtained once per run or per worker, cached until it expires and refreshed automatically, and a test calls the client. Reporting must show the method, address, body and response, but mask Authorization and secrets.

Example, before and after. Before: the address, headers and body are scattered over the tests. After: the transport and the endpoint client sit in one place, the data comes from a builder and the test stays a scenario.

type Reply<T> = { status: number; body: T };

export class Http {
  constructor(
    private baseUrl: string,
    private token: () => Promise<string>,
  ) {}

  async post<T>(path: string, body: unknown): Promise<Reply<T>> {
    const res = await fetch(this.baseUrl + path, {
      method: 'POST',
      headers: {
        'content-type': 'application/json',
        authorization: 'Bearer ' + (await this.token()),
      },
      body: JSON.stringify(body),
    });
    return { status: res.status, body: (await res.json()) as T };
  }
}
export const buildOrder = (over: Partial<NewOrder> = {}): NewOrder => ({
  sku: 'A-1',
  qty: 1,
  ...over,
});

// the test is only the scenario
const res = await orders.create(buildOrder({ qty: 0 }));
expect(res.status).toBe(422);

The builder gives an object that is valid by default and changes one field, so the test shows exactly which value is checked. If the server adds a required field, the builder is fixed, not a hundred tests. The OrdersClient holds the path /orders: renaming it changes one line.

Schemas and the contract. Typed models catch mistakes while tests are written, while JSON Schema or a client generated from OpenAPI checks the real response. If a specification exists, it is better to generate types and clients from it than to write them by hand: a mismatch between the specification and the code then becomes a build error. The response is checked against the schema in one place of the transport or a helper, not in every test.

Data and isolation. Every test creates the data it needs through the API (or a fixture) and does not depend on run order: otherwise a parallel run gives flaky failures. Unique values (an email, an order number) are generated by the builder. Cleanup is better done through a delete API or a separate test environment than by relying on a "clean database".

Limits and alternatives. There should be no more layers than the suite needs. For a dozen checks a Postman collection with Newman or the CLI in CI is enough; a framework in code pays off when there are dozens of tests, the logic is complex, and data and authentication need code. A deeply branched hierarchy of base classes and a "universal" client for every case make tests harder to maintain. Contract tests (Pact) and schema checks are kept apart from scenario tests because they answer a different question.

Common mistakes.

  • The token and address in every test: changing the environment or authentication scheme breaks the whole suite.
  • Assertions inside the client: a client that throws on 4xx does not allow a negative test to be written.
  • Shared data between tests: one test depends on what another created, and a parallel run breaks.
  • Secrets in the report: Authorization in a request log ends up in CI artifacts.

Check yourself

An endpoint client throws on every non-2xx response. What cannot be written?

Answer

Negative tests: checking that the server returns 422 or 404 for an invalid request turns into catching an exception instead of an ordinary status assertion. The client should return the status and the body, and the test decides what counts as an error.

A new required field on the server broke 60 tests. What does that number show?

Answer

The request body is built in the tests themselves rather than in a builder. In a proper structure the builder is fixed, and tests that do not care about the field notice nothing.

In an interview. Junior: tests should not know addresses and tokens: that lives in a separate client layer, and the data comes from a builder. Middle/Senior: I split the layers by reason to change: the transport with authentication, endpoint clients, models with schemas (preferably generated from the specification), builders, configuration and reporting with secret masking. Tests stay scenarios, data is independent, and the size of the framework matches the size of the suite: for a small suite a collection in a CLI is enough.

More articles in this chapter