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.
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
# Linux
curl -fsSL -o swc https://swastik.codes/download/files/swc-linux-x86_64
chmod +x swc && sudo mv swc /usr/local/bin/
swc versionOn 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):
swc run shop.json -e staging.jsonOr 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:
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
swc run <collection-id> -e <environment-id> -r cli,junit,htmljunit 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
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,junitYou 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:
swc run <collection-id> -e <environment-id> --affected-since origin/main --project ./backendIt 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 runruns a collection from a file or your account and exits1when a test fails.-r junitgives your CI a test report;--affected-sinceruns only the requests a change touches.
Next: catch breaking API changes before they are merged.