It’s time to Robotize your API!

Vanessa Mara Redes
4 min readJun 27, 2020

The time has come for you to automate your API testing using the Robot Framework. After reading this post, it will be clear how simple and effective it is to use this framework.

Many testers are now familiar with the Robot Framework, which is a full-stack automation tool that does not require prior knowledge of programming languages.

The idea of ​​this framework is to use keywords, leaving automation within the reach of any tester/QA without complex programming techniques. In Robot you can automate web, mobile, APIs and even desktop applications. Today we are going to talk about the APIs.

To use the Robot Framework you need to have already installed Python 3 on your computer and installed the Robot Framework. To automate APIs we will use the Requests library. You can install this through your console:

pip install -U robotframework-requests

To read all the library’s documentation and all the Keywords it makes available, you must (yes, you should, don’t skip this part) read all the documentation available at:

https://github.com/MarketSquare/robotframework-requests#readme

Another very useful library for API testing is Collections. This is a native Robot library so it is not necessary to install it, just declare it in the Settings section of your test.

To access the documentation, just click here:

Let’s roll up our sleeves and get our hands dirty. We will use Júlio de Lima’s API — API da Lojinha - (By the way, if you still don’t understand a lot about APIs and tests, Julio and Antônio Montanha made a “Survival Kit” that you can watch and enjoy, unfortunately this video series is in Portuguese, but I am sure you will find a lot of cool things about APIs out there).

Firstly, we will write our tests that will become KeyWords (keywords that perform some pre-defined action) later. So as not to prolong things we will just login to the API, register a new product and then do a GET to validate that our product is there, ok? Here we go:

We start by using the Settings section to document our test. In this section, there are just a few necessary notes.

And in the Test Case section is where we write the tests.

The highlighted lines will be the future Keywords in the Resources document.

The Resources file is where the tests will become Keywords, you can find all the actions there, leaving your test file very clean and “readable”.

Isn’t it so easy?

In my Suite Setup (a section where you define some actions that must be performed before starting the entire Suite) I left my keyword “Connect my API” as it is a prerequisite for all tests that we will do now and in the future.

You can see how it is structured here, and to see how easy it is to use Robot, we just use the Keyword Create Session, give a name for our API and then pass the base URL:

Now for the tests, we will use the Keywords created in the Test Case section:

As you can see we used the Keywords “Post Request” and “Get Request” from the Requests library and it does all the heavy lifting. We just created a dictionary to pass the header you need to access our API. In the data we send our JSON directly for the API to read and consume.

After logging in we take the Token response and store it in a variable to use in the other tests (in the header, because this API requires authentication).

And so it goes for all requests. It’s very easy isn’t it? You will see your work paying off.

Now let’s take a look at some of the validations we can do:

In the variables we create one with our expected JSON, and we make comparisons between the test response and our expected JSON. Is it not easy?

To perform all these tests, we use only one command line and in a few seconds all tests are performed:

robot TestCaseAPILojinha.robot

And the coolest of all is the report that Robot already makes and makes available to us:

In it you have statistics; how many tests passed, how many failed. You can divide your tests using tags and Robot creates a log that shows all keywords step by step.

So, could you fall in love with Robot?

--

--

Vanessa Mara Redes

👩🏼‍💻- Brazilian QA 📕- The mind that opens to a new idea never returns to its original size (Albert Einstein)