How do you test REST APIs using Robot Framework?

Robot Framework, combined with the RequestsLibrary, is a powerful combination for testing REST APIs. Here's a comprehensive guide on how to do it:

1. Installation:

First, you need to install the RequestsLibrary.

pip install robotframework-requests


2. Basic API Test
  :

***Settings***
Library    RequestsLibrary

***Variables***
${API_URL}    https://api.example.com  # Replace with your API URL

***Test Cases***
Get User Data
    Create Session    my_session    ${API_URL}
    ${response}    Get Request    my_session    /users/1  # Replace with your API endpoint
    Should Be Equal As Strings    ${response.status_code}    200
    ${user_data}    Set Variable    ${response.json()}  # Parse JSON response
    Log    User data: ${user_data}
    Should Be Equal    ${user_data}[name]    John Doe  # Example assertion

Explanation:

  • Create Session: Establishes a session with your API. This is important for managing cookies and headers across multiple requests.
  • Get Request: Sends a GET request to the specified endpoint. You can use other methods like Post Request, Put Request, Delete Request, etc., for other HTTP methods.
  • Should Be Equal As Strings: Verifies the status code of the response. It's good practice to check the status code to ensure the request was successful.
  • ${response.json()}: Parses the JSON response body. The RequestsLibrary automatically handles JSON parsing.
  • Should Be Equal: Asserts that a specific field in the JSON response has the expected value.


3. Handling Different HTTP Methods:

* POST :

${data}    Create Dictionary    name=Jane Doe    age=30
${response}    Post Request    my_session    /users    json=${data}

* PUT :

${data}    Create Dictionary    age=31
${response}    Put Request    my_session    /users/1    json=${data}

* DELETE :

${response}    Delete Request    my_session    /users/1



4. Setting Headers :

${headers}    Create Dictionary    Content-Type=application/json    Authorization=Bearer your_token
${response}    Get Request    my_session    /users    headers=${headers}


5. Query Parameters :

${params}    Create Dictionary    limit=10    offset=0
${response}    Get Request    my_session    /users    params=${params}


6. Handling XML Responses:

If your API returns XML, you can use the XML library to parse it, as discussed in the previous response.


7. Assertions:

Use a variety of assertions to validate the API response:

  • Should Be Equal: For exact value matching.
  • Should Contain: To check if a string contains a substring.
  • Should Match: For pattern matching (regular expressions).
  • Length Should Be: To check the length of lists or strings.
  • Dictionary Should Contain Key: To verify that a dictionary contains a specific key.


8. Data-Driven Testing:

You can use Robot Framework's data-driven features to run the same API test with different sets of data.

***Test Cases***
Get User Data
    [For]    ${user_id}    IN    1    2    3
        ${response}    Get Request    my_session    /users/${user_id}
        Should Be Equal As Strings    ${response.status_code}    200


9. API Key Management:

Store your API keys in variables or a separate file to avoid hardcoding them in your tests. Use environment variables or Robot Framework's variable files for better security.


10. Best Practices:

  • Use descriptive test case names: Clearly describe what each test case is verifying.
  • Organize your tests: Group related API tests into suites.
  • Use variables: Store URLs, headers, and other data in variables to make your tests more maintainable.
  • Implement proper error handling: Check the status codes and handle potential errors gracefully.
  • Document your tests: Add comments to explain the purpose of each test case and the expected behavior.