Reputation: 2780
I'm implementing Cucumber Testng for learning purposes. I've realized that the @After hook method is executed twice. I can confirm it with debugging set and a test report output. I only run 1 feature file and my test report shows two entries with identical class names.
Does anyone know why?
Code:
@After
public void tearDown(Scenario scen) throws IOException {
ExtentTest logger = reportMgr.getLogger();
String feature = getClass().getName() + " Feature"; //+ "." + Thread.currentThread().getStackTrace()[1].getMethodName().toString();
logger = reportMgr.getExtent().createTest(feature);
String screenShot = CaptureScreenshot.captureScreen(WebDriverManager.driver, CaptureScreenshot.generateFileName(feature));
if (!scen.isFailed()) {
logger.pass("Pass");
logger.addScreenCaptureFromPath(screenShot);
} else {
logger.fail("Fail");
logger.addScreenCaptureFromPath(screenShot);
}
}
Feature File:
@Login_Valid Feature: Login to Volare Collector Description: As a user, I want login to Volare Collector
Scenario: Valid Login
Given Volare Collector Home Page opens in browser
When I login to Volare Collector with Username and Password
Then Page navigate to Volare Collector Home Page
There is warning in the feature file called Multiple Definitions "Volare Collector Home Page opens in browser".
Please download my source code from this link.
Upvotes: 6
Views: 1249
Reputation: 9058
testng.xml
calls the TestRunner
class. In the cucumberoptions
there is no tags option
, therefore no filtering based on tags. Thus it will pick up all the feature files in the folder given in features
option - "src/Features"
ie. Login.feature and Logout.feature.
Each of the feature files has one scenario each. Therefore there are 2 scenarios in total and thus 2 tests will be run. This explains why you see 2 tests in the report.
When each scenario runs, they will run any present Before and After hooks
. So for 2 scenarios, hooks will be run twice.
Also hooks are global in nature ie, as long they are present in any classes in the option defined in glue(glue = {"Step_Definitions"})
they will be picked up. In your case you have two Before hooks in Login and Logout classes, you may want to look at fixing that. Use a Before hook and pass it a tag filter in the value option.
In the single After hook defined in the Login
class this piece of code
String feature = getClass().getName();
logger = reportMgr.getExtent().createTest(feature);
This means that name of the test will always be the full class name ie. Step_Definitions.Login
. Therefore it shows up as 2 tests with same names although they are different.
Upvotes: 3
Reputation: 439
I assume that you are using TestNG 6.8 or higher
Use the following Test class template and check if you have the issue
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
class PlaygroundTest {
@BeforeClass
public void setUp(){
}
@AfterClass
public void tearDown() {
}
@Test
public void test(){
}
}
Upvotes: 0