Iterate Using Data Files
The Collection Runner feature allows you to iterate over data files, making it easy to automate and manage data-driven requests. Bruno supports both CSV and JSON files for running requests, so you can efficiently run tests or process multiple data inputs.
Introduction
In this tutorial, we'll explore the Collection Runner feature, which enables you to run collections using custom data for each iteration.
Steps to Get Started
- Open the Bruno app.
- Create a collection called
runner-example
- Create a POST request and name it
runner-request
- Use the URL:
https://reqres.in/api/users
- Select the JSON format for the request body and add the following data:
{
"name": "morpheus",
"job": "leader"
}
Selecting the JSON option for the request body and adding JSON data is optional.
Using the Collection Runner
We will create a sample data file csv-example.csv
that includes input fields such as name
and job
to be used in data-driven testing. You need to create a CSV or JSON file according to the specific requirements of the API you're working with.
Since the API in this case expects two data inputs name
and job
the file should contain these fields. Here's an example of how to structure your data:
1. CSV Format Example
A sample CSV file might look like this:
name, job
John Doe, Software Engineer
Jane Smith, Product Manager
Mark Lee, Data Scientist
2. JSON Format Example
A sample JSON file might look like this:
[
{ "name": "John Doe", "job": "Software Engineer" },
{ "name": "Jane Smith", "job": "Product Manager" },
{ "name": "Mark Lee", "job": "Data Scientist" }
]
Now you're ready to use the Collection Runner. You can access it in two ways:
Using the Bruno App
- Click on the runner icon in the right-hand navbar.
- Check the Run with Parameters option.
- Select the file type: CSV or JSON.
- Click Run Collection.
Once the execution is complete, you can review the results for each individual request and check their statuses.
How to Use Variables from CSV/JSON Files
When you upload a CSV or JSON file, the variables within the file can be accessed using the {{var}}
syntax.
You can access these variables dynamically in your requests and use them within Bruno.
Example:
Accessing Variables in Request Body
{
"name": "{{name}}",
"email": "{{email}}"
}
In this example, {{name}}
and {{email}}
will be replaced by the actual values from the uploaded JSON file.
{
"name": "John Doe",
"email": "john.doe@example.com"
}
Accessing Variables in Scripts
You can also access these variables directly in your pre-request or post-response scripts using bru.getVar()
.
console.log(bru.getVar("name")); // Outputs: John Doe
console.log(bru.getVar("email")); // Outputs: john.doe@example.com
When you run the request, you’ll see the output of these variables in the console.
Using the Bruno CLI
- Navigate to the root directory of your Bruno collection.
- Run the following command:
bru run --reporter-html results.html --csv-file-path /path/to/csv/file.csv
or
bru run --output results.html --format html --csv-file-path /path/to/csv/file.csv
It will create a results.html
file in your Bruno collection's root directory. You can view this file in your browser.
Command Overview
--reporter-html results.html
: Generates a human-readable HTML report.--csv-file-path /path/to/csv/file.csv
: Specifies the path to the CSV file you want to use.