2. Using Variables
Now that we have a grasp of the basics, let’s explore how we can build on them. This test uses the token generated by the previous test to test an authenticated API endpoint.
Using a variable
In the previous test, we called a login endpoint and extracted the returned token into a variable named “token”.
That variable is now available to any other tests in the same run
session.
So here, we embed the token in the Authorization header using the ${token}
syntax.
And then we call the https://api.jikken.io/api/v1/examples/status
API endpoint.
Since the HTTP method is not specified, it will default to GET
.
Dependent tests
For this test to work, we need the “token” variable to be populated, which means the order of the test execution matters.
To ensure the login test runs before this one, we specify that this test requires
“auth”, which is the id
of the previous test.
As before, the only response
validation we perform is verifying that the HTTP status
code is 200.
Next, we’ll take a look at how to call multiple API endpoints in a single test, and compare the results.