Get started

Install the bbrun command, write a script that runs with nothing else, then run the same script against a real service.

Install

bbrun is one executable. It needs nothing else on the machine.

macOS, with Homebrew

shell
brew install STRIMS-AB/tap/bbrun

macOS and Linux, by hand

Each release has one executable for each platform. Download the one for yours from the releases on GitHub, unpack it, and put bbrun in a folder of your PATH.

Check that it answers:

terminal
$ bbrun --version
bbrun 1.2.0

A first script

A bbrun project is a folder with a configuration file, bbrun.yaml, and scripts, *.bbrun. To start one, let bbrun write a first project that needs nothing else:

terminal
$ bbrun init shop
  wrote shop/bbrun.yaml
  wrote shop/checkout.bbrun
  wrote shop/mocks/shopApi.yaml

Run it:  cd shop && bbrun checkout.bbrun

These are the three files it wrote. Read them to see how a project fits together, or write them by hand; they are also in the repository, in examples/quickstart.

The configuration names what the scripts can call. Here there is one resource, shopApi, and in the environment local it is a mock:

bbrun.yaml
defaultEnvironment: local

environments:
  local:
    resources:
      shopApi:
        type: mock

A mock answers from routes. bbrun finds them in mocks/, in a file named after the resource:

mocks/shopApi.yaml
routes:
  - method: get
    path: /products/{id}
    expr: '{ "id": params.id, "name": "Travel mug", "unitPrice": 12.5, "currency": "EUR" }'

  - method: post
    path: /orders
    status: 201
    expr: >-
      {
        "id": "ord_" + randomString(6),
        "productId": body.productId,
        "quantity": body.quantity,
        "total": 12.5 * body.quantity,
        "status": "confirmed"
      }

  - method: get
    path: /orders/{id}
    expr: '{ "id": params.id, "status": "confirmed" }'

The script says what it is for, then does it in named steps:

checkout.bbrun
goal "A customer buys two travel mugs and finds the order afterwards"

step "Look at the product" {
    shopApi.get("/products/P100")
    assert response.status == 200      // every call puts its answer in `response`
    let product = response.body        // keep what you need: the next call replaces it
    assert product.unitPrice > 0 : "a product has a price"
}

step "Place the order" {
    shopApi.post("/orders", { "productId": product.id, "quantity": 2 })
    assert response.status == 201
    let order = response.body
    assert order.total == product.unitPrice * 2
}

step "Find it again" {
    shopApi.get("/orders/${order.id}")
    assert response.status == 200
    assert response.body.status == "confirmed"
}

Run it

From the folder shop:

terminal
$ bbrun checkout.bbrun
⚡ BBRun Script Runner (Environment: local)
   Script: checkout.bbrun
   Config: bbrun.yaml

  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

✔ SUCCESS (18ms)
  Assertions: 6 passed, 0 failed
  Requests:   3
  Steps:
    ✔ Look at the product (4ms)
    ✔ Place the order (4ms)
    ✔ Find it again (0ms)

Every resource call puts its answer in response: its status, body, headers and time. assert fails the script when what it says is false, and the failure says where and why. Change * 2 to * 3 and run again:

terminal
$ bbrun checkout.bbrun
✖ FAILED (67ms)
  Step "Place the order" failed: Assertion failed (line 14: order.total == product.unitPrice * 3); got 25.0 against 37.5; last response 201 {"id":"ord_xqn140","productId":"P100","quantity":2,"total":25.0,"status":"confirmed"}
  Assertions: 3 passed, 1 failed
  Requests:   2
  Steps:
    ✔ Look at the product (3ms)
    ✖ Place the order (57ms)

The same script on a real service

The script never said where shopApi is. Add an environment in which it is a real service, and leave the script alone:

bbrun.yaml
defaultEnvironment: local

environments:
  local:                      # nothing to start: the shop is mocked
    resources:
      shopApi:
        type: mock

  test:                       # the real service
    resources:
      shopApi:
        baseUrl: "${SHOP_URL:-http://127.0.0.1:8080}"
        headers:
          X-Customer-Id: customer-alice

Run the script in the environment test, with SHOP_URL set to a service that answers these calls. --verbose shows each call:

terminal
$ bbrun checkout.bbrun --env test --verbose
  goal  A customer buys two travel mugs and finds the order afterwards
  step  Look at the product
  call  shopApi.get /products/P100 → 200 (51 ms)
  step  Place the order
  call  shopApi.post /orders → 201 (7 ms)
  step  Find it again
  call  shopApi.get /orders/f4f3c5b2-17b6-4eda-af68-c170ba92e2f2 → 200 (7 ms)

✔ SUCCESS (128ms)
  Assertions: 6 passed, 0 failed
  Requests:   3

That is the whole idea: the script holds the logic, the environment holds the addresses and the credentials. A base URL is the root of an API; the script gives the rest of the path. ${SHOP_URL:-…} reads an environment variable of your shell, with a value for when it is not set.

Where to go next

The source, more examples and how to run scripts inside a service are on GitHub.