Using Child Tests
A child test is a method of defining a set of steps once and executing those steps in other tests.
How does this benefit me?
If you have written a lot of tests, chances are that you have come across cases when you have had to use the same set of steps in multiple tests. For example, you may require authentication for your API requests, and thus need to call an authentication API to obtain a new token in each of your tests. You may also need to perform some processing after obtaining the token to prepare the data for an API call.
When programmers write code, they face a similar case where they need to utilize the same lines of code in multiple places. This is handled by making those lines of code modular (e.g., using functions), where it is written once and then called in multiple places that need it.
At RapidAPI, we apply the same principles of modularization to API Testing through Child Tests. That is, a Child Test allows you to define a set of test steps once, and call those steps in multiple tests that need them.
How do I use it?
We accomplish test modularization in RapidAPI by allowing a test to be called by other tests. Let us walk through an example by using the authentication use case.
For simplicity, we will assume that our authentication involves two steps. First, calling the /token endpoint and then creating a string โJWT_token=<token_from_step1>โ. In our example, these are the authentication steps that will need to happen in every test.
Create the Child Test
First create the child test, which is the test that we will โcallโ in our other tests. It is simply a regular test with the two steps from above in it.
Calling the Child Test
You can utilize the child test in other tests now that we have created it. In this example, we are going to make a second test and name it โGet Productsโ. As a first step, we want to call the authentication child test we created earlier to obtain a token. We can do that by using โ+ Add stepโ and selecting โExecute Child Testโ.
We now have a step in our new test called โExecute Child Testโ. We need to give it some information so that it knows what to do.
-
First, we will select the child test we want to use. In this example, we selected the โAuthenticationโ test we created earlier. Note that only tests within the same API Project can be leveraged as a child test.
-
We want to access all the output variables generated within the child test. We will do this by entering a desired variable name in the Variable field. In the screenshot below, we will name it โauthTokenโ. All the variables in the child test are scoped under authToken and are accessible from the parent test through the
{{{authToken.*}}}
nomenclature. As an example to #2, if we want to use โfullTokenโ, we can access it by using{{{authToken.fullToken}}}
. -
For convenience, once you select a test, you can easily see the child test definition by clicking the link icon.
That is it! At this point, we have defined a child test, called the child test from a parent test, and used values generated by the child test in the parent test.
Providing Input Variables
Similar to how functions work in code, there are instances when you want to pass input variables to the child test. We will walk through an example of how to do this in RapidAPI Testing.
In this example, we will use a Get Product test as the child test. The test performs a GET request for a specific product and performs several assertions. We also want the child test to take a product ID as an input for the GET request.
To accomplish this, we will first add the product ID as a test variable in the child test and then use the test variable in the GET request.
We will call the child test and pass a product ID as an input variable. We can do this through the โVariable Overridesโ section of the โExecute Child Testโ step. Each input in the Variable Overrides section is a key-value pair, where the key is the name that we used in the Test Variable, and the value is the new value we want to override.
When the test is executed, it will use the value we pass here as the product ID (in this example it is 212313), as opposed to the value we defined in the Test Variable of the child test (12345).
Updated about 1 year ago