Search code examples
javacucumbertestngmaven-surefire-plugintestng-dataprovider

Unable to control parallel execution for cucumber


I am unable to control the treads from dataproviderthreadcount. For example if we have 4 scenario and I run my script, it execute all 4 scenario's parallelly. Doesn't matter what dataproviderthreadcount value I gave in the pom for maven-surfire.

Below are the snapshots.

POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>testParallerRun</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <cucumbertestng.version>5.6.0</cucumbertestng.version>
        <cucumberjava.version>5.6.0</cucumberjava.version>
        <mvncompiler.version>3.8.1</mvncompiler.version>
        <javarelease.version>11</javarelease.version>
        <mvnsurefire.version>3.0.0-M5</mvnsurefire.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>${cucumbertestng.version}</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumberjava.version}</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${mvncompiler.version}</version>
                    <configuration>
                        <release>${javarelease.version}</release>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${mvnsurefire.version}</version>
                    <configuration>
                        <properties>
                            <property>
                                <name>dataproviderthreadcount</name>
                                <value>2</value>
                            </property>
                        </properties>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Runner Class:

package com.test.runner;


import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;


@CucumberOptions(
        plugin = {
                "pretty",
                "json:target/reports/json/result.json",
                "html:target/reports/html/result.html"},
        strict = true,
        features = {"src/test/resources/"},
        glue = {"com.test.stepdef"},
        tags = {""}
)

public class TestRunner extends AbstractTestNGCucumberTests {
    @DataProvider(parallel = true)
    @Override
    public Object[][] scenarios() {
        return super.scenarios();
    }
}

Feature 1:

Feature: test 1

  Scenario: 01
    Given Print "01"

  Scenario: 02
    Given Print "02"

  Scenario: 03
    Given Print "03"

  Scenario: 04
    Given Print "04"

Please let me if anyone know how to control the number of threads rather than let Testng decide.


Solution

  • There's no problem with TestNG per se here.

    The dataproviderthreadcount attribute that you have set in your pom file is going to be applicable and relevant only when you are running your test via mvn clean test.

    If you are trying to run this test class from within the IDE (IntelliJ or Eclipse for that matter) its not going take affect.

    Here's a test class that I created which is powered by a data provider ( For the sake of simplicity I have intentionally kept cucumber out of the equation )

    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;
    
    public class AppTest {
    
      @Test(dataProvider = "dp")
      public void shouldAnswerWithTrue(int i) {
        System.err.println("Running on [" + Thread.currentThread().getId() + "]");
    
      }
    
      @DataProvider(name = "dp", parallel = true)
      public Object[][] testData() {
        return new Object[][]{
            {1},
            {2},
            {3},
            {4},
            {5},
            {6}
        };
      }
    }
    

    Here's the command line output when I run mvn clean test

    [INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ testng_playground ---
    [INFO] 
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running com.rationaleemotions.AppTest
    Running on [15]
    Running on [14]
    Running on [15]
    Running on [14]
    Running on [14]
    Running on [15]
    [INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.62 s - in com.rationaleemotions.AppTest
    [INFO] 
    [INFO] Results:
    [INFO] 
    [INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0