API Test Cases
What to Cover
- Positive cases: valid input returns the expected status code and correct data.
- Negative cases: invalid input (missing fields, wrong data types) returns appropriate error codes and messages.
- Boundary cases: testing limits, such as maximum field length or pagination edges.
- Authentication/authorization: requests without valid credentials are correctly rejected.
- Response structure: confirming the response schema, data types, and required fields are correct.
Example Test Case
For a GET /users/{id} endpoint: input — a valid existing user ID; expected result — status code 200 and a response body containing the correct user's name and email.
A Template That Keeps API Test Cases Consistent
Writing every API test case with the same fields makes reviews faster and makes automation easier later. A practical template has these columns:
| Field | What to write |
|---|---|
| ID and title | A short unique ID plus a one-line purpose, e.g. "POST /users rejects missing email" |
| Preconditions | Auth token, existing records, environment |
| Request | Method, endpoint, headers, query params and body |
| Expected status | The exact code, such as 201, 400 or 401 |
| Expected body | Key fields, error message and schema rules |
| Post-checks | Database row created, event published, nothing changed on failure |
Worked Example: Test Cases for Creating a User
Take POST /users, which accepts name, email and age. One endpoint quickly produces a full set of cases:
| ID | Scenario | Expected |
|---|---|---|
| TC01 | All fields valid | 201, body returns a new id and the same name and email |
| TC02 | Email missing | 400 with a message naming the email field |
| TC03 | Email already registered | 409 or the documented conflict code; no duplicate row |
| TC04 | Age sent as text "twenty" | 400 with a type error |
| TC05 | Name at maximum allowed length | 201; one character more returns 400 |
| TC06 | No Authorization header | 401, and no user is created |
| TC07 | Valid token without create permission | 403 |
| TC08 | Wrong Content-Type (text/plain) | 415 or 400, as the API contract says |
Notice that failure cases also check the side effect: a rejected request must not leave a half-created record behind.
Turning a Case Into an Automated Check
Once written, the same case can be automated with Rest Assured in Java. This version covers TC02:
@Test
public void createUserWithoutEmailReturns400() {
given()
.baseUri("https://api.example.com")
.header("Authorization", "Bearer " + token)
.contentType(ContentType.JSON)
.body("{ \"name\": \"Asha\", \"age\": 24 }")
.when()
.post("/users")
.then()
.statusCode(400)
.body("error", containsString("email"));
}
Keep the test name equal to the case title so a failed run points straight back to the written case.
Common Gaps in API Test Suites
- Checking only the status code and ignoring the response body or headers.
- Skipping idempotency checks: calling PUT or DELETE twice should behave as documented.
- Never testing empty lists, pagination beyond the last page or very large page sizes.
- Hardcoding IDs that only exist in one environment, so tests break elsewhere.
- Leaving out response time expectations for endpoints users wait on.
- Not verifying that error messages avoid leaking stack traces or internal details.
Before calling an endpoint covered, confirm you have at least one positive case, one per required field missing, one per data type rule, one for auth failure, one for permission failure and one boundary pair for each length or range limit.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.