Skip to main content

An API Test in Code

Why This Matters

In lesson 6, Exploring an API by Hand you explored an API by hand. Hand checks are lost the moment you close Postman. An API test in code repeats those checks on every change, in seconds, without a browser. API tests sit in the middle of the pyramid: faster and more stable than UI tests, broader than unit tests.

Your findings become test cases

From your exploration of JSONPlaceholder:

#RequestThen
1GET /todos/1200, id is 1, title is “delectus aut autem”, completed is false
2GET /todos/9999404
3POST /posts with title, body, userId201, the response contains the same title and a new id
4GET /users/1/todos200, 20 todos, all with userId 1

Let AI write it, with Playwright

Playwright is best known for browser tests, but its request fixture is a clean way to test APIs too. In a new folder:

npm init playwright@latest   # accept the defaults, TypeScript

Then prompt:

Write a Playwright API test file tests/todos.api.spec.ts using the request fixture
and baseURL https://jsonplaceholder.typicode.com.
Cover exactly these cases: <paste the table>. One test per case.
Check status codes with expect(response.status()).toBe(...), and check the listed fields.

A good result looks like this:

import { test, expect } from '@playwright/test'

test.use({ baseURL: 'https://jsonplaceholder.typicode.com' })

test('returns a single todo by id', async ({ request }) => {
  const response = await request.get('/todos/1')
  expect(response.status()).toBe(200)
  const todo = await response.json()
  expect(todo).toMatchObject({ id: 1, userId: 1, title: 'delectus aut autem', completed: false })
})

test('returns 404 for a todo that does not exist', async ({ request }) => {
  const response = await request.get('/todos/9999')
  expect(response.status()).toBe(404)
})

test('creates a post and returns it with a new id', async ({ request }) => {
  const response = await request.post('/posts', {
    data: { title: 'foo', body: 'bar', userId: 1 },
  })
  expect(response.status()).toBe(201)
  const post = await response.json()
  expect(post).toMatchObject({ title: 'foo', body: 'bar', userId: 1 })
  expect(post.id).toEqual(expect.any(Number))
})

Run it with npx playwright test tests/todos.api.spec.ts.

Judge it

The same questions as in lesson 8, A Unit Test: Generate, Then Judge, plus two that are typical for API tests:

  • Does it check the status code and the content? A test that only checks 200 passes when the API returns an empty object.
  • Would it fail if the API broke? Prove it: change /todos/1 to /todo/1 (a wrong URL), or expect completed: true. The test must go red. Then undo.
  • Does it depend on data it does not control? JSONPlaceholder’s data is fixed, so exact titles are safe. Against a real system, prefer data your test creates itself.

Exercise

  1. Generate and run the API tests for cases 1 to 3.
  2. Add case 4 yourself, with the AI’s help. Make sure it checks both the count and the userId of every todo.
  3. Break each test once by changing an expected value. Does every test fail with a clear message?

Key Takeaways

  • Turn your manual API findings into automated API tests.
  • Playwright’s request fixture tests APIs without a browser.
  • Check status code and content, and prove the test fails when the API is wrong.
  • Prefer test data you control.