Add Test Script
add_test_scriptAdd pre-request, post-response, or test scripts to a Bruno request with append or replace modes for automated API validation.
Instructions
Add pre-request, post-response, or tests scripts to a Bruno request. Canonical scriptType values are pre-request/post-response/tests; the aliases before-request (→ pre-request) and after-response (→ post-response) are also accepted. Appends to any existing script of that type by default — pass scriptMode:"replace" to overwrite it, or use remove_script to clear it. Assertions must be wrapped in test("name", function() { ... }) to be reported. Scripts run as async functions: top-level await works, and bru.sleep(ms), setTimeout and setInterval are available. Time spent waiting counts against the script timeout (settings.timeout, default 5000ms); raise it with modify_request's settings argument.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | Script body. For post-response/tests, wrap every assertion in a test() block — test("status is 200", function() { expect(res.getStatus()).to.equal(200); }); — because only test() blocks are recorded in run_collection results. A bare passing expect() at the top level records nothing and the run reports "tests": []. res.getBody() returns the response already parsed into a JS object/array for application/json and +json content-types, so read fields directly (res.getBody().field) and do NOT JSON.parse() it. | |
| scriptMode | No | How to write the script. "append" (default) concatenates onto any existing script of this type; "replace" overwrites it. Each of the three script types has its own slot in both .bru and .yml, so replacing one leaves the other two untouched. | append |
| scriptType | Yes | Script type. Canonical: pre-request, post-response, tests. Aliases: before-request (→ pre-request), after-response (→ post-response). | |
| bruFilePath | Yes | Absolute path to the .yml or .bru request file. Get from list_requests or get_collection_stats. |