Micro Focus is now part of OpenText. Learn more >

You are here

You are here

APIs and automated testing: Go integrated for the best of both worlds

public://webform/writeforus/profile-pictures/bas.jpg
Bas Dijkstra Test automation speaker and writer
Chocolate cake
 

Modern IT applications are becoming more distributed, mobile applications integrate with back-end systems through standardized interfaces, Internet of Things (IoT)-enabled devices communicate with each other and third-party services, and IT service providers are exposing parts of their data and services through APIs in order to generate additional cash flow (a phenomenon known as the API economy.)  

So what does that all mean for development teams? Performing automated integration tests at the API level is rapidly becoming an indispensable step in the overall development and delivery process, since malfunctioning or underperforming APIs results in difficulties in integration, a lower rate of adoption of the product or service and, ultimately, a loss of revenue.

Sadly, many software development teams and projects completely overlook the API layer when creating and executing automated tests. Too often, these teams create application components and accompanying unit tests, and then resort directly to end-to-end user interface-driven automated tests (for example by using tools such as Selenium WebDriver) when they want to determine whether multiple application components work correctly once integrated.

Writing and executing automated tests at the API layer should be an integral part of your overall testing strategy for distributed applications. Here are the benefits successful API testing adoption provides for testing and software development processes, and how you can get there. 

Avoid "Big Bang" integration testing

While integration testing has been part of the test automation pyramid for as long as the model has existed, it's also been the most overlooked layer in functional test automation. All tests that exceed the scope of an individual unit, and therefore can't be covered by unit testing anymore, are often executed through user interface-driven, end-to-end test scenarios.

There is a place for end-to-end tests in any test automation approach. But while end-to-end tests can be seen as the ultimate integration test, where all components come together, having too many of them leads to test suites that take unnecessarily long to execute, and that are hard to maintain and keep stable.

It's often possible to test significant parts of an application's technical and business logic through an API. This can be a RESTful or a SOAP-based web service meant to expose data or logic to the outside world, or an internal API used for the sole purpose of gluing different application layers together and creating a good separation of concerns.

API test automation: the best of both worlds

Here are some reasons why API-level test automation can rightfully be considered the best of both worlds:

Increased scope compared to unit tests

Unit tests focus on the workings of individual components or small groups of components within a single application or application layer. But issues in distributed applications often occur where the scope of one application (layer) ends and the next one starts.

You will not find these issues with unit tests, but API-level integration tests are designed to verify whether components interact as designed or requested. As the ability to properly integrate with external components becomes more important, your need for a proper API testing strategy will increase.

Test environment management is a potential issue when integration testing in distributed applications. Getting all components in place at the same time, as well as provisioned with the desired test data, can be a complex task—especially when you develop components within different teams or even different organizations. In such cases, you can use approaches such as mocking, stubbing, and service virtualization to perform integration testing extensively, even when critical dependencies are hard to access on demand.

Increased stability and speed of execution compared to end-to-end tests

When you compare integration tests at the API level with end-to-end, user interface-driven tests, API tests have a narrower scope. That's because they focus on integration between two components, or application layers, whereas end-to-end tests cover all components and layers of an application or distributed system.

API tests make up for this loss of scope in two areas, though. The first area where API tests outperform end-to-end tests is in execution speed. Since end-to-end user interface-driven tests require firing up an application or browser, you spend a lot of test execution time waiting for screens or web pages to load and render. To make matters even worse, much of the data that gets loaded is often insignificant for testing (think, for example, of ad banners on a web page, unless, of course, these are the subject of the test).

API tests are generally built up out of individual request-response interactions (for example in JSON or XML format), and these result in less overhead, faster execution times, and therefore in shorter feedback loops for development teams.

The other factor in favor of API tests is their inherent stability. User interfaces tend to change, due to their dynamic nature, as a result of advanced front end frameworks, or due to rapid change requests from users or other stakeholders. But APIs, especially when exposed to third parties, usually have a more stable interface.  As a result, tests require less maintenance and produce fewer false negatives due to outdated tests.

Get serious about your API-level testing

Any serious testing and test automation approach need to include API-level integration testing, but getting started can be daunting for those with no prior experience. Since APIs can cross component or application boundaries, the tests are often regarded as out of scope for developers, which leaves the responsibility to—you guessed it—testers. Whether that's a good or a bad thing, especially when considering the transition towards agile development teams, is beside the point. For better or worse, if you're a testing professional, it's in your wheelhouse.

So where do you start? One complicating factor is the absence of a user interface that you can use to access and test the API. Fortunately, you'll find myriad tools available to assist testers—even those without test automation or programming experience—in the writing and execution of useful, maintainable API tests. One such tool, REST Assured, is a Java-based DSL (Domain-Specific Language) that you can use to write readable and maintainable automated tests for RESTful APIs, even if you don't have much experience in object oriented programming.

Take a closer look at including test automation at the API level. I guarantee that you'll reap significant benefits in your testing and software development process.

Want to know more? A great way to get started is to drop in on my online presentation, "Testing RESTful APIs with REST Assured," at the Automation Guild online conference, where I'll demonstrate how to use REST Assured to write readable, powerful and maintainable tests for all of your API testing needs.

Keep learning

Read more articles about: App Dev & TestingTesting