To use cucumber reporting in your karate project, you first need to add the maven dependency.
Maven Dependency
Add the net.masterthought:cucumber-reporting
jar as a dependency in test
scope
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
Since the maven-cucumber-reporting plugin has an issue where reports will not be generated if the build fails, we recommend that you directly use the cucumber-reporting library programmatically in combination with the Karate parallel runner. Here is how:
package gitAutomation; import com.intuit.karate.KarateOptions; import com.intuit.karate.Results; import com.intuit.karate.Runner; import net.masterthought.cucumber.Configuration; import net.masterthought.cucumber.ReportBuilder; import org.apache.commons.io.FileUtils; import org.junit.Test; import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.List; import static org.junit.Assert.assertTrue; //DO NOT USE @ RUN WITH(Karate.class) //@RunWith(Karate.class) @KarateOptions(features = "classpath:gitAutomation/deleteRepoDynamicOutline.feature") public class gitRunnerCucumberReport { @Test public void testParallel() { //System.setProperty("karate.env", "demo"); // ensure reset if other tests (e.g. mock) had set env in CI // Specify your feature here Results results = Runner.path("classpath:gitAutomation").tags("~@ignore").parallel(5); generateReport(results.getReportDir()); assertTrue(results.getErrorMessages(), results.getFailCount() == 0); } // Generate report method to generate the report 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())); //Specify the folder where to want to generate the feature file Configuration config = new Configuration(new File("target"), "gitAutomation"); ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config); reportBuilder.generateReports(); } }
You can run the above runner class and the report will get generated in your specified folder.
