> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.doc-detective.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.doc-detective.com/_mcp/server.

# Run an existing Karate suite from Doc Detective

> Run a Karate 2.x API test suite from a Doc Detective test, check its exit code, and reuse numbers from its run summary in later steps.

Your team already covers its API contracts with [Karate](https://github.com/karatelabs/karate). Your docs still need their own tests: the procedures readers follow, the screenshots they compare against, and the recordings they watch. You don't have to choose between the two or port one suite into the other. A Doc Detective test can run the Karate suite as a shell command, fail when Karate fails, and carry Karate's results into later steps.

This page uses Karate 2.x.

## Decide what goes where

Keep each kind of test in the tool built for it:

* **Karate** owns API contract tests. They cover every endpoint, status code, schema, and edge case, often far more cases than any page documents. Developers write the feature files, and Karate's reports are where they look first when a contract breaks.
* **Doc Detective** owns what the docs promise. That's the documented requests and responses, the steps of a procedure, and the screenshots and recordings on the page. See [Test HTTP and APIs](/docs/test-docs/http-and-api).

Run Karate from Doc Detective when a docs test depends on the API being healthy. For example, a tutorial that takes screenshots of a dashboard is only worth running if the API behind it passes its contract tests. One `runShell` step gates the rest of the test on that.

## Before you begin

Make sure you have the following:

* [Doc Detective installed](/docs/get-started/installation).
* Karate 2.x and a suite that runs with the `karate run` command. The examples assume your feature files are in `src/test/features`.
* A POSIX shell for the `cat` command in Step 2. `runShell` uses Bash by default on every platform, with Git Bash on Windows, so the command works as written. If you set [`shell`](/docs/actions/runshell#shell-selection) to `cmd`, use `type` with backslash paths instead: `type target\karate-reports\karate-json\karate-events.jsonl`.

## Step 1: Run the suite and check the exit code

Add a [`runShell`](/docs/actions/runshell) step that runs the suite. Karate exits with one of these codes:

* `0`: all tests passed.
* `1`: one or more tests failed.
* `2`: a configuration error.
* `3`: a runtime error.

`runShell` passes only when the exit code is in `exitCodes`, which defaults to `[0]`. So any failure, error, or bad configuration fails this step.

```json
{
  "description": "Run the Karate API suite and write its events as JSON Lines.",
  "runShell": {
    "command": "karate run -f karate:jsonl src/test/features",
    "exitCodes": [0],
    "timeout": 600000
  }
}
```

The `-f karate:jsonl` option tells Karate to write its run events to `target/karate-reports/karate-json/karate-events.jsonl`, one JSON object per line. Karate doesn't write that file unless you ask for it. The HTML report at `target/karate-reports/karate-summary.html` is still written by default.

Raise `timeout`, 60000 milliseconds by default, to fit how long the suite takes.

## Step 2: Read the run summary into variables

The events file ends the run with a line whose `type` is `SUITE_EXIT`. Its `data.summary` object holds the totals:

* `featureCount`, `featuresPassed`, and `featuresFailed`
* `scenarioCount`, `scenariosPassed`, `scenariosFailed`, and `scenariosSkipped`
* `passed`, `true` when the whole run passed
* `durationMillis`

Print the file with a second `runShell` step, then save fields with the step-level `variables` object. Doc Detective's built-in [`jq()` function](/reference/reference/expressions#function-operators) does the parsing, so you don't need to install `jq`. The query splits the output into lines, parses each one, and keeps the `SUITE_EXIT` line:

```json
{
  "description": "Read the Karate run summary and save the totals.",
  "runShell": {
    "command": "cat target/karate-reports/karate-json/karate-events.jsonl"
  },
  "variables": {
    "KARATE_SCENARIOS_PASSED": "jq($$stdio.stdout, '[split(\"\\n\")[] | select(length > 0) | fromjson | select(.type == \"SUITE_EXIT\")][0].data.summary.scenariosPassed')",
    "KARATE_SCENARIOS_FAILED": "jq($$stdio.stdout, '[split(\"\\n\")[] | select(length > 0) | fromjson | select(.type == \"SUITE_EXIT\")][0].data.summary.scenariosFailed')"
  }
}
```

To save a different total, change the last field name in the query, such as `.featuresFailed` or `.durationMillis`. If a `jq()` query in `variables` fails, for example because a line isn't JSON, the step fails rather than storing the query text.

## Step 3: Use the saved values

Later steps read the variables as `$KARATE_SCENARIOS_PASSED` and `$KARATE_SCENARIOS_FAILED`. Use them in a command, a request, or a file you write for your team:

```json
{
  "description": "Log the Karate totals.",
  "runShell": {
    "command": "echo Karate passed $KARATE_SCENARIOS_PASSED scenarios, $KARATE_SCENARIOS_FAILED failed",
    "stdio": "/Karate passed \\d+ scenarios, 0 failed/"
  }
}
```

The `stdio` pattern doubles as an assertion. The step fails unless no scenario failed.

## Put it together

The complete spec runs the suite, reads its summary, and uses the totals:

**`karate.spec.json`**

```json title="karate.spec.json"
{
  "tests": [
    {
      "steps": [
        {
          "description": "Run the Karate API suite and write its events as JSON Lines.",
          "runShell": {
            "command": "karate run -f karate:jsonl src/test/features",
            "exitCodes": [0],
            "timeout": 600000
          }
        },
        {
          "description": "Read the Karate run summary and save the totals.",
          "runShell": {
            "command": "cat target/karate-reports/karate-json/karate-events.jsonl"
          },
          "variables": {
            "KARATE_SCENARIOS_PASSED": "jq($$stdio.stdout, '[split(\"\\n\")[] | select(length > 0) | fromjson | select(.type == \"SUITE_EXIT\")][0].data.summary.scenariosPassed')",
            "KARATE_SCENARIOS_FAILED": "jq($$stdio.stdout, '[split(\"\\n\")[] | select(length > 0) | fromjson | select(.type == \"SUITE_EXIT\")][0].data.summary.scenariosFailed')"
          }
        },
        {
          "description": "Log the Karate totals.",
          "runShell": {
            "command": "echo Karate passed $KARATE_SCENARIOS_PASSED scenarios, $KARATE_SCENARIOS_FAILED failed",
            "stdio": "/Karate passed \\d+ scenarios, 0 failed/"
          }
        }
      ]
    }
  ]
}
```

## Step 4: Run the test

Run the spec where Karate and your suite are available, such as the CI image that already runs Karate.

```bash
npx doc-detective --input karate.spec.json
```

## Verify it works

Each step reports `PASS`. If Karate exits with a non-zero code, the first step reports `FAIL` with that code, and the steps after it don't run. To see where results land, see [Reporters and artifacts](/docs/ci/reporters-and-artifacts).

## Troubleshooting

### The summary step fails with a `jq` error

**Cause:** The command printed something other than the events file, such as an error that the file doesn't exist. Karate writes `karate-events.jsonl` only when the run includes `-f karate:jsonl`, and its `-o` option moves the output to a different folder.

**Solution:** Add `-f karate:jsonl` to the suite command. If you set `-o`, point the `cat` command at the `karate-json/karate-events.jsonl` file under that folder.

### The suite step fails on a timeout

**Cause:** The suite ran longer than the step's `timeout`.

**Solution:** Raise `timeout` in milliseconds to fit the suite, or run a smaller set of feature files with Karate's tag options.

## What's next

* [Run shell and code steps](/docs/test-docs/run-shell-and-code) for more on `exitCodes`, `stdio`, and passing values between steps.
* [Run a mock backend for API tests](/docs/test-docs/mock-backend) to test documented requests without a live API.
* [Expressions](/reference/reference/expressions) for `jq()` and the other functions you can use in `variables`.