Scenarios

A scenario uses your services the way a user or an app does, checks what they answer, and says what passed. It knows nothing of how they are built: that is black box testing.

A scenario

scenarios/upload-and-read.bbrun
goal "A user uploads a file and reads its content through an API"

include "_lib/sign-in.bbrun"

step "Noah uploads the file" {
    files.post("/files", { "name": "stores.csv", "rows": readCsv("fixtures/stores.csv") })
    let fileId = response.body.id
    cleanup {
        files.delete("/files/${fileId}")
    }
    assert response.status == 201
}

step "The portal reads it" {
    portal.get("/stores?City=Berlin")
    assert response.status == 200
    assert response.body.total == 1
}
StatementWhat it does
goal "…"Says what the script is for. A script may have several, the general one first. Goals are printed and kept in the result.
step "…" { }Runs a block as one named part of the scenario. A failure inside is reported as Step "…" failed, and the result lists each step with how it ended and how long it took. What a step declares stays visible after it.
cleanup { }Keeps a block for the end of the script, passed or failed. Blocks run last kept, first run, each with the variables and the credentials of the moment it was kept: write it right after making what it removes.
include "…"Runs another script here: its variables, credentials, clean-ups and functions become this script's. Good for a sign-in, a set-up or steps that several scenarios share.

A clean-up that fails makes a passing script fail. After a failure it is only a warning: the first failure is the one to read.

Checking answers

A scenario checks what the outside sees: status, headers and body.

bbrun
shopApi.post("/orders", { "productId": "P100", "quantity": 0 })
assert response.status == 400
validate response.body as ApiError
assert response.body.code == "INVALID_ORDER"
assert response.headers["content-type"] contains "application/json"
warn response.time < 300 : "slower than 300 ms"

assert and warn are described in The language. validate checks a whole body against a model of your API's contract, so that a scenario does not need a line for every field: see Models and mappings.

A shared sign-in

A script that several scenarios include does its part once, in the scenario's own context. A sign-in gets a token and sets it on the resource, and every call after the include is signed in:

_lib/sign-in.bbrun
signIn.post("/login", form({ "email": env("USER_EMAIL"), "password": env("USER_PASSWORD") }))
assert response.status == 302 : "the sign-in was refused"
api.post("/token", { "code": urlParam(response.headers.location, "code") })
authenticate(api, { "type": "bearer", "token": response.body.accessToken })

Keep such scripts in a folder whose name starts with _: a suite leaves those out, so they run only where they are included.

Steps that several scenarios take with different values are functions in such a file. The include makes them known; each scenario calls them with its own values:

_lib/orders.bbrun
function placeOrder(productId, quantity) {
    shopApi.post("/orders", { "productId": productId, "quantity": quantity })
    assert response.status == 201 : "ordering ${quantity} of ${productId} answered ${response.status}"
    let orderId = response.body.id
    cleanup {
        shopApi.delete("/orders/${orderId}")
    }
    return response.body
}
bbrun
include "_lib/orders.bbrun"

step "Two mugs and a lid" {
    let mugs = placeOrder("P100", 2)
    let lid = placeOrder("P205", 1)
    assert mugs.total > lid.total
}

A folder of scenarios

shop/
  bbrun.yaml
  mocks/
    shopApi.yaml
  _lib/
    sign-in.bbrun
  01-checkout.bbrun
  02-invalid-input.bbrun
  03-not-found.bbrun
terminal
$ bbrun run shop/
⚡ BBRun 3 scripts (Environment: local)
   Config: shop/bbrun.yaml

▶ shop/01-checkout.bbrun
  goal  A customer buys two travel mugs and finds the order afterwards
  step  Look at the product
  step  Place the order
  step  Find it again
  ✔ passed (12ms, 6 assertions, 3 requests)

▶ shop/02-invalid-input.bbrun
  goal  An order for nothing is refused
  ✔ passed (0ms, 2 assertions, 1 requests)

▶ shop/03-not-found.bbrun
  goal  A product that does not exist is not found
  ✔ passed (0ms, 2 assertions, 1 requests)

Summary
  PASS shop/01-checkout.bbrun  3/3 steps  12ms
  PASS shop/02-invalid-input.bbrun  0ms
  PASS shop/03-not-found.bbrun  0ms

✔ 3 passed, 0 failed, 3 scripts

Each script starts fresh: nothing one scenario declared or signed in to is seen by the next. Running, options and load tests are in The command line.