Overview
In Cucumber, there are no built-in annotations specifically named @BeforeAll
and @AfterAll
like in JUnit or TestNG. before the cucumber 7 version. But now Cucumber introduces these two very demanding hooks so that we don’t need to use extra tools like TestNg to implement BeforeSuite and AfterSuite.
Let us take an example of how we can use those hooks.
@BeforeAll
- This will run before all the scenarios.
- Setting up test environment: You can use
@BeforeAll
to perform setup tasks such as starting services or servers, initializing test data, or establishing connections to external systems. These operations are typically resource-intensive and need to be executed only once before running any tests. - Preparing test data:
@BeforeAll
can be used to create or load test data that is shared among multiple tests. This ensures that the data is available and ready before executing any tests. - Configuration initialization: To set up and configure global settings or properties that apply to all tests. For example, initializing the WebDriver or configuring logging frameworks.
- Initialize DB connection
@AfterAll
- This will run After all the scenarios.
- Cleanup operations:
@AfterAll
is suitable for performing cleanup tasks after all tests have finished executing. This can include stopping services or servers, releasing resources, or cleaning up temporary files. - Reporting or result aggregation:
@AfterAll
can be used to generate test reports, aggregate test results, or perform any post-test analysis or processing.
BeforeAll And AfterAll Classes
Example Of Hook Class
Write Demo Feature File and Definitions File
Feature: This is test feature to see working of Cucumber Hooks
Scenario: Scenario-1
Given given step
When when step
Then Then step
Scenario: Scenario-2
Given given step two
When when step two
Then Then step two
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HooksDemoDefs {
private static final Logger LOGGER = LogManager.getLogger(HooksDemoDefs.class);
@Given("given step")
public void givenStep() {
LOGGER.info("given step");
}
@When("when step")
public void whenStep() {
LOGGER.info("when step");
}
@Then("Then step")
public void thenStep() {
LOGGER.info("Then step");
}
@Given("given step two")
public void givenStepTwo() {
LOGGER.info("given step two");
}
@When("when step two")
public void whenStepTwo() {
LOGGER.info("when step two");
}
@Then("Then step two")
public void thenStepTwo() {
LOGGER.info("when step two");
}
}
Execution Logs
Testing started at 7:12 pm ...
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run before all scenarios.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run before every scenario.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run before every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] HooksDemoDefs - given step
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run after every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run before every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] HooksDemoDefs - when step
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run after every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run before every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] HooksDemoDefs - Then step
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run after every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run after every scenario.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run before every scenario.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run before every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] HooksDemoDefs - given step two
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run after every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run before every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] HooksDemoDefs - when step two
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run after every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run before every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] HooksDemoDefs - when step two
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run after every scenario step.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run after every scenario.
[INFO ] 2023-07-08 19:12:46 [main] Hooks - I will run after all scenarios.
2 Scenarios (2 passed)
6 Steps (6 passed)
0m1.216s
Conclusion
In summary, @BeforeAll
and @AfterAll
annotations are suitable for operations that need to be performed once before and after all tests in a test automation framework. They help ensure efficient test setup, teardown, and resource management.
Reference: Cucumber Hooks