Introduction
Running test cases in parallel in Cucumber with TestNG helps speed up the execution process by running multiple tests at the same time. For those working with Cucumber, a popular Behavior-Driven Development (BDD) testing framework, executing tests in parallel can significantly optimize the testing process. In this tutorial, I will explain Parallel Testing using Cucumber with TestNG
Before moving forward, make sure you're familiar with configuring the @CucumberOptions annotation. You can refer to my earlier blog for a step-by-step guide
Steps to Run Cucumber Tests in Parallel with TestNG
1. Set Up a Maven Project
✓Create a Maven project in your IDE/Intelji
✓Add the necessary dependencies for Cucumber, TestNG, and Maven Surefire plugin in your pom.xml
<dependencies>
<!-- Cucumber dependencies -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.0.0</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
2. Create Feature Files
Create your .feature files inside the src/test/resources/ folder, where you describe the test scenarios.
Feature: parallel execution
Feature: Sample test for parallel execution
Scenario: Test Scenario 1
Given I am on the homepage
Scenario: Test Scenario 2
Given I am on the login page
3. Step Definitions
Write step definitions for the feature file in the src/test/java/ folder.
package parallelRun;
import io.cucumber.java.en.Given;
public class StepDefinitions {
@Given("^I am on the homepage$")
public void iAmOnTheHomePage() {
System.out.println("On homepage");
}
@Given("^I am on the login page$")
public void iAmOnTheLoginPage() {
System.out.println("On login page");
}
}
4. Cucumber Test Runner
Add a cucumber runner by extending the AbstractTestNGCucumberTests class and overriding the scenarios method. Set the parallel option value to true for the DataProvider annotation.
DataProvider (parallel = true): This makes sure that the tests are executed in parallel.
package parallelRun;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import org.testng.annotations.DataProvider;
public class RunCucumberTest extends AbstractTestNGCucumberTests {
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios(
);
}
}
5.Add Maven Surefire Plugin for Parallel Execution
In the pom.xml, configure the Maven Surefire Plugin for parallel execution by specifying the number of threads.
By default, the DataProvider in TestNG runs 10 threads in parallel. You can adjust this by specifying thed ataproviderthreadcount
Maven Surefire Plugin: Manages the parallel execution configuration.
Thread Count: You can adjust the number of threads depending on how many tests you want to run in parallel.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<parallel>methods</parallel>
<threadCount>4</threadCount>
</configuration>
</plugin>
</plugins>
</build>
6. Run the Tests
This will run the test scenarios in parallel, with the number of threads specified in the thread-count
Conclusion
Parallel testing in a Cucumber project using TestNG and Maven helps speed up your testing process and reduce execution time. By running tests in parallel, you can optimize resources and get faster feedback, making it ideal for larger test suites. This approach improves efficiency and ensures quicker test execution, enhancing the overall quality of your software.
Comments
Post a Comment