API Testing · Automating API Tests

API Test Framework Structure and CI

Lesson 7 of 7 · 9 min read

Ten API tests fit in one class. Two hundred do not. Here is how to organise an API test project so it stays easy to change.

Folder structure

src/test/java/
  clients/
    PostsClient.java        knows the endpoints, returns responses
    AuthClient.java
  models/
    Post.java               request and response objects (POJOs)
  tests/
    PostsTest.java          only behaviour and assertions
  utils/
    Specs.java              shared base URL, headers, logging
    TestData.java           unique, valid data
src/test/resources/
  schemas/post.json
  config.properties

This is the Page Object Model idea applied to APIs: tests never contain URLs, clients never contain assertions.

One shared request specification

public final class Specs {
    public static RequestSpecification base() {
        return new RequestSpecBuilder()
                .setBaseUri(Config.get("apiUrl"))
                .setContentType(ContentType.JSON)
                .addFilter(new RequestLoggingFilter(LogDetail.ALL))
                .build();
    }
}

An API client

public class PostsClient {
    public Response create(Post post) {
        return given().spec(Specs.base()).body(post).post("/posts");
    }

    public Response get(int id) {
        return given().spec(Specs.base()).get("/posts/" + id);
    }

    public Response delete(int id) {
        return given().spec(Specs.base()).delete("/posts/" + id);
    }
}

A clean test

public class PostsTest {
    private final PostsClient posts = new PostsClient();

    @Test(groups = "smoke")
    public void creatingAPostReturnsIt() {
        Post newPost = TestData.post();

        Response res = posts.create(newPost);

        Assert.assertEquals(res.statusCode(), 201);
        Assert.assertEquals(res.jsonPath().getString("title"), newPost.getTitle());
    }
}

Test data rules

  • Each test creates the data it needs, ideally through the API, and deletes it afterwards.
  • Make values unique, for example "QA-Post-" + System.currentTimeMillis(), so parallel runs do not clash.
  • Never depend on data another test created.
  • Keep passwords and keys in environment variables, not in the code.

Run the API tests in CI

# .github/workflows/api-tests.yml
name: API tests
on: [push, pull_request]
jobs:
  api:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17
      - run: mvn -B test -Dgroups=smoke
        env:
          API_USER: ${{ secrets.API_USER }}
          API_PASSWORD: ${{ secrets.API_PASSWORD }}

API tests need no browser, so they are a great first step into CI: fast, stable and cheap to run on every commit.

Where to go next

  • Contract testing (Pact) checks that a provider and its consumers agree on the API.
  • Performance testing with k6 or JMeter checks response times under load.
  • Combine API and UI: create data through the API, then check it in the UI. It makes UI tests faster and more stable.
You finished the API Testing track. Add this project and the Selenium project to GitHub with a short README each. Together they make a strong QA automation portfolio. Then check the QA roadmaps for what to learn next.
All 7 lessons in API Testing
  1. What Is API Testing?
  2. HTTP Basics for Testers: Methods, Status Codes, Headers
  3. API Testing with Postman, Step by Step
  4. What to Test in an API: A Complete Checklist
  5. Testing API Authentication: API Keys, Tokens and OAuth
  6. Automate API Tests with REST Assured (Java)
  7. API Test Framework Structure and CI