ihossain
ihossain

Reputation: 353

Run tests in parallel using Cucumber with Java Selenium with Gradle

I'm using Cucumber with Gradle and would like to run Cucumber features in parallel but can't figure out how. My cucumber executor looks like this:

task cucumber() {
dependsOn assemble, compileTestJava
doLast {
    javaexec {
        main = "cucumber.api.cli.Main"
        classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
        args = ['--plugin', 'pretty',
                '--plugin', 'json:build/reports/cucumber-report.json',
                '--plugin', 'html:build/reports/cucumber-report.html',
                '--glue', 'stepDefinitions',
                'src/test/java']
        systemProperty "cucumber.options", System.getProperty("cucumber.options")
    }
}

}

Thanks, any help is appreciated

Upvotes: 1

Views: 2856

Answers (1)

Sean F
Sean F

Reputation: 198

There is a bit of setup needed to get cucumber running in parallel properly with gradle.

  • Every test has to be threadsafe so you need to be careful with static usage.
  • Multiple drivers need to be made for each thread
  • logging needs to log the correct thread and scenario

I have a skeleton framework that handles all of this that you can use as a reference or build from here

For your specific question in the build.gradle you determine the --threads cucumber option.

Take a look in the build.gradle here

These variables are used to setup parallel runs and determine threads to use in the cucumber options

def availableThreadCount = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
def cucumberThreadsOption = ['--threads', availableThreadCount.toString()]

if runInParallel task is used it puts setParallel to true. We then add the --threads arg into the cucumber options This is where that happens in the cucumber task

Here is where the built cucumber options are used

CreateSharedDrivers.java here is where we handle creating multiple drivers for threads and have the shutdown hook implemented.

In Hooks.java here there is an example of how we log the thread and its current scenario

Upvotes: 2

Related Questions