06 Parallel Run - biswajitsundara/Karate GitHub Wiki

In this section we will look into it three things

  • Parallel Run
  • Cucumber Reporting
  • Logging

Add dependency for cucumber reporting

Add below maven dependency in pom.xml file

<!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
<dependency>
    <groupId>net.masterthought</groupId>
    <artifactId>cucumber-reporting</artifactId>
    <version>4.4.0</version>
</dependency>

Add commons-io dependency for file copying

Add below maven dependency in pom.xml file

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

Add logging configuration

Add logback-test.xml under src\test\java

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
  
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>target/karate.log</file>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>    
   
    <logger name="com.intuit.karate" level="DEBUG"/>
   
    <root level="info">
         <!-- <appender-ref ref="STDOUT" /> -->
        <appender-ref ref="FILE" />
    </root>
  
</configuration>

Create BasicFeature1.feature

Feature: User details
  
  Background:
      * url 'https://reqres.in'

  Scenario: Get call test
    Given path '/api/users/2'
    When method GET
    Then status=200
    Then print response

Create BasicFeature2.feature

Feature: User details
  
  Background:
      * url 'https://reqres.in'

  Scenario: Get call test
    Given path '/api/users/3'
    When method GET
    Then status=200
    Then print response

Create Test Runner (ParallelTestRunner.java)

package testscripts.parallel;
import static org.junit.Assert.assertTrue;
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.Test;
import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;


// important: do not use @RunWith(Karate.class)
public class ParallelTestRunner {

    
    @Test
    public void testParallel() {
        //System.setProperty("karate.env", "demo"); // we are not using this so commented out
        //Results results = Runner.path("classpath:demo").tags("~@ignore").parallel(5);   
        Results results = Runner.parallel(getClass(),5);   //use either this line or the above line
        generateReport(results.getReportDir());
        assertTrue(results.getErrorMessages(), results.getFailCount() == 0);        
    }
    
    public static void generateReport(String karateOutputPath) {        
        Collection jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[] {"json"}, true);
        List jsonPaths = new ArrayList(jsonFiles.size());
        jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
        Configuration config = new Configuration(new File("target"), "demo");
        ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
        reportBuilder.generateReports();        
    }
}

Execute Test

  • Run the ParallelTestRunner.java
  • It will create reports at \target\cucumber-html-reports\overview-features.html
  • The karate log will be available at \target\karate.log
  • I have intentionally commented out appender-ref ref="STDOUT" so that it doesn't print all the run time information in console.
  • Lets say we have two scenarios in a feature file and we want to run only one scenario in parallel then add the tag @parallel=false above the scenario that we don't want to run in parallel. The feature files will still run in parallel only this scenario will run in sequential.

Generate only Cucumber Report

Above we see its running in parallel and generating the cucumber report. If we want to generate only the cucumber report and don't bother about parallel run then simply use the below code in the method testParallel()

 @Test
    public void testParallel() {
        generateReport("target/surefire-reports");
    }

Exclude Feature/Scenario

You can exclude feature files/scenarios by applying @ignore tag.

Feature File

@ignore
Feature: User details
  
  Background:
      * url 'https://reqres.in'

  Scenario: Get call test
    Given path '/api/users/2'
    When method GET
    Then status=200
    Then print response

Runner File

@KarateOptions(tags = "~@ignore") 
public class TestRunner {

}

Reference

⚠️ **GitHub.com Fallback** ⚠️