Reputation: 2762
I'm new to Cucumber BDD testing v4.1.
Questions:
Please help. Thanks.
Upvotes: 0
Views: 1766
Reputation: 2762
I got the answers already which is implement ConcurrentEventListener.
Upvotes: 0
Reputation: 2209
Cucumber Hooks – Where to use @Before @Before, in its most basic usage, allows you to run a block of code before every scenario. Normally in Cucumber, we tend to do the initialization related things – such as object initialization, data setup etc in the Given statement. Because of this a lot of people don’t see the need of using Cucumber’s @Before. But you can use @Before to make in an entry in your reporting that a new scenario is being executed. Since @Before always runs before every scenario, you can use this in your reports to clearly depict when a scenario starts executing.
It’s not necessary that you add @Before in each feature file. Just add it in any one feature file and let Cucumber do its job. Cucumber will figure out where you have saved @Before and then it will use it before all the scenarios. To make your reporting bit more useful and easy to understand, you can actually write the scenario name as well in the report. The Java code for this is given below –
@Before
public void before(Scenario scenario) {
System.out.println("------------------------------");
System.out.println("Starting - " + scenario.getName());
System.out.println("------------------------------");
}
Cucumber API provides an Interface called Scenario, using which you can get can instance of this class. In the above code, we have just used the getName() method of this interface to print the scenario name in our log.
Test Hooks with Single Scenario (Example):
Feature File
Feature: Test Hooks
Scenario: This scenario is to test hooks functionality
Given this is the first step
When this is the second step
Then this is the third step
Step Definitions:
package stepDefinition;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class Hooks_Steps {
@Given("^this is the first step$")
public void This_Is_The_First_Step(){
System.out.println("This is the first step");
}
@When("^this is the second step$")
public void This_Is_The_Second_Step(){
System.out.println("This is the second step");
}
@Then("^this is the third step$")
public void This_Is_The_Third_Step(){
System.out.println("This is the third step");
}
}
Hooks
package utilities;
import cucumber.api.java.After;
import cucumber.api.java.Before;
public class Hooks {
@Before
public void beforeScenario(){
System.out.println("This will run before the Scenario");
}
@After
public void afterScenario(){
System.out.println("This will run after the Scenario");
}
}
EDIT:
Workaround mentioned in Issue #515 and elsewhere is to use JUnit‘s @BeforeClass
and @AfterClass
annotations in the runner class, like this:
@RunWith(Cucumber.class)
@Cucumber.Options(format = {
"html:target/cucumber-html-report",
"json-pretty:target/cucumber-json-report.json"})
public class HooksTest {
@BeforeClass
public static void setup() {
System.out.println("Ran the before");
}
@AfterClass
public static void teardown() {
System.out.println("Ran the after");
}
}
Note: While @BeforeClass
and @AfterClass
may look like the cleanest solution at first, they are not very practical to use. They work only when Cucumber-JVM is set to use the JUnit runner. Other runners, like TestNG, the command line runner, and special IDE runners, won’t pick up these hooks. Their methods must also be are static and would need static variables or singletons to share data anyway.
to run JUnit test cases from the command line
Upvotes: 2