What is API Testing

What is API Testing

In the last post we discussed about UI automation and some of its disadvantages. Here we talk about what API level tests are and why to use them.

What are APIs

Application Programmable Interfaces (APIs) is an Interface to help a Program (client side programs like browsers, or any other program) access an Application. This way the main application is placed on a secure server where all the business logic works and connects with rest of the world using an API. To learn more about the basic concepts, this video will help.

APIs in the context of automation

Taking a step back from APIs, let’s illustrate here the different wheels turning behind the scene when a user, fills a form on a website for instance.

From the image above, when we perform UI automation, we are engaging the Application Under Test (AUT) from step 1 through step 8 all the way, hence another name for UI tests, end to end tests (end to end has a contextual meaning, more on that here). Alternatively, we can start our test from step 2 instead, and validate the response at step 7, which is generally called API / integration tests. We will be invoking every process along the control flow except steps 1 and 8. In other terms the ‘client-side’ scripting part is going to be ignored and rest of the application will be tested.

Automation pyramid

As we move from the database level to the browser level (step 4 to 1) automation is going to get more complex for automation and time consuming, with the advantage of testing more technology layers of the AUT. Similarly, in comparing automation from step 1 vs step 2, API tests are going to be less complex and easy to maintain, however we will forego testing certain portions of the AUT. This concept is captured in the phrase ‘Automation Pyramid’ illustrated below.

API level tests

Since we are not testing steps 1 & 8 we will simulate that functionality performed by our AUT to complete the flow of the application necessitating to understand how our client side scripts are generating messages (POST, GET etc.) sent to the server side. Also in the response, what values are we expecting to receive which would translate into the expected output on the browser’s interface.

Perhaps this might seem a bit tricky to work on, for most cases its way easier than it sounds. Developers simulate API calls and responses all the time to complete their development. A lot of efficient tools and methods are available to learn how the API is working without need for detailed documentation or insight from the product group (not to say you shouldn’t talk to the dev team, that would kill the whole purpose of testing. Rather it’s not that hard to understand an API from the outside to begin with).

Uses of API level tests

All the drawbacks we discussed in the previous post work to the advantage of API tests. These tests compared to UI tests take a lot less time to develop and maintain, more robust due to being less susceptible to change, execute way faster and allow the testers to cover a lot test coverage with the same amount of effort as well.

Business logic

Developers while building the application do not go through the whole control flow every time they test what they just coded. Instead the back-end developers working on the server side just test the API level messages being received and sent. Similarly, the front-end developers working on the client side (front end scripting) would also test their piece of code by simulating API messages. This makes testing both areas (front end and back end) less complex and quick.

What’s in all of this for the automation guy? We can break down a complex application’s automation into smaller and easier solutions. For applications which are huge, specially having a lot of business logic, it would be a nightmare to have decent code coverage from the UI level. API calls would make that job so much easier and effective.

Complex UI

Sometimes we run into problems on the UI level where certain elements on the web page are not recognized easily by the tool, or do not always perform the way it’s expected by the tool. These tests generally termed as ‘not automatable’ can be covered from API level tests since we don’t have to deal with the user interface level complexities.

Service virtualization

Many areas of the code need a very specific circumstance to execute which is sometimes very difficult from UI tests. Perhaps there are third party APIs involved, or some external hardware sending in data used by our application, or a piece of code which is scheduled for development in the future needs to be simulated for other teams to complete their feature set. We had a similar problem in one automation project where the server was supposed to receive input data from a hardware device which was still under development.

 On the API level this becomes way lot easier. Mostly in such cases the input / output of the API is defined. Lots of tools are out there which can simulate the required calls or responses. It’s then just a matter of constructing the calls properly and validating the expected responses. You might need to create a test harness using other tools / frameworks, but it’s a very powerful concept.

Input for UI tests

Creating pre-test data and / or removing it at the end of the test can becomes a problem. API level tests can be very effective for test data management. Again one or more structured API requests would do the trick in no time. Similarly, for deleting data which is no longer needed should be equally simple (except where business logic is complex enough blocking the removal of a record by simply one call).


Would love to hear what other uses you can think of API tests.

Mahesh Joshi

FMG Insurance | Guidewire Cloud | Passionate Automation Test Analyst | Driving Quality & Efficiency in Technology

7 年

Wow this is what I have been looking for :)

Adrian Pop

Senior Software Engineer la Fabrit Global

7 年

Nice articles you have written... keep up with the good work

Salman Ahmad

We are Fuel Expense Tracking Experts. We Leverage Cutting-edge Technology to Reduce your Costs and Maximize your Savings - Let's Talk!

7 年
回复
Majd Uddin

Head Testing CoE at HBL

7 年

Thanks Ali and this is a very good article explaining all the bits at one place. I am happy that more people are getting into the business of testing through APIs. We have been doing API testing in the context of Desktop applications such that our applications consume the API. So rather than test the application more, we have been writing tests for the APIs that do the magic. Interestingly we were able to use GoogleTest as a tool to write both Unit and Integration level API tests. From experience, I can tell that API testing is far more rewarding than automating UI tests. Thanks and keep sharing your knowledge :)

Usamah Ahmed

QV Engineering @ EA

7 年

never stop writing, please.

回复

要查看或添加评论,请登录

Ali Khalid的更多文章

  • Intro to Data Analytics & Quality Workshop

    Intro to Data Analytics & Quality Workshop

    The WHY Getting familiar with the basics of data analytics can be a daunting task. With so many buzz words flying…

    15 条评论
  • Testing AI based systems - QA&TEST 2020

    Testing AI based systems - QA&TEST 2020

    This year’s QA&TEST conference 2020 was another great addition. While planning the event there were discussions on if…

    4 条评论
  • Tips on Working From Home

    Tips on Working From Home

    For a while I’ve worked from home off and on. Even while not working from home.

    7 条评论
  • QA&Test Conference 2019

    QA&Test Conference 2019

    Having a background in embedded systems, I was contacted by the QA&TEST organizers to speak at their conference in…

    5 条评论
  • My Experience at the Test Leadership Congress

    My Experience at the Test Leadership Congress

    Two weeks ago I went to the Test Leadership Congress conference in NYC, absolutely loved the content and speakers. It…

  • 900 miles to seek knowledge

    900 miles to seek knowledge

    From the past few years whenever I travel, I usually try to meet people I know from social media if I get a chance…

    7 条评论
  • The Art of Resigning

    The Art of Resigning

    When I switched jobs recently, it was a very surreal experience. The stakes were high and there was a lot of ‘be…

    12 条评论
  • Intro to Pace Layered Application Strategy

    Intro to Pace Layered Application Strategy

    Just like products across different industries are not the same, similarly software products are not the same either…

  • Lessons Learned from 2018

    Lessons Learned from 2018

    Looking back at the year I thought to jot down the most important things I learned and share with the community. People…

    11 条评论
  • The passionate knowledge worker

    The passionate knowledge worker

    One of the biggest problem employers have is they are not able to find / or get their team passionate about what they…

社区洞察