Midsummer, APIs and Robots
Using Robot Framework to test Calendarific API
This is my first attempt at testing an API with Robot Framework. I chose Robot Framework since it is an interesting technology that I have wanted to try out for some time. I was also motivated to demonstrate something different than testing an UI with Selenium.
The Calendarific API
Calendarific is a Restful API that supports a detailed calendar with national and regional holidays. It can help international organizations plan holidays or plan lead times across different countries for example.
I have decided to make a very basic test of the functionality and check if Calendarific returns the Swedish Midsummer celebration in June. For a Swede Midsummer is an important national holiday, and recently the celebration was the theme for a horror film no less.
dot robot
Robot Framework is built in Python and I am running the tests in a virtual environment created with pipenv. But the tests themselves are keyword driven and uses its own .robot extension for the tests. Here is a link to an brief introduction to robot framework syntax.
File structure
\---calendar_api_testing
| .gitignore
| Pipfile
| Pipfile.lock
| README.md
| resources.robot
|
\---TestCases
basic_get.robot
The test only contains one test “basic_get.robot”. I have also a separate resources.robot file that contains my api key to Calendarific (similar to how .config files are used in Pytest). The resources file is not committed to GitHub.
When opening a Python file in VS code the editor automatically detects and activates the virtual environment. Not so with .robot files. I first have to explicitly activate the virtual environment.
pipenv shell
Next run the robot command.
robot TestCases/basic_get.robot
Basic_get.robot
I will break down the description of file into the three sections below.
Settings
*** Settings ***
Library RequestsLibrary
Library Collections
Library JSONLibrary
Resource ../resources.robot
The libraries used in the framework are added to settings: Requests for creating web requests, Collections to handle lists and dictionaries and the JSONLibrary to parse json. The resource file containing the api key is also par of the settings section.
Variables
*** Variables ***
${base_url} https://calendarific.com/api/v2
${country} se
${year} 2020
${month} 6
All of the variables are used as part of the API request sent to Calendarific.
Test Cases
*** Test Cases ***
Get_holiday_info
${params}= Create Dictionary api_key=${api_key} country=${country} year=${year} month=${month}
Create Session mysession ${base_url}
${response}= Get Request mysession /holidays params=${params}
# Assertions
# Assert successful response to the API call
${status_code}= Convert To String ${response.status_code}
Should Be Equal ${status_code} 200
# Return the content-type from the response header and assert that it is a json response
${contentTypeValue}= Get From Dictionary ${response.headers} Content-Type
Should Be Equal ${contentTypeValue} application/json
# Convert the json response body to string and assert that Midsummer Day is in the response
${response_body}= Convert To String ${response.content}
Should Contain ${response_body} Midsummer Day
Get_holiday_info creates a GET requests . At first a session is created which itself does not send a request. The RequestsLibrary documentation says the following about the session “To be able to distinguish HTTP requests to different hosts and for ease of creation of test cases /../ .”
Next the “Get request” keyword sends the request with the chosen parameters that limits the query to Swedish holidays in the month of June.
The assertions as such are commented, so I won’t repeat them here. Note how assertions keywords in Robot Framework consistently begin with “should” instead of “assert”.
A link to the GitHub repository here.
A look at the test results
A test run creates both report.html and log html as well as an xml output. The inbuilt reporting capabilities are definitely a valuable part of Robot Framework. Below is an example of the more detailed log with each step of the test in a collapsible layout.
For the future
I have only demonstrated the basics in this project. With the RequestsLibrary and JSONLibrary it is possible to build much more more elaborate test where the JSON response is analysed more in depth.
My impression is that Robot Framework is a very solid tool for automated test, and I will be happy to learn more this framework. It is a simple tool with plenty versatility that has a range of libraries supporting a wide range of testing scenarios.