In our previous article we had learned about TestNG and how can we add packages, classes and methods to test. In this article we will focus on TestNG annotations and different features provided by them. Annotations are used to add metadata(data about data) to the existing Java source code. Annotations can be applied for classes, methods, variables and parameters which allows us to add extra information about data objects in our source code. There are certain predefine set of annotations in Java.For example, @Overide, @After, @Before, @Deprecated, @SupressWarning and so on.There are certain Annotations that TestNG has over JUnit4 one of them is extra Before and After annotations such as Before/After Suite and Before/After Group.
Before and After annotations are used to basically set up some variables or configuration before the start of a test execution and then to cleanup any of these things after the test execution ends. TestNG provides five different kinds of Before and After annotation options, each of which can be used depending upon the test requirements. The following are the different before and after options provided by TestNG:
1. @BeforeSuite/@AfterSuite BeforeSuite: Method annotated with BeforeSuite will be executed before starting of any test in the suite AfterSuite: Method annotated with AfterSuite gets executed after execution all tests in the suite
2. @BeforeTest/@AfterTest BeforeTest: Method annotated with BeforeTest gets executed before the first test-method mentioned in each test inside the 'test' tag in test suite. AfterTest: Method annotated with afterTest will be executed after execution of all tests inside the 'test' tag in test suite.
3. @BeforeGroups/@AfterGroups BeforeGroup: Method annotated with BeforeGroup gets executed before executing any of the tests belonging to the group as mentioned in the 'groups' attribute. AfterGroup: Method annotated with BeforeGroup will be executed after execution of all tests belonging to the group as mentioned in the 'groups' attribute
4. @BeforeClass/@AfterClass BeforeClass: Method annotated with BeforeClass gets executed before executing any of the tests belonging to the specific class. AfterClass: Method annotated with AfterClass will be executed after execution of all tests belonging to the specific class.
5. @BeforeMethod/@AfterMethod BeforeMethod : Method annotated with BeforeMethod gets executed before each test method. AfterMethod: Method annotated with AfterMethod will be executed after execution of each test method.
we will need a sample project for defining test suites for execution.
Sample Project: a. Open Eclipse. b. create a project MutiTest . c. In MultiTest create a package firstPackage. d. In firstPackage create a class FirstTestClass.java
Add the following piece of code in FirstTestClass.java:
public class FirstTestClass {
//@BeforeSuite/@AfterSuite
@BeforeSuite
public void beforeSuite(){
System.out.println("Before Suite method");
}
@AfterSuite
public void afterSuite(){
System.out.println("After Suite method");
}
//@BeforeTest/@AfterTest
@BeforeTest
public void beforeTest(){
System.out.println("Before Test method");
}
@AfterTest
public void afterTest(){
System.out.println("After Test method");
}
//@BeforeTest/@AfterTest
@BeforeGroups(groups={"groupTest"})
public void beforeGroup(){
System.out.println("Before Group Test method");
}
@AfterGroups(groups={"groupTest"})
public void afterGroup(){
System.out.println("After Group Test method");
}
//@BeforeClass/@AfterClass
@BeforeClass
public void beforeClass(){
System.out.println("Before Class method");
}
@AfterClass
public void afterClass(){
System.out.println("After Class method");
}
//@BeforeMethod/@AfterMethod
@BeforeMethod
public void beforeMethod(){
System.out.println("Before Method");
}
@AfterMethod
public void afterMethod(){
System.out.println("After Method");
}
@Test(groups={"groupTest"})
public void testMethod(){
System.out.println("Test one method");
}
}
In order to test it add the following content to the testng.xml file:
<suite name="First Suite" verbose="1" >
<test name="First Test" >
<classes>
<class name="MultiTest.firstPackge.FirstTestClass" >
<methods>
<include name="testMethod"/>
</methods>
</class>
</classes>
</test>
</suite>
Execute the preceding testng.xml file as a TestNG suite.
Output:
Before Suite method
Before Test method
Before Class method
Before Group Test method
Before Method
Test one method
After Method
After Group Test method
After Class method
After Test method
After Suite method
From above example we have successfully created a test class with all kinds of Before and After annotations and executed it using a testng.xml. This also explains the sequence in which all annotations will be executed. I look forward to questions and suggestions in the comments section.
Comments