How to use GitHub as a Data Source in Tests
You can use CSV, JSON, or XML files that are stored in a GitHub repository as a data source in your tests. Follow this guide to learn how to use this bypopulation.csv file as a data source.
What You'll Need
- A Sauce Labs account (Log in or sign up for a free trial license).
- An existing API Testing Project. For details on how to create one, see API Testing Quickstart.
Step 1: Setting Up GitHub
-
Create a new access token associated to your GitHub profile by accessing https://github.com/settings/tokens. GitHub will show you the token only once, so make sure you’re copying and pasting it somewhere safe and accessible.
noteWhen creating the token, make sure to select the
public_repo
subcategory or therepo
category based on which type of repository hosts your input data. -
Create a repository to host your input data if one doesn't exist already.
-
Commit and push a data source file. It can be a CSV, JSON or XML file.
Step 2: Creating a Basic Test
-
Go to Sauce Labs API Testing and create a new test.
-
Add the GitHub component and configure it as follow:
- Account is your GitHub username.
- Repository is the name of the repository that your data file is pushed to.
- Branch is the repository branch that the desired version of the data file is in.
- Token is the access token you created in Step 1.
- Path is the name of the file in the repository.
- Variable is the variable to store the payload in.
- Mode is the filetype of the file in the repository.
The system uses these component settings to retrieve the bypopulation.csv file, parse it as a CSV file, and assign it to the inputData variable.
- (Optional) Verify the set up by adding a Comment component for printing the parsed data as shown:
- (Optional) Run the test.
- Next, iterate over a subset of this input set by adding an Each component.
This iterates over a subset of five randomly selected items.
To help you identify an item for debugging purposes, you can add a comment that prints out the current item in each iteration. (like ID: ${_1[0]}
)
-
Use the data to perform your request. For example, in a POST Body:
{
"id":${_1[0]},
"city":"${_1[1]}",
"state":"${_1[2]}",
"population":"${_1[3]}"
} -
Add the assertions required for testing purposes.
-
Run the test.