Diving Into Automated API Tests

Khan Saad Bin Hasan
Towards Data Science
8 min readJul 12, 2020

--

Source

What comes to mind when thinking of automation? Robots making robots, large factories producing products, vehicles driving themselves, or maybe some other thing far into the future? Well, automation is already far more than this.

Automation means that we are making some sort of machine do the work with minimal human assistance. The most common example of automation that might come to the mind of most people would be that of a factory. Where a number of machines are working in a synchronous fashion with a great number of conveyor belts going all around the facility.

Source

This image has led many people to believe that soon all factories will be run like this. However, automation is far broader than this narrow vision. The most important automation is happening all day in our lives and we don’t even pay much attention to it.

What Is API and Why Do We Even Need It?

Let us take an example of a social media application. You might have seen the name of places in social media posts. When we click on them, they will take us to a map that will point us to a particular place. Now the social media company may have a map service of its own. However, more often than not, it will be using a third-party map service.

This third-party map service may be providing services to many other applications. If it allows all the applications to use its original database, it might lead to the corruption of data at the very least. And complete destruction of data (along with some hefty lawsuits) at worst. Hence, to allow any app to use its database without any restrictions is a very bad and impractical approach.

Nor is it possible for this service provider to send a copy of data to all the applications, since every app may need only a very small subset of the data. Also, the data has to be updated as soon as possible. Then there is the problem of astronomical bandwidths required to send so much data regularly.

How can we overcome these issues so that the map service can send the data to the social media application?

How about — The map service provides a set of functions, tools, and rules for communication with proper documentation to the social media app. Using these services the social media app can get the data it needs without endangering the security of the map service provider (Of course some authentication system must be required).

This package of functions, tools, and rules is called an API or Application Programming Interface since it provides an interface to interact applications.

Source

I like to think of an API as a set of buttons on a complex machine (let us say a computer with a keyboard :) ). You may want this machine to do something for you. What you have to do is to use those buttons to direct it to do your work.

For example, let the machine contain a database of maps. Now we might ask the machine to provide us the information about a particular coordinate and it would slip out the information, without us bothering much about its inner workings or what other data it has. This is a relatively secure way to communicate with the machine.

Apart from providing the above advantages and services, an API is usually cross-platform and is available to be used with a number of programming languages. This is important since more than one application might request data from one service.

For example, let us consider our map service again. Let us say another application wants to use its data. However, this application uses a different language and platform than our social media app. How might it use the data from the map service provider?

Here again, an API comes in. An API should be able to work with multiple languages and platforms hence it should help overcome this issue.

Why Test APIs?

APIs are very useful in our day to day activities. If you are using any sort of computer device, you might be using APIs without knowing it. Most applications and software communicate via APIs. Even different parts of software communicate via APIs.

Now, if you are using APIs so much, A natural question might arise. How do I know which API is best for me? Or how do I make sure the API I am using is safe and is working as is expected of it. Or maybe since most software is using APIs are they even safe? Do they value my privacy? And are there any loopholes that can be exploited to access my data.

APIs provide us a passage to the database of a company. As you may expect, if APIs are not built properly, they might be used to steal or corrupt the data. Hence, it is extremely important that we test our APIs thoroughly.

However, security is not the only issue to test our API. We also need to make sure that all the provided functions work as expected. And that the API can handle as many users as is claimed by it. We should also test whether it works when stretched to its limits. There can also be a number of corner cases that we might want to consider.

The company that provides APIs may use humans for this task. But the sheer number of APIs that are available and the number of skills the human requires to perform these tests would make it unfeasible. Add to it the time that an application will save as compared to human testers and you would agree that an application is better off testing the API than humans.

There are a number of API testing tools available. Most popular of them being JMeter, Postman, REST assured. I found a great video that explains the REST API with examples. I recommend you watch this video to get more information about APIs in general. Viewing this video would give you a feel of how applications are used to test an API. I also highly recommend this blogpost.

Till now we have established the usefulness of automation. And its superiority in cost-saving and efficiency. We should go a little deeper into testing to understand how it can be automated and why automating it is a reasonable choice.

Why Should We Automate Testing?

You may have seen messages like “404 website not found” or “200 OK” while browsing the web. These messages are very important to understand the state of our request. For example, let us say we send a request for a webpage to a server. It might not have that webpage and may return a specific code. Or it may have that website or file and may return a specific code.

We can check the code and get information about our request. Since we know what request to send and what we expect. We can put this information in an automation tool and let it query all the servers for the website or file. This would automate the testing and help us.

Let us consider another example. Let us say we have software with a huge amount of code. If you have written code before in your life, you may have come across this issue — you change some small thing and it destroys the whole code. So, if a developer changes some small thing in the code it has the potential of destroying the whole software.

To make sure this does not happen we run a number of tests. We upload these to the testing tool and whenever we make any changes to the software we run these tests. If some issue is found we recheck our code before committing it to the source code of the software. This can potentially save us hours of headaches.

There are a number of API testing tools available out there. The one I’ve been using is Loadmill, a free tool I found quite helpful. Let us take a closer look at this tool to understand what it is doing and how it can help your testing process.

Well firstly, we have to choose the tests we want to run (or make our own tests). Then we run the tests. This is what the final result might look like:

Source: Automated test from loadmill

You can see it runs a number of tests that can be fed into it. These tests can then be used to run on multiple websites. Here, it runs 9 tests and reports the time taken and whether the tests failed or passed. Here 8 of the tests have passed and only one test failed.

Clicking on any of these tests would show more information about the test, including why the test passed or failed, or what went wrong.

Final Words

I hope I have convinced you that automation is indeed very helpful and we are using it in our everyday lives without even noticing its efficiency or lauding its role in making our lives simpler and more productive.

Automation is nothing new. Humans have been looking to find clever ways to automate things so they can focus on other endeavors. I would consider a wheel as automation to solve the problem of too much walking. Or the usage of windmills in medieval times to ground wheat. Or the usage of engines to drive cars.

However, recently there has been a surge in the number of people considering the rise of automation as a negative thing that would render humans useless. This idea can not be farther from the truth. Automation has been around since the beginning and should be around till later. We can always make things more efficient.

People who argue negatively about automation do not realize that when engines came they also displaced a number of jobs but in time new jobs were also created and on the whole humanity only progressed.

Even though modern technology has gone very far it still has a long way to go, at each step, there are a number of people maintaining all the tools and working on making them better and better. Also, even if we make a program to automate a job, we will need a number of developers to maintain that program.

I think as we acquire more and more data and the technology gets better, there will be more and more tasks that will be automated. The wiser approach is to find tasks that can be automated and try to automate them, then move onto the next task. Once all are done let’s focus on going to Mars!

--

--