</>

Technology

Cucumber

Difficulty

Advanced

Interview Question

How do you integrate Cucumber with Jenkins CI/CD pipeline?

Answer

Cucumber + Jenkins CI/CD Integration

Jenkins Pipeline (Declarative)

GROOVY
pipeline {
    agent any

    parameters {
        choice(name: 'ENV', choices: ['staging', 'dev'], description: 'Target environment')
        choice(name: 'TAGS', choices: ['@smoke', '@regression', '@sanity'], description: 'Test suite')
        choice(name: 'BROWSER', choices: ['chrome', 'firefox'], description: 'Browser')
    }

    environment {
        MAVEN_OPTS = '-Xmx1024m'
    }

    stages {
        stage('Checkout') {
            steps {
                git branch: 'main', url: 'https://github.com/org/automateqa.git'
            }
        }

        stage('Build') {
            steps {
                sh 'mvn clean compile -q'
            }
        }

        stage('Run Cucumber Tests') {
            steps {
                sh """
                    mvn test \
                        -Denv=${params.ENV} \
                        -Dcucumber.filter.tags="${params.TAGS}" \
                        -Dbrowser=${params.BROWSER} \
                        -Dmaven.test.failure.ignore=true
                """
            }
        }

        stage('Rerun Failed Tests') {
            when {
                expression {
                    fileExists('target/rerun/failed_scenarios.txt') &&
                    readFile('target/rerun/failed_scenarios.txt').trim() != ''
                }
            }
            steps {
                sh 'mvn test -Prerun -Dmaven.test.failure.ignore=true'
            }
        }

        stage('Publish Reports') {
            steps {
                sh 'mvn verify -DskipTests'  // triggers masterthought plugin
            }
        }
    }

    post {
        always {
            // Publish Cucumber HTML report
            cucumber(
                fileIncludePattern: '**/cucumber.json',
                jsonReportDirectory: 'target/cucumber-reports',
                reportTitle: 'AutomateQA BDD Report'
            )

            // Archive reports
            archiveArtifacts artifacts: 'target/cucumber-html-reports/**/*', allowEmptyArchive: true
        }

        failure {
            // Slack notification on failure
            slackSend(
                channel: '#qa-alerts',
                color: 'danger',
                message: """
                    🔴 Cucumber Tests FAILED
                    Job: ${env.JOB_NAME} #${env.BUILD_NUMBER}
                    Environment: ${params.ENV}
                    Tags: ${params.TAGS}
                    Report: ${env.BUILD_URL}cucumber-html-reports/overview-features.html
                """.stripIndent()
            )
        }

        success {
            slackSend(
                channel: '#qa-alerts',
                color: 'good',
                message: "✅ ${params.TAGS} passed on ${params.ENV} — Build #${env.BUILD_NUMBER}"
            )
        }
    }
}

Jenkins Cucumber Reports Plugin

CODE
Jenkins → Manage Plugins → Available → "Cucumber Reports" → Install

Adds a "Cucumber Reports" link to each build with:

  • Dashboard (pass/fail chart)
  • Feature breakdown
  • Scenario details
  • Failure stack traces

Trigger Strategy

GROOVY
triggers {
    // On every PR merge to main
    githubPush()

    // Nightly regression
    cron('0 22 * * *')  // 10 PM daily

    // Scheduled smoke after deployment
    upstream(upstreamProjects: 'deploy-to-staging', threshold: hudson.model.Result.SUCCESS)
}

Maven pom.xml CI Configuration

XML
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <!-- Don't fail build on test failure — Jenkins handles it -->
        <testFailureIgnore>true</testFailureIgnore>
        <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>

Follow AutomateQA

Related Topics