When you're building and testing APIs, automation is key. Bruno, as an API client, empowers you to automate your API workflows using powerful JavaScript scripts. But to truly master Bruno's scripting capabilities and write effective, bug-free tests, you need to understand one crucial concept: script execution flow
Bruno offers two distinct ways your scripts can execute during a collection run: the Sandwich Flow and the Sequential (Natural) Flow. Knowing which one to use and how they work will help you write cleaner, more organized, and more efficient scripts.
Let's dive in!
In Bruno, you can attach scripts at different levels:
Additionally, each of these levels can have a Pre-request Script (runs before the request is sent) and a Post-response Script (runs after the response is received, typically for assertions and tests).
Bruno provides two modes for how these layered scripts interact during a collection run:
The Sandwich Flow is named for how it "sandwiches" the individual request's execution between the collection and folder level scripts. This is the default and most commonly used flow because it ensures all setup (pre-scripts) happens before the request, and all teardown/validation (post-scripts) happens after the request, across all layers.
Imagine it like preparing and cleaning up a meal:
Here's a visual representation of the Sandwich Flow:
This flow is ideal for managing authorization tokens, setting global variables, and performing clean-up operations that need to happen around the core request logic.
The Sequential Flow, also called Natural Flow, executes all pre-scripts first (from outermost to innermost), then the request, and then all post-scripts (again, from outermost to innermost). It's more of a batch execution model.
Think of it as preparing all the ingredients, then cooking, then doing *all* the cleaning:
Here's a visual representation of the Sequential Flow:
While less common for typical API testing, this flow can be useful in specific scenarios where you want all pre actions to complete before any post actions begin, across the entire scope.
Regardless of whether you're using Sandwich or Sequential flow, there's a crucial rule to remember when running a collection:
Any Collection-level Pre-request or Post-response script will execute once for every single request that runs in that collection. The same applies to folder-level scripts for requests within that folder.
This means if you have 10 requests in a collection and a script at the collection level:
This behavior is fundamental for tasks like:
At the most granular level, Request Pre-request scripts execute before the API call for that specific request, allowing you to prepare the request body, headers, or URL dynamically. Request Post-response scripts execute after the API call, enabling you to extract data from the response, perform assertions and conduct tests.
These request-level scripts are where you define the specific setup and validation logic unique to each API call, working in concert with the broader collection and folder scripts.
While the Sandwich Flow is the default behavior, you can explicitly set the desired execution flow (either Sandwich or Sequential) for your collection within your bruno.json configuration file.
This allows you to override the default and specify the flow type that best suits your testing needs.
Here’s how you can configure the flow property:
bruno.json
{
"scripts": {
"moduleWhitelist": ["crypto", "buffer", "form-data"],
"filesystemAccess": {
"allow": true
},
"flow": "sequential" // Or "sandwich"
}
}
By setting "flow": "sequential", all collection runs for this bruno.json file will use the Sequential Flow. If this property is omitted, Bruno will default to the Sandwich Flow.
Bruno's script execution flows, particularly the default Sandwich Flow, provide a powerful framework for organizing your API automation. By understanding when and where each script executes, you can write sophisticated tests, manage complex authentication, and streamline your entire API development and testing workflow.
Dive into Bruno's scripting features, experiment with different flows, and build API test suites that are not just functional but also clean, efficient, and robust!
For more detailed examples and advanced use cases, refer to the official Bruno documentation on Script Flow.
Join our Discord server to connect with the Bruno community!