Assertions & Tests
Tests let you assert that a response is correct — the right status, the right headers, the right body — and see pass/fail results after each run.
The Tests tab
Section titled “The Tests tab”Each request has a Tests tab. Scripts you write there run after the response arrives, alongside any post-response script, and have access to the same bru, req, and res objects.
test() and expect()
Section titled “test() and expect()”Wrap each assertion in a named test() block. Inside it, use expect() to make the assertion:
test("status is 200", () => { expect(res.status).to.equal(200);});
test("response is fast enough", () => { expect(res.responseTime).to.be.below(500);});Each test() block reports independently, so one failing assertion does not hide the others.
Common assertions
Section titled “Common assertions”| Goal | Example |
|---|---|
| Status code | expect(res.status).to.equal(201) |
| Header present | expect(res.headers["content-type"]).to.include("json") |
| JSON body field | expect(res.body.user.id).to.equal(42) |
| Field exists | expect(res.body.token).to.exist |
| Array length | expect(res.body.items).to.have.lengthOf(3) |
| Response time | expect(res.responseTime).to.be.below(800) |
A typical Tests tab combines several checks:
test("created order", () => { expect(res.status).to.equal(201); expect(res.body.order.id).to.exist; expect(res.body.order.status).to.equal("pending");});The Test Results view
Section titled “The Test Results view”After a request runs, the Test Results view lists every test() block with a pass or fail marker. Failed assertions show what was expected versus what was received, so you can diagnose the difference quickly.
Running tests for a whole collection
Section titled “Running tests for a whole collection”You can run an entire collection in one pass. Every request’s tests execute in order, and the Test Results view aggregates pass/fail counts across all of them — useful for regression-checking an API after a change.
See the Scripting API reference for the full test and expect API.