Robot Framework – API Testing

Robot Framework supports API testing through the RequestsLibrary, which provides keywords to easily send HTTP requests (GET, POST, PUT, DELETE) and validate responses.

1. Setup and Configuration

Install the RequestsLibrary

pip install robotframework-requests


Import the Library

In your .robot file:

*** Settings ***
Library    RequestsLibrary


2. Creating a Simple API Test Suite

We’ll use https://jsonplaceholder.typicode.com, a free fake REST API, to demonstrate API testing.

Example: api_tests.robot

*** Settings ***
Library    RequestsLibrary
*** Variables ***
${BASE_URL}    https://jsonplaceholder.typicode.com
*** Test Cases ***
Get All Posts
    Create Session    jsonplaceholder    ${BASE_URL}
    ${response}=    Get Request    jsonplaceholder    /posts
    Should Be Equal As Integers    ${response.status_code}    200
    Log To Console    ${response.text}


3. HTTP Methods Examples

GET Request

Get Single Post
    Create Session    jsonplaceholder    ${BASE_URL}
    ${response}=    Get Request    jsonplaceholder    /posts/1
    Should Be Equal As Integers    ${response.status_code}    200
    Log    ${response.json()}

Post Request

Create New Post
    Create Session    jsonplaceholder    ${BASE_URL}
    ${data}=    Create Dictionary    title=Robot Test    body=API Example    userId=1
    ${response}=    Post Request    jsonplaceholder    /posts    data=${data}
    Should Be Equal As Integers    ${response.status_code}    201
    Log    ${response.json()}


PUT Request

Update Post
    Create Session    jsonplaceholder    ${BASE_URL}
    ${data}=    Create Dictionary    id=1    title=Updated Title    body=Updated Body    userId=1
    ${response}=    Put Request    jsonplaceholder    /posts/1    data=${data}
    Should Be Equal As Integers    ${response.status_code}    200
    Log    ${response.json()}


DELETE Request

Delete Post
    Create Session    jsonplaceholder    ${BASE_URL}
    ${response}=    Delete Request    jsonplaceholder    /posts/1
    Should Be Equal As Integers    ${response.status_code}    200
    Log    ${response.status_code}


4. Validating Response and JSON Data

Robot Framework makes JSON validation easy with Evaluate and dictionary-based assertions.

*** Test Cases ***
Validate JSON Response
    Create Session    jsonplaceholder    ${BASE_URL}
    ${response}=    Get Request    jsonplaceholder    /posts/1
    ${json}=    Evaluate    ${response.json()}    json
    Should Be Equal    ${json['id']}    1
    Should Be Equal    ${json['userId']}    1


Other Useful Assertions:

     - Should Contain: Verify text exists in response
     - Should Not Contain: Ensure text is absent
     - Dictionary Should Contain Key: Check if a key exists in JSON

5. Running the Test

Run the test suite: robot api_tests.robot

Output Files Generated:

log.html → Detailed keyword-level execution log

report.html → Summary of all test results
 

Related Tutorials