๐ฅ Enabling Cucumber Reports in KarateBDD with Maven


๐ 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
๐ 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
Subscribe to my newsletter
Read articles from Himanshu Pandey directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
