# Run API tests in CI

> Write tests in the API client, commit the workspace to Git, and run them in GitHub Actions with cloakctl collection run and a JUnit report.

Source: https://docs.subcloak.com/cloak-proxy/guides/run-api-tests-in-ci/

Cloak Proxy's API client is a collection of plain JSON files, so your requests and their tests live in Git and run in CI with no app, like Newman or the Postman CLI. This guide writes tests on a request, commits the workspace, and runs the collection in GitHub Actions, exporting JUnit XML for the test report.

## What you need

| | |
|---|---|
| The API client with a collection and an environment | See [Collections and requests](https://docs.subcloak.com/cloak-proxy/api-client/collections-and-requests/) |
| `cloakctl` available in CI | It ships inside the app bundle at `Contents/Resources/cloakctl`; CI usually runs it from a checked-out copy or a linked path |
| A Git repository for the workspace | The runner reads the folder of JSON files directly |

## Write tests on a request

1. In the API Client (`<kbd>⇧</kbd> <kbd>⌘</kbd> <kbd>O</kbd>`), open a request and go to its **Scripts** tab, then **Post-response**. Write assertions with `pm.test` and `pm.expect`.

   *[Screenshot: A request's Scripts tab with a post-response script of pm.test assertions, and Test Results showing three passes. Post-response tests on a request, with results below.]*

2. Use the **Snippets** menu for common checks (status is 2xx, response time, JSON value, schema). Press **Send** to run them; results appear under **Test Results**.

```javascript title="Post-response script"
pm.test("Status code is 2xx", () => pm.response.to.be.success);
pm.test("Responds within 500 ms", () => pm.expect(pm.response.responseTime).to.be.below(500));
const json = pm.response.json();
pm.test("Has a name", () => pm.expect(json).to.have.property("name"));
```

> **Tip:**
> Keep secrets out of the repo. Store API keys in the [Vault](https://docs.subcloak.com/cloak-proxy/api-client/variables-and-environments/) and reference them as `{{vault:name}}`, or mark an environment variable as a secret so its value stays in the git-ignored `.cloak/` folder.

## Check the run locally

Open **Collections**, select the collection, and click **Run** to open the runner. Set iterations, pick a dataset or data file for data-driven runs, and run it.

*[Screenshot: The collection runner with iterations, a data option, a request checklist, and passed test results on the right. The collection runner. The same run happens headlessly in CI.]*

## Commit the workspace to Git

The workspace is a folder of small, pretty-printed JSON files — one per request — so it diffs and merges cleanly. Secret values stay in a git-ignored `.cloak/` folder.

- cloak-workspace.json
- collections/
  - Petstore/
    - collection.json
    - List pets.request.json
    - Create pet.request.json
- environments/
  - Staging.environment.json
- specs/
- .cloak/ (git-ignored: secret values, local state)
- .gitignore

1. Open the **Git** view in the API client to see the workspace folder. Use **Change Folder…** to point the workspace at your repo, or **Initialize Repository** to start one.

   *[Screenshot: The Git view showing the workspace folder path, Show in Finder and Change Folder buttons, and an Initialize Repository button. The Git view: the workspace folder, ready to commit.]*

2. Commit from the Git view (enter a message and **Commit All**), or from your terminal. Confirm `.cloak/` is ignored before you push.

## Run it in GitHub Actions

`cloakctl collection run` runs the collection with its scripts and tests and returns a non-zero exit code when any test or request fails. Export JUnit XML for the test report.

```bash title="Terminal"
cloakctl collection run Petstore -e Staging -r cli,junit --reporter-junit-export results.xml
```

```yaml title=".github/workflows/api-tests.yml"
name: API tests
on: [push, pull_request]
jobs:
  test:
    runs-on: macos-14
    steps:
      - uses: actions/checkout@v4
      - name: Run collection
        run: |
          cloakctl collection run Petstore \
            -e Staging \
            -r cli,junit \
            --reporter-junit-export results.xml
      - name: Publish test report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: api-test-results
          path: results.xml
```

> **Note:**
> Run `cloakctl` from the path where it lives in your setup — for example the app bundle's `Contents/Resources/cloakctl`, or a symlink you created (**Settings → Advanced → Link to /usr/local/bin**). The workflow above assumes it's on `PATH`.

### The flags you'll use

| Flag | What it does |
|---|---|
| `-e ENV` | Run with an environment (name or a `.json` file) |
| `-d DATA` | A dataset name or CSV/JSON data file: one iteration per row |
| `-n N` | Iteration count |
| `--folder NAME` | Run only one folder |
| `--bail` | Stop after the first failure |
| `--timeout-request MS` | Per-request timeout |
| `--insecure` | Skip TLS verification (same as `-k`) |
| `-r cli,json,junit,html` | Which reporters to produce |
| `--reporter-junit-export FILE` | Where to write the JUnit XML |
| `--reporter-json-export FILE` | Where to write the JSON report |
| `--reporter-html-export FILE` | Where to write the HTML report |
| `--env-var KEY=VALUE` | Override or add an environment variable |
| `--workspace DIR` | Use a specific workspace folder |

Exit code is `1` when any test or request fails, which fails the CI step.

> **Tip:**
> `cloakctl collection list` prints the collections and environments in the workspace, so you can confirm the names your workflow passes to `run`.

[Flows](https://docs.subcloak.com/cloak-proxy/api-client/run/flows/) can run in the same job: `cloakctl flow run "Checkout" -e Staging` exits with `1` when the flow fails. See [Headless and CI](https://docs.subcloak.com/cloak-proxy/automation/headless-and-ci/#run-flows-and-monitors).

## Keep model costs in check

(added in Cloak Proxy 0.4.0)

If your tests call a model API, record the run and fail the build when it spends more than you expect. A price file in the repository keeps the result independent of the prices saved on your Mac:

```bash title="Terminal"
cloakctl run --ssl '*' --record run.cloaksession -- npm test
cloakctl llm --session run.cloaksession --max-cost 0.50 --prices prices.json
```

`cloakctl llm` exits with `2` when the total is over the budget, or when a model has no price. See [LLM Inspector](https://docs.subcloak.com/cloak-proxy/inspect/llm-inspector/#in-ci).

## Related

  - [Scripts and tests](https://docs.subcloak.com/cloak-proxy/api-client/scripts-and-tests/): The pm.* API, assertions and packages.
  - [Collection runner](https://docs.subcloak.com/cloak-proxy/api-client/run/collection-runner/): Iterations, order, folders and delays.
  - [Workspace and Git](https://docs.subcloak.com/cloak-proxy/api-client/workspace-and-git/): The folder format, the .cloak/ secrets, and the Git view.
  - [cloakctl](https://docs.subcloak.com/cloak-proxy/automation/cloakctl/): Every command and flag.
