Recently my team and I tried to enable parallel test execution for our Cucumber framework. We are using Cucumber 4, JUnit 4 and Gradle and It does not have built-in parallel mechanism.
Gradle does not support parallel execution below class level. So with Gradle and JUnit 4 we can not run Cucumber in parallel.
With Gradle and JUnit 5 this is not a problem as JUnit 5 has new built-in parallel mechanism but we have to use Cucumber JUnit Engine to enable it. That’s why I decided to upgrade framework dependencies version.
I firstly tried to use Cucumber 6 and JUnit5 here. But then I realised Cucumber 7 brings more new cool features.
What is newly introduced in Cucumber 7 ?
EXECUTION_MODE_FEATURE_PROPERTY_NAME
Property name used to set the executing thread for all scenarios and examples in a feature: “cucumber.execution.execution-mode.feature”
Valid values are same_thread or concurrent. Default value is concurrent.
When parallel execution is enabled, scenarios are executed in parallel on any available thread. setting this property to same_thread executes scenarios sequentially in the same thread as the parent feature.
Before-All and After-All Hooks
The Cucumber-Java module now supports before-all and after-all hooks.
The hooks have ‘invoke around’ semantics. @BeforeAll hooks will be executed before any scenario is executed. After all scenarios have been executed @AfterAll hooks will be executed.
You can find more details all properties of Cucumber 7 here: https://javadoc.io/doc/io.cucumber/cucumber-junit-platform-engine/7.11.1/io/cucumber/junit/platform/engine/Constants.html
These properties are Cucumber specific properties as well as overwriting JUnit 5 properties for parallel execution. In JUnit5 documentation, it explains very good about these parallel execution properties: https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution
Here is the complete framework. those tests in this fw are against real web application Unsplash.
Automation Framework using Cucumber JVM7 + JUnit5 + Selenide + Allure + logback + Gradle
A sample BDD Automation framework using Cucumber7, JUnit 5, Selenide, Allure, Logback, Gradle.
Support running tests in parallel, sharing data among test steps, manage Objects using Spring Dependency Injection and clean up created data using API.
Libraries Used
- Cucumber – 7.11.1
- Junit – 5.9.2
- Spring Boot – 2.6.14
- Gradle – 7.4.2
- Selenide – 5.24.2
- Selenide wiki
- Allure – 2.19.0
- Logback – 1.2.11
- AssertJ – 3.20.2
Reference Links:
Run/Debug tests
- run all tests in parallel, number of threads can be specified in RunCucumberTests.class, junit-platform.properties or command line:
- cucumber.execution.parallel.enabled: set to true to enable parallel execution
- cucumber.execution.parallel.config.fixed.parallelism: integer, set number of threads
- cucumber.execution.execution-mode.feature:
- concurrent (scenarios are executed in parallel on any available thread)
- same_thread (executes scenarios sequentially in the same thread as the parent feature)
- run all tests in
firefoxbrowser
$ gradlew -Dselenide.browser=firefox clean test --info
- run tests in Selenium Grid
- enable parallel execution properties in junit-platform.properties
- set these 2 properties in selenide.properties
- selenide.remote.execution = true
- selenide.remote = http://localhost:4444/wd/hub (Grid hub Url)
- Debug test on IntelliJ IDE. Run Debug RunCucumberTests.java as JUnit Test
After the tests are ran, you can see:
- logs from file appender under
build/logs/log.log - Allure results
build/allure-results
ScenarioContext
Represents test context to save/get test data and share it among test steps in one Cucumber scenario.
Example: scenarioContext.setContext(DataItem.COLLECTION_NAME, collectionName); String collectionName = (String) scenarioContext.getContext(DataItem.COLLECTION_NAME);
DataStorage
Represents a storage to store test data and handle clean up after each Cucumber scenario.
Example: dataStorage.setCollectionNames(collectionName);
Allure reports
Allure report will contain framework logs, Selenide browser interaction logs, screenshots and page sources for failing test cases
- Allure CLI should be installed
- Allure results stored in
build/allure-results - To generate allure report, first navigate to unsplash directory then run command:$ allure serve
Logback configuration
You can find logback configuration here src/test/resources/logback-test.xml
Current configuration contains two appenders: (default log level: INFO)
- ConsoleAppender will output logs to system out stream
- FileAppender will output logs to
build/logs/log.log