Friday 5 February 2016

Selenium: Annotations in testNG framework

Annotations in testNG framework :
===========================

TestNG is a testing framework.

It is an open source automated testing framework; where NG of TestNG means Next Generation. TestNG is similar to JUnit but it is much more powerful than JUnit.

Advantages of TestNG:
======================
Generates different kinds of HTML reports after execution of test cases
Annotations made testers life easy
Test cases can be Grouped & Prioritized more easily
Parallel testing is possible
Data Parameterization is possible through @data provider annotation.

Annotations in TestNG:
======================
@BeforeSuite: The annotated method will be run before all tests in this suite have run.

@AfterSuite: The annotated method will be run after all tests in this suite have run.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.

@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

@BeforeMethod: The annotated method will be run before each test method.

@AfterMethod: The annotated method will be run after each test method.


@Test: The annotated method is a part of a test case.

//Sample example on testng annotations:
---------------------------------------------------
package testngdemo;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestngAnnotationsDemo {

@Test
 
public void testCase1() {

System.out.println("This is the Test Case 1");

}

@Test

public void testCase2() {

System.out.println("This is the Test Case 2");

}

@BeforeMethod

public void beforeMethod() {

System.out.println("This will execute before every Method");

}

@AfterMethod

public void afterMethod() {

System.out.println("This will execute after every Method");

}

@BeforeClass

public void beforeClass() {

System.out.println("This will execute before the Class");

}

@AfterClass

public void afterClass() {

System.out.println("This will execute after the Class");

}

@BeforeTest

public void beforeTest() {

System.out.println("This will execute before the Test");

}

@AfterTest

public void afterTest() {

System.out.println("This will execute after the Test");

}

@BeforeSuite

public void beforeSuite() {

System.out.println("This will execute before the Test Suite");

}

@AfterSuite

public void afterSuite() {

System.out.println("This will execute after the Test Suite");

}
}


















No comments:

Post a Comment