How to run API tests in Bitbucket Pipelines

Bitbucket Pipelines picks up JUnit test reports automatically and shows them on the pipeline's Tests tab. Pair that with the Requestarium CLI and the API collections your team already uses become a check on every pull request.

Before you start

  1. Create an API token for CI in Requestarium: Settings → API tokens. It starts with trt_.
  2. In Bitbucket, open Repository settings → Repository variables and add REQUESTARIUM_TOKEN as a Secured variable. Add any keys your requests need the same way, for example STAGING_API_KEY.
The API tokens settings page with a new token named GitHub Actions, shown once with a Copy button
Create a dedicated token for CI in Settings → API tokens. It is shown only once.

The pipeline

YAML
image: node:22

pipelines:
  pull-requests:
    '**':
      - step:
          name: API tests
          script:
            - npm install -g @requestarium/cli
            - requestarium run-collection "Orders API" --env Staging
                --env-var API_KEY=STAGING_API_KEY
                --junit test-results/requestarium.xml --save-run
  branches:
    main:
      - step:
          name: API tests (full)
          script:
            - npm install -g @requestarium/cli
            - requestarium run-collection "Orders API" --env Staging --retries 2
                --junit test-results/requestarium.xml --html report.html --save-run
          artifacts:
            - report.html

A few notes:

  • Test reports. Bitbucket looks for JUnit XML in directories such as test-results, so writing the report there is enough for the Tests tab. Each pm.test shows up as a test case, and a request without tests gets one case that checks the status is below 400.
  • Secrets. --env-var API_KEY=STAGING_API_KEY reads the secured variable into {{API_KEY}}. The value is masked in reports, and the run stops if the variable isn't set.
  • History. --save-run records the run in Requestarium with the branch, commit and a link to the pipeline, so failures from CI and from developers' machines show up in one place.
  • Exit code. The CLI exits with 1 when a request fails, errors or has a failed test, and the step fails with it.

Report failures to Jira automatically

Bitbucket teams usually live in Jira. With the Jira integration connected (Pro and Team plans), a failed run can file an issue, or comment on the open one, instead of waiting for someone to notice:

Terminal
requestarium run-collection "Orders API" --env Staging --jira-on-failure OPS

A collection that keeps failing doesn't open a new issue every time: the next failure on the same branch adds a comment to the issue that's still open. Details in File Jira issues from failed API tests.

Settings → Integrations: connect Jira Cloud with a site URL, account email and API token
An admin connects Jira once, in Settings → Integrations

Scheduled runs

In Bitbucket, Pipelines → Schedules runs a branch's pipeline on a schedule. Combine it with a custom pipeline for a nightly regression run:

YAML
pipelines:
  custom:
    nightly-api:
      - step:
          script:
            - npm install -g @requestarium/cli
            - requestarium run-collection "Orders API" --env Staging --save-run --max-p95 800

Keep the collection in Git too

If you'd like the collection itself to live in the same repository, Requestarium can sync a collection with Bitbucket as plain YAML files, one per request, with Push and Pull from the app.