Status and JSON assertions
Chapter lab: API foundations
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:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch11-api-foundations.zip && unzip -q ch11-api-foundations.zip && rm ch11-api-foundations.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch11-api-foundations.zip -OutFile ch11-api-foundations.zip; Expand-Archive ch11-api-foundations.zip -DestinationPath .; Remove-Item ch11-api-foundations.zipOpen 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:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 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:
uv run python demo_app/serve.pypython demo_app/serve.pypoetry run python demo_app/serve.pyOpen 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”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 RequestsLibraryResource ../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-backpackDone 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 RequestsLibraryResource ../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.90Task 2 — Run it green
Section titled “Task 2 — Run it green”Run:
uv run robot checksrobot checkspoetry run robot checksDone 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.
How do you read a field from a JSON response?
${resp.json()}parses the body; index into it with[key], e.g.${resp.json()}[slug].Show the answer
Answer:
${resp.json()}[slug]—${resp.json()}parses the body; index into it with[key], e.g.${resp.json()}[slug].Status Should Be 200 ${resp}checks…It asserts the HTTP status code of the response.
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].