Variables allow you to pass state between requests within your API tests. For example, in one request you might create a new record, extract the new record’s ID into a variable, and then use that variable on a subsequent request to verify that the new record was created properly. Runscope also supports bucket-wide variables that can be set once and used by any test within a bucket.
Variable values in Runscope tests are isolated and ephemeral—isolated, in that variables in one test run are siloed from other test runs, and ephemeral, in that a clean variable environment is set up before each test run. There are many times in which you’ll need to create a highly complex, multi-step test that requires persisting a variable across multiple test runs. We’ve come up with an effective solution that you can easily implement so you can programmatically set and get variables in Runscope by integrating APIs from a BaaS provider.
Popular BaaS solutions for app developers are Firebase and Parse include a real-time database, user management, access control and hosting. Setting and getting data to the database is as simple as making PUT and GET calls to its RESTful API. We’ve compiled a quick guide for how you can use Firebase to persist data that is used across multiple tests.
Storing Variables for OAuth Flow Tests
An OAuth flow test is a great candidate for a persistent data store because you’ll likely need to store access tokens used for testing API endpoints that are protected with OAuth. This is particularly useful if your OAuth setup does not permit long-lived (or non-expiring) access tokens. In this example scenario, our OAuth provider supports refresh tokens, and when a refresh is called, the previous access token is invalidated, and the newly minted access token lives for just an hour.
You can add expiration checking and refresh functionality into your Runscope tests, but for the sake of keeping things simple, let’s assume that the token refresh call is taking place on a scheduled script outside of Runscope every hour. The script will make a call to the OAuth refresh endpoint, and update Firebase via a PUT request to the /oauth/token.json endpoint with the token and expiration.
Quick Guide to Integrating Firebase into Runscope Tests
The very first request in this Runscope test will fetch the current access token from Firebase using a GET call. From the JSON body, extract both the access token and expiration date and store them into variables.
On the second request, use the access token in the authorization header.
Important Considerations and Caveats
In the example above, we are fairly confident that we will not run into any problems or race conditions by introducing changes to variables by multiple test runs because changes (refreshes) are managed by an external script. However, if your testing scenario introduces dynamic changes to variable values during a test run, this may have unintended consequences on the functionality of your test assertions.
Give it a try in your own tests, and let us know what you think! You can sign up for Runscope free and get going in minutes.