Run an existing Karate suite from Doc Detective
Your team already covers its API contracts with 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.
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.
- Karate 2.x and a suite that runs with the
karate runcommand. The examples assume your feature files are insrc/test/features. - A POSIX shell for the
catcommand in Step 2.runShelluses Bash by default on every platform, with Git Bash on Windows, so the command works as written. If you setshelltocmd, usetypewith 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 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.
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, andfeaturesFailedscenarioCount,scenariosPassed,scenariosFailed, andscenariosSkippedpassed,truewhen the whole run passeddurationMillis
Print the file with a second runShell step, then save fields with the step-level variables object. Doc Detective’s built-in jq() function 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:
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:
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:
Step 4: Run the test
Run the spec where Karate and your suite are available, such as the CI image that already runs Karate.
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.
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 for more on
exitCodes,stdio, and passing values between steps. - Run a mock backend for API tests to test documented requests without a live API.
- Expressions for
jq()and the other functions you can use invariables.