Gradle, TestNG, Cucumber-jvm and paralleling scenarios
Here is a problem!
Cucumber-jvm with jUnit-4 runner can parallel tests. FYI current version of cucumber-jvm is 4.3.0
But! But!
- You can only parallel features and can’t parallel scenarios. This it not efficient.
- Paralleling will be done via processes instead of threads. So you can’t share state between forked jvm’s so there will be many problems with common system resources. For example you will have problems with binding ports or with creating file etc.
So what can we do about it?
We can use TestNG
But!
Gradle didn’t find any tests to run. Even when you specify the filter.
./gradlew clean test --tests "space.sirdir.CucumberRunner"
And in the end you get FAILURE
> Task :test FAILEDFAILURE: Build failed with an exception.* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [space.sirdir.CucumberRunner](--tests filter)
Fix of that problem.
Simple as hell. And it is one single line of code in gradle test{} task.
scanForTestClasses = false
Your test closure will be look like this
#gradle.build filetest {
useTestNG()
scanForTestClasses = false
testLogging.showStandardStreams = true
}
Customize thread count
Cucumber runner use testng dataprovider to parallel scenarios. So we need to pass to jvm -Ddataproviderthreadcount=3 to run tests in 3 threads.
- In gradle.build file add jvmArgs to test task
#gradle.build filetest {
useTestNG()
jvmArgs(["-Ddataproviderthreadcount=${threads}"])
scanForTestClasses = false
testLogging.showStandardStreams = true
}
2. Create gradle.properties file to store there default value
#gradle.properties filethreads = 3
3. To use custom count of threads run in terminal with -Pthreads=value
./gradlew clean test -Pthreads=4
P.S.
Cucumber team planning to move to jUnit5 in Cucumber-jvm 5.0 library but it will not done really soon. And they have examples for TestNG only for maven. So it take some time to investigate.