Like most modern applications, RapidAPI Testing is built with an API layer connecting the backend logic with the front-end UI. In this infrastructure, the API layer becomes the critical point that ensures our Testing app is always available and working correctly. Recognizing how critical this API layer is, we use our own API Testing tool to not only monitor our production environment, but also ensure that the APIs are functioning correctly.
While creating our tests, we faced the same challenges that our users face, not the least of which is duplication of test steps. In our case, we needed to create an authentication token in each test to authenticate the rest of the API calls. This would mean adding the same authentication steps in all tests, which is time-consuming, repetitive and inefficient. This is a challenge akin to one that software developers are familiar with – having many lines of code that need to exist in multiple places. Developers have an elegant solution to this challenge. Instead of rewriting lines of code in multiple places, they simply write it once, and then “call” it in the necessary places, simply by using functions. Leveraging functions makes code more readable, easier to maintain, and results in an overall shorter codebase.
At RapidAPI, we’ve taken a similar approach to solve the challenge of test step duplication. Just as developers use functions to call the same code in multiple places, we’ve added the functionality that allows test builders to utilize test components which they have already built. We call this “Test Fragments”. In a nutshell, Test Fragments allow you to reuse common test steps across multiple tests by allowing you to call one test from another. Variables can be passed in and out of this child test, the fragment, fully unlocking the power that child tests can create.
Test Fragments In Action
Let’s take the authentication example mentioned earlier for example. In this scenario, we would create one test that performs and validates the authentication flow – let’s call this “Authentication Test”. Then, in all of the other tests that require an authentication token, we would simply call the “Authentication Test,” retrieve the necessary token, and use it in all of the places that we need it.
Using Test Fragments saves time today, eliminating the need to repeatedly add steps to obtain a token in each test, and will continue to save time in the future. When the method of obtaining tokens changes, instead of having to go into every test and modify the steps, we simply need to change the Authentication Test. All of the tests leveraging the Authentication Test as a fragment will simply inherit the change.
We want all of you to also be empowered to save time and be efficient in building your API tests by using Test Fragments. To get started, learn how to use it in our Help Center article. Happy testing!