Scripts & tests
Scriptless assertions
Section titled “Scriptless assertions”Most checks don’t need code. On the request’s Tests tab, add assertion rows like:
| Expression | Check |
|---|---|
res.status | eq 200 |
res.body.items.length | gt 0 |
res.body.items | isArray |
Each row passes or fails on its own in the response’s Tests tab and in the Test Explorer.
Captures
Section titled “Captures”Captures, on the Vars tab, save a value from the response into a variable after it
arrives, for example the access_token from a login response. Pick the value from the last
response instead of typing its res.body… path. Choose where the value goes:
- runtime: for this session only;
- environment: the active environment, in memory;
- global: the workspace environment.
Scripts
Section titled “Scripts”For anything assertions can’t express, write JavaScript:
- Pre-request scripts run before the request is sent. Use them to compute a signature or set a timestamp.
- Post-response scripts run after the response arrives.
- Tests, on the Tests tab, hold
test()blocks withexpect()assertions.
test('returns the created order', () => { expect(res.status).to.equal(201); expect(res.body.id).to.be.a('string'); bru.setVar('orderId', res.body.id);});Pre-request and post-response scripts are on the Scripts tab. Scripts use the bru, req and res objects and run in a sandboxed engine with no file
system or process access. The full API is in the
scripting reference.
Test Explorer
Section titled “Test Explorer”Requests appear in VS Code’s Testing view under Vegha. Each assertion and each
test() is its own test item:
- run one request, a folder or the whole collection from the Testing view;
- failures show their message inline, next to the assertion or test that failed.
To run a whole collection with iterations and data files, use the collection runner.