Skip to content

Status and JSON assertions

Chapter lab: API foundationsch11-api-foundations.zip · 17 KB

This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.

Download ch11-api-foundations.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch11-api-foundations.zip && unzip -q ch11-api-foundations.zip && rm ch11-api-foundations.zip

Open the folder in VS Code with code rfe-ch11-api-foundations (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 3.13 or newer is required; uv installs it when it is missing.

Also needed for this lab

Start the RF Demo Shop in its own terminal and keep it running while you work:

Terminal window
uv run python demo_app/serve.py
Terminal window
python demo_app/serve.py
Terminal window
poetry run python demo_app/serve.py

Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.

What's inside
  • Directoryrfe-ch11-api-foundations/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • smoke.robot
    • Directorydemo_app/
      • api.py
      • serve.py
      • store.py
      • web.py
    • Directoryresources/
      • api.resource
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

Two things to assert on any response: its status code and its body. Status Should Be 200 ${resp} checks the code. ${resp.json()} parses the JSON body, and you index into it like a dictionary: ${resp.json()}[slug].

Task 1 — Assert a product’s status and JSON fields

Section titled “Task 1 — Assert a product’s status and JSON fields”
Edit

Goal: create checks/json.robot that GETs the travel-mug product, asserts a 200, and checks its slug and price fields.

Here is the same status-and-field check on a different product. Adapt it to travel-mug and add the price.

Worked example — adapt it, don’t copy it

*** Settings ***
Library RequestsLibrary
Resource ../resources/api.resource
*** Test Cases ***
Backpack Has Expected Fields
${resp}= GET ${API_URL}/api/products/trail-backpack
Status Should Be 200 ${resp}
Should Be Equal ${resp.json()}[slug] trail-backpack

Done when

the response is 200 and its fields match.

Hint 1

GET ${API_URL}/api/products/travel-mug, check Status Should Be 200, and assert Should Be Equal ${resp.json()}[slug] travel-mug.

Hint 2

Assert the price too. It is a number, so use Should Be Equal As Numbers ${resp.json()}[price] 19.90.

Solution
*** Settings ***
Library RequestsLibrary
Resource ../resources/api.resource
*** Test Cases ***
Product Has Expected Fields
${resp}= GET ${API_URL}/api/products/travel-mug
Status Should Be 200 ${resp}
Should Be Equal ${resp.json()}[slug] travel-mug
Should Be Equal As Numbers ${resp.json()}[price] 19.90
Run

Run:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

Done when

the test Product Has Expected Fields passes.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. How do you read a field from a JSON response?

    Show the answer

    Answer: ${resp.json()}[slug] — ${resp.json()} parses the body; index into it with [key], e.g. ${resp.json()}[slug].

  2. Status Should Be 200 ${resp} checks…

    Show the answer

    Answer: the HTTP status code — It asserts the HTTP status code of the response.

Key terms

Status Should Bekeyword
Assert an HTTP response's status code.
${resp.json()}concept
Parse a JSON body and index into it: ${resp.json()}[price].

All terms in the glossary →