๐Ÿฅ‹ Enabling Cucumber Reports in KarateBDD with Maven

Himanshu PandeyHimanshu Pandey
3 min read

๐Ÿ” Whether you're just getting started with API testing in KarateBDD or want to level up your test reports with beautiful Cucumber HTML output โ€” this guide walks you through every step.

๐Ÿ“š Table of Contents

  1. ๐Ÿš€ Prerequisites

  2. ๐Ÿ› ๏ธ Step 1: Set Up a Karate Maven Project

  3. โœ… Step 2: Validate Setup with mvn test

  4. ๐Ÿ“Š Step 3: Add Cucumber Reporting Dependency

  5. ๐Ÿงช Step 4: Update Runner File for Reporting

  6. ๐Ÿ“ Step 5: Generate Cucumber HTML Report

  7. ๐Ÿ”š Conclusion

๐Ÿš€ Prerequisites

Before we dive in, make sure you have:

  • Java 17+ installed

  • Maven installed and available on your system PATH

  • A terminal/CLI that can run Maven commands

๐Ÿ› ๏ธ Step 1: Set Up a Karate Maven Project

We'll start by using the official Karate Quickstart Guide.

mvn archetype:generate \
-DarchetypeGroupId=io.karatelabs \
-DarchetypeArtifactId=karate-archetype \
-DarchetypeVersion=1.5.1 \
-DgroupId=com.example \
-DartifactId=karate-cucumber-demo

๐Ÿ‘‰ This creates a folder karate-cucumber-demo with a sample feature and test file ready to go:

โœ… Step 2: Validate Setup with mvn test

cd karate-cucumber-demo
mvn test

You should see output indicating that the default Karate test has passed. This confirms your setup is working. ๐ŸŽ‰

๐Ÿ“Š Step 3: Add Cucumber Reporting Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>net.masterthought</groupId>
    <artifactId>cucumber-reporting</artifactId>
    <version>5.8.1</version>
</dependency>

๐Ÿ”ง Pro tip: Place it in the <dependencies> section.

This library will help us generate pretty HTML reports from the Karate test results.

๐Ÿงช Step 4: Update Runner File for Reporting

Default Runner File ExamplesTest.java, created in Step 1 that need to be updated:

package examples;

import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class ExamplesTest {

    @Test
    void testParallel() {
        Results results = Runner.path("classpath:examples")
                //.outputCucumberJson(true)
                .parallel(5);
        assertEquals(0, results.getFailCount(), results.getErrorMessages());
    }

}

Updated ExamplesTest.java File :

package examples;

import com.intuit.karate.Results;
import com.intuit.karate.Runner;

import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;

class ExamplesTest {

    @Test
    void testParallel() {
        Results results = Runner.path("classpath:examples")
                .outputCucumberJson(true)
                .parallel(5);
        generateReport(results.getReportDir());
        assertTrue(results.getFailCount() == 0, results.getErrorMessages());
    }

    public static void generateReport(String karateOutputPath) {
        Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[] {"json"}, true);
        List<String> jsonPaths = new ArrayList<>(jsonFiles.size());
        jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
        Configuration config = new Configuration(new File("target"), "karate-cucumber-demo");
        ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
        reportBuilder.generateReports();
    }

}

๐Ÿ“ Step 5: Generate Cucumber HTML Report

Run the test class:

mvn clean test

๐ŸŽ‰ After the test run, you should find your report at:

target/cucumber-html-reports/overview-features.html

something like this:

Open it in your browser to enjoy the beautiful HTML report with feature summaries, step details, and pass/fail status

๐Ÿ”š Conclusion

You've now set up a full KarateBDD project that:

โœ… Runs tests using Maven
โœ… Outputs JSON for each scenario
โœ… Generates a rich Cucumber HTML report


0
Subscribe to my newsletter

Read articles from Himanshu Pandey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshu Pandey
Himanshu Pandey