Tests component and use a Chai-based assertion library.
Overview
Tests in Bruno are JavaScript code snippets that run after receiving an API response. They use the same execution environment as scripts but are specifically designed for assertions and validations.Tests vs Assertions
Bruno provides two ways to validate responses:- Tests Tab
- Assert Tab
JavaScript test suite using Chai assertions. Managed by the
Tests component.Use the Assert tab for simple, declarative assertions. Use the Tests tab for complex validations, loops, and conditional logic.
Test Editor
TheTests component provides a CodeMirror editor with:
- JavaScript syntax highlighting
- Auto-hints for
req,res, andbruobjects - Keyboard shortcuts (Cmd/Ctrl+Enter to run, Cmd/Ctrl+S to save)
- Error indicators in the Tests tab
RequestPane/Tests/index.js:
Available Objects in Tests
object
Response object containing:
res.status: HTTP status code (e.g., 200, 404)res.statusText: Status text (e.g., “OK”, “Not Found”)res.headers: Response headers objectres.body: Parsed response body (JSON object/array)res.getHeader(name): Get specific header valueres.getBody(): Get raw response body
object
Request object (read-only in tests):
req.url: Request URLreq.method: HTTP methodreq.headers: Request headersreq.body: Request body
object
Bruno utility object:
bru.getVar(name): Get collection variablebru.setVar(name, value): Set collection variablebru.getEnvVar(name): Get environment variablebru.setEnvVar(name, value): Set environment variable
Chai Assertion Library
Bruno tests use Chai for assertions. Theexpect and test functions are globally available.
Basic Assertions
- Equality
- Type Checking
- Existence
- Comparison
Advanced Assertions
- Object Properties
- Arrays
- Strings
- Headers
Real-World Examples
From the Bruno test suite:Form URL Encoded Test
bruno-tests/collection/echo/echo form-url-encoded.bru
Collection-Level Test
Fromcollection.bru:
Assert Combinations Test
Frombruno-tests/collection/asserts/test-assert-combinations.bru showing various assertion patterns:
Common Testing Patterns
Test Status Code Range
Test Status Code Range
Validate Response Schema
Validate Response Schema
Test Pagination
Test Pagination
Validate Email Format
Validate Email Format
Test Array Elements
Test Array Elements
Conditional Testing
Conditional Testing
Test Error Responses
Test Error Responses
Test Storage
Tests are stored in the.bru file:
updateRequestTests action.
Test Execution & Results
When you send a request:1
Request Executes
Bruno sends the HTTP request using the Axios client.
2
Response Received
Response data is captured and made available as
res object.3
Tests Run
Tests execute in order. Each
test() function runs independently.4
Results Displayed
Test results appear in the Response Pane, showing passed/failed tests.
Test Error Indicators
The Tests tab shows error indicators:From HttpRequestPane component
item.testScriptErrorMessage.
Collection Runner Tests
When running multiple requests using theRunCollectionItem component, tests run for each request and results are aggregated in RunnerResults.
Best Practices
Write Descriptive Test Names
Write Descriptive Test Names
Use clear, descriptive names that explain what is being tested:
Test One Thing Per Test
Test One Thing Per Test
Keep tests focused on a single assertion or related group:
Use Appropriate Assertion Types
Use Appropriate Assertion Types
Choose the right Chai assertion for readability:
Validate Both Success and Error Cases
Validate Both Success and Error Cases
Test happy paths and error scenarios:
Extract Common Validations
Extract Common Validations
Put repeated validations in collection-level tests:
collection.bru tests
Next Steps
Assertions
Learn about inline assertions using the Assert tab
Scripts
Combine tests with pre/post-request scripts
Collection Runner
Run tests across multiple requests
Collection Settings
Configure collection-level tests