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

QA Fundamentals › Manual API testing

What is Swagger and how do you use it?

OpenAPI is a format for describing an HTTP API in JSON or YAML, and Swagger is the set of tools around it, such as Swagger UI. QA reads the specification as a contract and derives tests: required fields, limits, enums, error codes. A mismatch with behaviour may be a code bug, a documentation bug or an unsettled requirement, so it is raised as a bug or question.

QA Fundamentals · Manual API testing

In Swagger a field is required, yet the API accepts a request without it. Whose mistake is it?

The specification says currency is required, but the API accepted a transfer without it and debited in a default currency. The tester does not know what to do: fix the documentation, file a bug or ask. Is it an error when the code and the description disagree? A mismatch like this is exactly what QA looks for. This lesson is about what OpenAPI and Swagger are, how to read a specification as a source of tests and where it lets you down.

How it works and why. OpenAPI (formerly the Swagger Specification) is a format for describing an HTTP API in JSON or YAML: paths, methods, parameters, schemas of request and response bodies, response codes, authorisation schemes. Swagger is the name of the set of tools around it: Swagger UI shows the specification as interactive documentation with a "Try it out" button, Swagger Editor helps to write it and Codegen generates code. The current line is OpenAPI 3.x; older projects still have Swagger 2.0. The idea is that the contract between client and server exists in a machine-readable form instead of in a developer's head. From one file the tools take documentation, requests and stubs.

One file, many uses

OpenAPI fileSwagger UIPostmanTest casesMock server
Figure 1. One OpenAPI file feeds the documentation, a request collection, test cases and a mock server.

Example: from "tried it in Swagger" to tests from the specification. A simplified fragment for creating a transfer:

{
  "openapi": "3.0.3",
  "paths": { "/transfers": { "post": {
    "requestBody": { "required": true, "content": {
      "application/json": { "schema": {
        "$ref": "#/components/schemas/NewTransfer" } } } },
    "responses": {
      "201": { "description": "Created" },
      "400": { "description": "Validation error" },
      "401": { "description": "No or invalid token" } }
  } } },
  "components": { "schemas": { "NewTransfer": {
    "type": "object",
    "required": ["amount", "currency"],
    "properties": {
      "amount": { "type": "number", "minimum": 0.01 },
      "currency": { "type": "string", "enum": ["UAH", "EUR"] }
    } } } }
}

Before: opened Swagger UI, pressed "Try it out", sent a request with valid values, got 201 and moved on. After: every line of the specification becomes a test, and each word "required", "minimum", "enum" becomes a boundary condition:

Spec line                          Test
required: amount, currency         no amount → 400; no currency → 400
amount: number, minimum 0.01       0.01 → 201; 0 → 400; "10" (a string) → 400
currency: enum UAH, EUR            "UAH" → 201; "USD" → 400; "uah" → 400?
responses: 201, 400, 401           no token → 401; no other codes in the spec

Actual: amount = 0 → 422 Unprocessable Entity   (the spec says 400)

The last line is a typical find: the spec says 400, the API answers 422. It is not necessarily a bug in the code, there are three options. A mistake in the documentation (the code is right, the spec must be fixed), a mistake in the code (the contract matters for clients that already expect 400) or an unsettled requirement (the team has not agreed yet). QA's role is to show the mismatch in time and ask, not to decide alone. And if clients already depend on the code's behaviour, changing the status may become a breaking change.

Limits and alternatives. A specification does not guarantee behaviour, it only describes it. If it is written by hand, it drifts from the code; if it is generated from the code, it reflects what the code does and cannot find a mistake in the implementation. So it works as a requirement only when it is maintained together with development and CI has a conformance check (contract testing). Version specifics: in OpenAPI 3.0 a nullable flag allows null, while in 3.1 the schemas are aligned with JSON Schema and the type is written as an array, for example ["string", "null"]. A separate Swagger UI trap: the servers list may point to a real environment, and "Try it out" with the Authorize button sends real requests with a real token. Before pressing Execute on a DELETE, check which server it goes to. Sometimes there is a Postman collection or just a Confluence page instead of OpenAPI; then requirements are collected from them and from developers, and a specification appears as a result of testing.

Common mistakes.

  • Trusting the specification without checking: it may lag behind the code, and a test based on a stale description gives false bugs.
  • Checking only the 200 case from "Try it out": the spec describes error codes, and each needs a test.
  • Pressing "Execute" without looking at the server: the request may hit a shared or even a production environment.
  • Settling a mismatch alone: changing the expectation to match the API's behaviour without discussion, because "that is how it works". The defect then becomes the norm.
  • Not adding negative examples: the specification says what is allowed, and what is forbidden needs checking too.

Check yourself

In the specification amount has "minimum": 0.01. Which values do you take for tests and what do you expect?

Answer

Boundaries: 0.01 (accepted, 201), 0.009 and 0 (rejected, 400), a negative number (400). Also values of other types: the string "10", null, a missing field (400). Plus typical ones: an ordinary amount and a large one. Expected codes come from the specification, and where it is silent (say, 0.009 with three decimals) we ask the team, because it is also a rounding question.

The API returns 422 while the specification says 400 for this error. Which three conclusions are possible and what do you do?

Answer

A mistake in the documentation, a mistake in the code or an unsettled requirement. I collect evidence (request, response, a quote from the specification), file a bug or a question in the tracker and involve the developer and the PO, because the team has to decide what is right. Separately I check whether clients already depend on the current behaviour: then changing the code would become a breaking change.

In an interview. Junior: Swagger is API documentation in the OpenAPI format where you can send requests from the browser. I use it as the requirements for the API, for quick checks and to look for mismatches between the documentation and the real behaviour. The specification can be imported into Postman. Middle/Senior: I read the specification as a contract and derive tests from it: required fields, boundaries, enums, response codes. I do not settle a mismatch between the description and the behaviour alone but raise it as a bug or a question, because the mistake may be in the code or in the documentation. I make sure the specification does not drift: generation from code or a conformance check in CI. In Swagger UI I check which server a request goes to.

More articles in this chapter