paint-brush
This Framework Lets You Test Your Rest-API With The Get Methodby@mka342
117 reads

This Framework Lets You Test Your Rest-API With The Get Method

by Mohsen KhosroanjamDecember 6th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this article, I will show you how you can test a restful “Get” method using the Robot Framework.
featured image - This Framework Lets You Test Your Rest-API With The Get Method
Mohsen Khosroanjam HackerNoon profile picture

In this article, I will show you how you can test a restful “Get” method using the Robot Framework. If you are new to Robot Framework, please read the doc from the website to find out the setup guide. You can install Robot Framework using “pip install robotframework” in your command line. Please note that you must install pip on your system before running this command.


To write test cases in Robot Framework, you should create a file with .robot extension, then you can write your scenario in the file to be run.


Each .robot file usually contains three main parts: Settings, Variables, and Test cases like the below image.



In settings, you can import the libraries, suit setup, and documentation. In the variables part, you can put your variables, and the scenarios are written in the test cases section.


To test a simple Get request, we should import “RequestsLibrary” in settings because we want to make a request. In test cases, first, we should define a keyword for our test scenario, and then we should write the test steps in the keyword’s scope. See the picture below:



Let’s automate a simple Get request together.


Suppose we want to write a test case for the following curl. This test case will call the endpoint and assert whether:

1- The endpoint responds with 200.

2- The response body contains “Google“ word.

3- The value of Content-Type is “application/json” in response headers.

curl --location 'https://api.restful-api.dev/objects' --header 'Content-Type: application/json' --data ''


This is a simple request that returns some mobile names like the below picture:


In this step, I am going to write a Robot Framework code to call the API and assert the response.


First, I define a variable to hold the base address of the API. Second, I put the URI of the API in another variable. You may ask why I put the URI in another variable. You will get your answer in the creating get request step.



As I mentioned earlier, it it important to choose a name for our keyword. I choose get_request for the keyword part and then write the codes in the keyword’s scope. To create a restful request, we should establish a session first. Robot Framework has a built-in function to establish a session, it is “Create Session”:

Create Session    session_name    ${base_address}


Then we can make a get request by using “Get Request“ built-in function:

Get Request    session_name      ${uri}


Get Request is a built-in function in Robot Framework that gets at least two arguments. This is why I put the URI in a separate variable. The code above calls the API and gets the response, so we must hold the response in a variable like:

${resp}=    Get Request    session_name     ${uri}


Going through the steps that I have mentioned so far, will make get request, call the API, and save the response in the “resp” variable. Now we have the response and are ready to do assertions. One of the assertions in Robot Framework is “Should Be Equal“ built-in function. It gets two arguments and compares them. If they are the same, the assertion will return true.

Should Be Equal    ${resp.status_code}    200


Note that integers in Robot Framework are considered as strings and ${resp.status_code} is returned as an integer. For example, in the code line above, 200 is an integer but Robot Framework considers it as a string so you should convert ${resp.status_code} to a string to compare it with 200. The built-in function for converting an integer to a string is “Convert To String“.


Through the response of the API, we can get the response headers and response body by ${resp.headers} and ${resp.content} respectively. Once you have the body of the response and the headers of the response, you can assert them as well. For instance, suppose you want to check whether the response body contains a specific word “x“. It is possible to make this assertion by using a built-in function “Should Contain“.

Should Contain    ${resp.content}    x


Please note that ${resp.content} is the body of the response in bytes, so you should convert the word “x“ to byte to do the assertion. The built-in function for converting to byte is “Convert To Bytes“.


To assert the headers, we should capture all headers from the response. ${resp.headers} will capture all response headers as a dictionary. As a result, to assert a specific value, we should get the value from the headers dictionary by using the key(we know that the dictionary holds keys and the related values; we can access any value by the related key). This can be done by “Get From Dictionary“ built-in function.

${content_type_value}=    Get From Dictionary     ${resp.headers}     Content-Type


Now we have the value of Content-Type header and we can assert it.

Should Be Equal    ${content_type_value}    application/json


In the picture below, you can see all the assertions at a glance.


Resources:

https://restful-api.dev/

https://robotframework.org/

Image header: by brgfx on freepik