I am running a test suite with the test methods belonging to a certain groups.
Below is the Selenium code:
public class BaseClass
{
@BeforeMethod(onlyForGroups = {"P1"})
public void bmeth1()
{
System.out.println("Before Method1 called");
}
@BeforeMethod(onlyForGroups = {"P2"})
public void bmeth2()
{
System.out.println("Before Method2 called");
}
@BeforeMethod(onlyForGroups = {"P3"})
public void bmeth3()
{
System.out.println("Before Method3 called");
}
@AfterMethod(onlyForGroups = {"P1"})
public void ameth1()
{
System.out.println("After Method1 called");
}
@AfterMethod(onlyForGroups = {"P2"})
public void ameth2()
{
System.out.println("After Method2 called");
}
@AfterMethod(onlyForGroups = {"P3"})
public void ameth3()
{
System.out.println("After Method3 called");
}
}
public class TC_003 extends BaseClass
{
@Test(groups = {"P1"})
public void tCase6()
{
System.out.println("Inside testcase 6");
}
@Test(groups = {"P2"})
public void tCase7()
{
System.out.println("Inside testcase 7");
}
@Test(groups = {"P3"})
public void tCase8()
{
System.out.println("Inside testcase 8");
}
}
Below is the testng.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="10">
<test name="Test1">
<groups>
<run>
<include name=".*"/>
</run>
</groups>
<classes>
<class name="testing.TC_003"/>
</classes>
</test>
</suite>
Actual Output:
Inside testcase 6
Inside testcase 7
Inside testcase 8
Expected Output:
Before Method1 called
Inside testcase 6
After Method1 called
Before Method2 called
Inside testcase 7
After Method2 called
Before Method3 called
Inside testcase 8
After Method3 called
The test methods are executed, but the @BeforeMethod/@AfterMethod did not execute. This issue occurs only if we include certain groups in testng.xml file. But if we exclude certain groups or do not use groups tag in testng.xml file, then they are executed.
As suggested here, the current workaround is to use alwaysRun=true flag along with onlyForGroups flag. But if we apply this workaround, and if there is any SkipException in the preceding/parent config methods, then it is forced to execute the @BeforeMethod/@AfterMethod methods, even when the test method is going to be skipped. There is a similar issue logged here, when the preceding/parent config methods has failed.