Midsummer, APIs and Robots

Using Robot Framework to test Calendarific API

Magnus Rydberg
Magnus Rydberg
4 min readJul 9, 2020

--

01tonythomas / CC BY-SA (https://creativecommons.org/licenses/by-sa/4.0)

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

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.

Next run the robot command.

Basic_get.robot

I will break down the description of file into the three sections below.

Settings

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

All of the variables are used as part of the API request sent to Calendarific.

Test Cases

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.

A part of log.html with the assertion of successful response expanded

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.

--

--

Magnus Rydberg
Magnus Rydberg

Forging a career in Mars-terraforming. Prior to my off-world transition I am open to Earth-based projects in software testing.