05 · API & Python extensions
Use RequestsLibrary and typed Python libraries for service checks, negative paths and custom domain keywords.
The API contract
Check status, response shape, types, relevant business invariants and persistence. Do not share mutable HTTP sessions across unrelated tests. Treat the Python library boundary as an implementation detail, not a hiding place for assertions.
RequestsLibrary: API automation
Test HTTP APIs with explicit status, payload and contract-level assertions.
RequestsLibrary wraps Python Requests and provides both sessionless and session-based HTTP keywords.
python -m pip install robotframework-requestsA runnable sessionless example:
*** Settings ***
Library RequestsLibrary
*** Test Cases ***
Public JSON Post Can Be Read
${response}= GET https://jsonplaceholder.typicode.com/posts/1 expected_status=200
Should Be Equal As Integers ${response.status_code} 200
Should Be Equal As Integers ${response.json()}[id] 1
Should Not Be Empty ${response.json()}[title]For a real product API, create a session when several requests share base URL, headers, authentication, proxies or TLS configuration. Keep HTTP mechanics in reusable keywords but keep business assertions visible.
Validate more than status codes. Important API checks include response schema/shape, required fields, types, domain rules, headers, idempotency, error bodies and persistence side effects. Avoid asserting every field just because it is present; assert the contract and behavior the test owns.
Secrets such as bearer tokens should come from environment/CI secret storage and can be represented as Robot secret variables before being passed to a library that safely handles them.
Key points
- Sessionless requests are enough for isolated calls; sessions centralize shared HTTP configuration
- Assert contract and domain behavior, not only HTTP status
- Keep credentials out of
.robotfiles
Common pitfalls
- Creating one global session whose mutable headers/state leak between independent tests
Custom Python libraries, typing and Libdoc
Move complex technical logic into typed Python and expose a small Robot keyword API.
Use Python when logic becomes awkward in Robot syntax: parsing, SDK integrations, cryptography, binary protocols, calculations or reusable technical abstractions.
OrderLibrary.py:
from robot.api.deco import keyword, library
@library(scope="SUITE", auto_keywords=False)
class OrderLibrary:
@keyword("Calculate Expected Total")
def calculate_expected_total(self, first: float, second: float) -> float:
return round(first + second, 2)order_total.robot:
*** Settings ***
Library OrderLibrary.py
*** Test Cases ***
Order Total Uses Python Keyword
${total}= Calculate Expected Total 10.25 4.75
Should Be Equal As Numbers ${total} 15.0Robot uses supported Python type hints for argument conversion. The library scope controls instance lifecycle; use TEST, SUITE or GLOBAL deliberately depending on whether the object carries state.
Generate library documentation with Libdoc:
python -m libdoc OrderLibrary.py OrderLibrary.htmlRobot Framework 7.5 significantly enhances Libdoc, including Markdown documentation support and richer argument/return/exception documentation. Treat your library as a real API: type it, document it, test it directly in Python where appropriate, and keep the Robot-facing surface small.
Key points
- Python libraries are for technical implementation, not for hiding test intent
- Library scope is state lifetime
- Use type hints and Libdoc to make keyword contracts explicit
Common pitfalls
- Using
GLOBALlibrary scope for stateful clients without designing thread/process isolation
API negative paths and contract-oriented checks
Go beyond status-code checks without overasserting incidental fields.
An API test needs an explicit oracle: status class, required keys and types, domain invariants and side effects. A success status alone does not prove the business operation worked. Isolate created data and verify expected failures without swallowing transport errors.
Example with RequestsLibrary
*** Settings ***
Library RequestsLibrary
Library Collections
*** Test Cases ***
Existing Post Has Expected Shape
${response}= GET https://jsonplaceholder.typicode.com/posts/1 expected_status=200
Dictionary Should Contain Key ${response.json()} userId
${body}= Set Variable ${response.json()}
Should Be Equal As Integers ${body}[id] 1This example needs Internet access and robotframework-requests. JSONPlaceholder is a public demonstration API, not a service under your control; production suites should use a controlled test environment and contract fixtures. A real negative-path test should assert the documented 4xx response body and unchanged persistent state, not merely accept every exception.
For strict schemas, call a typed Python validation library from a keyword. Reserve Pact for consumer-provider compatibility when a provider must verify consumer expectations.
Key points
- Assert business invariants and types in addition to status
- Separate a service's contract from incidental response details
Common pitfalls
- Catching all request failures and treating network timeouts as a valid 4xx negative test