Add Test Script
add_test_scriptAdd pre-request, post-response, or test scripts to a Bruno request, appending by default or replacing existing scripts when needed.
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 spend the script timeout (settings.timeout, default 5000ms), which write_request's settings argument raises.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"bruFilePath": "<bruFilePath>",
"scriptType": "<scriptType>",
"script": "<script>"
},
"name": "add_test_script"
}
}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 top-level expect() records nothing and the run reports "tests": []. res.getBody() returns the response already parsed 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 script type has its own slot in both .bru and .yml, so replacing one leaves the others 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. |