In a testing.xml file, when executed with and tag, it has different behavior in execution @Before and @After annotations and kindly confirm if this is how it is designed.
I have a below BaseClass file with annotation defined for @BeforeSuite, @AfterSuite, @BeforeClass, @AfterClass, and with respect to groups = {"Windows","Android"} we have defined @BeforeGroups and @AfterGroups
public class BaseClass {
@BeforeSuite
public void beforeSuite() {
System.out.println("----@BeforeSuite function");
}
@AfterSuite
public void afterSuite() {
System.out.println("----@AfterSuite function");
}
@BeforeClass
public void beforeClass() { System.out.println("----------------@BeforeClass function"); }
@AfterClass
public void afterClass() { System.out.println("----------------@AfterClass function"); }
@BeforeGroups(alwaysRun = true, groups = {"Windows","Android"})
public void beforeGroups() { System.out.println("----------------@BeforeGroups function"); }
@AfterGroups(alwaysRun = true, groups = {"Windows","Android"})
public void afterGroups() { System.out.println("----------------@AfterGroups function"); }
}
I have defined a PArameterSample class file which exteds the above BaseClass.
public class ParameterSample extends BaseClass {
@Test(groups={"Windows"})
public void simpleTest() {
System.out.println("------------------------@Inside ParameterSample: simpleTest");
}
@Test(groups={"Android"})
public void sample() {
System.out.println("------------------------@Inside ParameterSample: sample");
}
}
None of the annotation @BeforeSuite, @AfterSuite, @BeforeClass, @AfterClass has (alwaysRun = true) defined in the BaseClass file.
Below is the testng.xml which defines the groups tag to execute only "Windows" group with tag.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Regression1">
<groups>
<run>
<include name="Windows"></include>
</run>
</groups>
<classes>
<class name="sample.ParameterSample"/>
</classes>
</test>
</suite>
As we have not defined (alwaysRun=true) for @BeforeSuite, @AfterSuite, @BeforeClass, @AfterClass annotations, the o/p is inline as expected.
----------------@BeforeGroups function
------------------------@Inside ParameterSample: simpleTest
----------------@AfterGroups function
It executed only @BeforeGroups and @AfterGroups annotation and it has not executed any of the other annotations.
Now below XML has tag instead of tag which output is what I need clarification.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Regression1">
<groups>
<run>
<exclude name="Windows"></exclude>
</run>
</groups>
<classes>
<class name="sample.ParameterSample"/>
</classes>
</test>
</suite>
Below is the output of the XML.
----@BeforeSuite function
----------------@BeforeClass function
----------------@BeforeGroups function
------------------------@Inside ParameterSample: sample
----------------@AfterGroups function
----------------@AfterClass function
----@AfterSuite function
In the XML we have given which is executing @BeforeSuite, @AfterSuite, @BeforeClass, @AfterClass annotations also even when (alwaysRun=true) is not defined.
Is the behavior correct for tag, that it executes @BeforeSuite, @AfterSuite, @BeforeClass, @AfterClass class by default and only for tag we need to define (alwaysRun=true) in other annotations if those annotations need to be executed.
Yes, the behavior you described is correct. The
<run>tag in TestNG executes all annotations in the class, even if they are not annotated withalwaysRun=true. The<exclude>tag excludes the specified groups from execution but does not exclude the annotations themselves.In your example, the
<run>tag in the first XML file includes the "Windows" group, so the@BeforeGroupsand@AfterGroupsannotations in theBaseClassare executed, even though they are not annotated withalwaysRun=true. The<exclude>tag in the second XML file excludes the "Windows" group, so the@BeforeGroupsand@AfterGroupsannotations are not executed, but the@BeforeClass,@AfterClass,@BeforeSuite, and@AfterSuiteannotations are still executed because they are not excluded by the tag.To summarise:
<run>- All annotations in the class unless they are excluded by a<exclude>tag.<exclude>- Annotations in the specified groups are not executed. Other annotations are still executed.