Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: an API test suite

Build a Robot Framework API suite for a todo service: sessions, CRUD tests, negative cases, a data-driven validation table and a runnable fake API.

In this project you test a small REST API for a todo list. You will design the test plan from the API contract, write session-based Robot tests for create, read, update and delete, add negative and data-driven checks, and clean up after yourself. API tests are the best return on effort in most projects: they are fast, stable and find logic bugs early.

No real service or robot command is available here, so the Robot code is read-only. A Python stand-in service (a fake API function with the same contract) lets you run the same ideas and see them pass and fail.

The contract

Tests start from what the API promises:

Method and path Purpose Success Errors
POST /todos Create 201 + body 422 bad body, 401 no token
GET /todos/{id} Read one 200 404 unknown
GET /todos List 200 list 401
PATCH /todos/{id} Update 200 404, 422
DELETE /todos/{id} Delete 204 404

A todo has id (int), title (string, 1 to 80 characters) and done (boolean).

Test plan

Happy path Create, read, update, delete.
Errors 401, 404, 422.
Boundaries Title length 0, 1, 80, 81.
Idempotency Delete twice gives 404.

Each test creates its own data and removes it.

1 / 2

The Robot suite (read-only)

TEXT
*** Settings ***
Library          RequestsLibrary
Library          Collections
Suite Setup      Create Session    api    ${BASE_URL}    headers=${HEADERS}
Suite Teardown   Delete All Sessions

*** Variables ***
${BASE_URL}    https://todo.example.test
&{HEADERS}     Authorization=Bearer ${TOKEN}

*** Test Cases ***
Create Read Update Delete A Todo
    [Tags]    smoke
    ${todo}=    Create Todo    Write tests
    ${id}=      Set Variable    ${todo}[id]
    ${read}=    GET On Session    api    /todos/${id}
    Should Be Equal    ${read.json()}[title]    Write tests
    ${patch}=   Create Dictionary    done=${True}
    ${upd}=     PATCH On Session    api    /todos/${id}    json=${patch}
    Should Be True    ${upd.json()}[done]
    DELETE On Session    api    /todos/${id}    expected_status=204

Unknown Todo Is Not Found
    GET On Session    api    /todos/999999    expected_status=404

Title Boundaries
    [Template]    Title Should Give Status
    ${EMPTY}          422
    a                 201
    ${{"x" * 80}}     201
    ${{"x" * 81}}     422

*** Keywords ***
Create Todo
    [Arguments]    ${title}
    ${body}=    Create Dictionary    title=${title}
    ${resp}=    POST On Session    api    /todos    json=${body}    expected_status=201
    RETURN    ${resp.json()}

Title Should Give Status
    [Arguments]    ${title}    ${status}
    ${body}=    Create Dictionary    title=${title}
    ${resp}=    POST On Session    api    /todos    json=${body}    expected_status=anything
    Should Be Equal As Integers    ${resp.status_code}    ${status}
    Run Keyword If    ${resp.status_code} == 201
    ...    DELETE On Session    api    /todos/${resp.json()}[id]

Points to notice: the Title Boundaries test is a template with generated 80 and 81 character strings; Title Should Give Status cleans up whatever it created; RETURN (Robot 5 and later) gives a keyword's result. The first test is one long story on purpose: it is a lifecycle check.

A fake service to run it against

This stand-in implements the contract. Run it: first the happy path, then the error cases, then the boundary table. Each check prints PASS or FAIL.

Python Stand-in: fake todo API and its tests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line proves the tests cleaned up (zero items left). A suite that leaves data behind slowly poisons its own environment.

Contract checks with a shape

Beyond status codes, verify the body shape on every success. This stand-in checks the id, title and done types, including a response with a defect.

Python Stand-in: check the response shape

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The "extra key" case matters for security: an API that leaks an internal field is a bug even though the status was 200.

Next steps

  • Add authentication tests: expired token, wrong scope.
  • Run the suite against staging in CI with the token as a secret; see CI.
  • Add pagination (?page=2) and sorting checks.
  • Measure response time with ${resp.elapsed.total_seconds()} and fail above a budget.
  • Link tests to ticket numbers with tags such as todo-42.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the delete test call DELETE a second time?

    Checking repeated deletes verifies the service reports a missing resource correctly.
  2. 2. Which title lengths are the boundary cases for a 1 to 80 rule?

    Values at and just beyond each edge expose off-by-one mistakes.
  3. 3. Why clean up created todos in the test?

    Tests that leave data behind break each other and the environment.
  4. 4. A response has an unexpected secret key. What does this show?

    Checking the whole shape, including extra keys, catches fields that should never be exposed.

Summary

  • Derive the test plan from the API contract: happy path, errors, boundaries, repeats.
  • Use a session, On Session keywords and expected_status to keep tests short.
  • Template tests cover tables of inputs; keywords create and delete data.
  • Check status first, then body shape and values; watch for extra keys.
  • Run it in CI with secrets for tokens.

Next: Project: a data-driven calculator suite