Q28 of 40 · Karate
How would you integrate Karate with CI (Jenkins, GitHub Actions) including parallel execution?
Short answer
Short answer: Set karate.env via environment variable (GitHub Actions env: or Jenkins withEnv), run mvn test, and publish target/karate-reports as an artifact. Enable parallel in the JUnit runner class and assert results.getFailCount() == 0. Use 'if: always()' (GHA) or post { always } (Jenkins) to publish reports even on failure.
Detail
GitHub Actions workflow:
jobs:
api-tests:
runs-on: ubuntu-latest
env:
KARATE_ENV: staging
API_BASE_URL: ${{ secrets.STAGING_API_URL }}
API_TOKEN: ${{ secrets.STAGING_TOKEN }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with: { java-version: '21', distribution: 'temurin' }
- run: mvn test -Dkarate.env=$KARATE_ENV
- uses: actions/upload-artifact@v4
if: always()
with:
name: karate-reports
path: target/karate-reports/
Jenkins pipeline:
stage('API Tests') {
environment {
KARATE_ENV = 'staging'
API_TOKEN = credentials('staging-api-token')
}
steps {
sh 'mvn test -Dkarate.env=$KARATE_ENV'
}
post {
always {
publishHTML(target: [
reportDir: 'target/karate-reports',
reportFiles: 'karate-summary.html',
reportName: 'Karate API Tests'
])
junit 'target/karate-reports/*.xml'
}
}
}
Parallel tuning for CI: set thread count to half the agent's vCPU count — leave capacity for the running application and database. Monitor test duration as you scale.
// EXAMPLE
SuiteRunner.java
class SuiteRunner {
@Test
void runApiTests() {
// Thread count: tuned for CI agent (4 vCPU → 2-4 threads)
int threads = Integer.parseInt(
System.getProperty("karate.threads", "4"));
Results results = Runner
.path("classpath:features")
.tags("~@wip", "~@manual")
.outputJunitXml(true)
.outputCucumberJson(true) // enables Allure/ReportPortal integration
.parallel(threads);
// This assertion fails the Maven build (and the CI job) if any scenario fails
assertThat(results.getFailCount())
.as("Failing scenarios:
" + results.getErrorMessages())
.isZero();
}
}
// Run with: mvn test -Dkarate.threads=4 -Dkarate.env=staging// WHAT INTERVIEWERS LOOK FOR
// COMMON PITFALL
// Related questions
How do you set up Cypress (or Playwright) in GitHub Actions with reporting?
CI/CD & DevOps
How would you implement a pre-commit hook that blocks QA engineers from committing test files containing skipped tests or hard-coded credentials?
Git
Walk through how Karate generates HTML reports and what's actionable in them.
Karate