Automate · Lesson 11 of 20

Run your tests in CI with swc

Install the swc tool, run collections in any pipeline, and fail the build when a test fails.

Intermediate · 2 min · Updated

swc is the Swastik Codes command-line tool: one file, nothing else to install. It runs collections with the same engine as the app, so a test that passes in the app passes in CI.

Install it

Shell
# Linux
curl -fsSL -o swc https://swastik.codes/download/files/swc-linux-x86_64
chmod +x swc && sudo mv swc /usr/local/bin/
swc version

On Windows, download swc-windows-x86_64.exe and put it in a folder on your PATH.

Run a collection

From a file exported from the app (or a Postman collection):

Shell
swc run shop.json -e staging.json

Or straight from your account, by id, so CI always runs the latest version. Create an API key in the app under Account → API keys, then:

Shell
export SWC_API_KEY=swc_...        # keep it in your CI's secret store
swc run <collection-id> -e <environment-id>

Find the ids in the app: right-click a collection or an environment and choose Copy id. The runner's Run in CI dialog also fills them in for you.

The exit code is 0 when every test passed and 1 otherwise, which is all a CI system needs to pass or fail the build.

Reports

Shell
swc run <collection-id> -e <environment-id> -r cli,junit,html

junit writes swc/swc-run-report-<time>.xml (choose the path with --reporter-junit-export); most CI systems show it as a test report. html writes a page to keep as a build artifact, and json a machine-readable result.

Other useful options: --folder <name or id> to run part of the collection, -d data.csv for a data file, --env-var key=value to override a variable, --bail to stop at the first failure.

A GitHub Actions job

YAML
name: API tests
on: [push, pull_request]
jobs:
  api:
    runs-on: ubuntu-latest
    steps:
      - name: Install swc
        run: curl -fsSL -o swc https://swastik.codes/download/files/swc-linux-x86_64 && chmod +x swc && sudo mv swc /usr/local/bin/
      - name: Run the collection
        env:
          SWC_API_KEY: ${{ secrets.SWC_API_KEY }}
        run: swc run <collection-id> -e <environment-id> -r cli,junit

You do not have to write this by hand: the runner's Run in CI button writes it for GitHub Actions, GitLab CI, Jenkins, Azure Pipelines and CircleCI.

Run only what a change affects

On a big collection, a pull request that touches one endpoint does not need every test. If the repository holds the API's code, swc can work out which requests call an endpoint that changed:

Shell
swc run <collection-id> -e <environment-id> --affected-since origin/main --project ./backend

It reads the routes in the code now and at origin/main, finds the endpoints that changed, and runs only the requests that call them. Run the full collection on your main branch as well, so nothing is missed.

What you learned

  • swc run runs a collection from a file or your account and exits 1 when a test fails.
  • -r junit gives your CI a test report; --affected-since runs only the requests a change touches.

Next: catch breaking API changes before they are merged.

Try it on your own API

Swastik Codes runs in the browser and is free to use. Create an account to send requests, sync your workspaces and invite your team.