Q28 of 40 · Karate

How would you integrate Karate with CI (Jenkins, GitHub Actions) including parallel execution?

KarateSeniorkarateci-cdjenkinsgithub-actionsparallel-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

Passing karate.env and secrets via CI environment variables (not baked into the runner), publishing reports with 'always' so failures are diagnosable, JUnit XML for CI test result parsing, and configurable thread count via system property.

// COMMON PITFALL

Not publishing reports when the build fails — if you only publish on success, the Karate HTML report (with the HTTP payloads showing what went wrong) is unavailable for the most important case: a failing build.