Chapter 8: TestNG setup and Annotations

What is TestNG?

It is an open source automated testing framework; where NG stands for Next Generation. TestNG made the choice to use annotations to annotate test classes.

It is a testing framework which has been inspired by JUnit which uses annotations(@). It is a more powerful testing framework than JUnit as it contains more powerful functionalities such as:

  1. It provides flexible test configuration.
  2. It also supports the parameters.
  3. It supports Data-driven testing with the help of @DataProvider.
  4. TestNG annotations.
  5. It supports multithreaded execution.
  6. It is supported by various tools and plug-ins such as Eclipse, Maven, IDEA, etc.

By using TestNG you can also generate proper report and easily come to know how many test cases are passed,failed and skipped.also we can execute failed test case separately.

Advantages of TestNG over Junit

  • In TestNG, annotations are easier to understand than Junit.
  • It produces the HTML reports for implementation.
  • It also generates the Logs.
  • In TestNG, there is no constraint available such as @beforeclass and @afterclass which is present in Junit.
  • TestNG enables you to group the test cases easily which is not possible in Junit.
  • TestNG supports three additional levels such as @Before/After suite, @Before/AfterTest, and Before/AfterGroup.
  • TestNG does not extend any class. TestNG framework allows you to define the test cases where each test case is independent of other test cases.
  • It allows you to run the test cases of a particular group. Let’s consider a scenario where we have created two groups such as ‘Smoke’ and ‘Regression’. If you want to execute the test cases in a ‘Regression’ group, then this can only be possible in the TestNG framework.
  • Parallel execution of test cases, i.e., running multiple test cases is only possible in the TestNG framework.

In this chapter we will learn about how to create TestNG @test annotations,how to convert these classes into testing suite file and execute through eclipse.

TestNG Annotations:

  • @BeforeSuite: This annotated method will be run before all tests in this suite have run.
  • @AfterSuite: This annotated method will be run after all tests in this suite have run.
  • @BeforeTest: This annotated method will be run before any test method belonging to the classes inside the tag is run.
  • @AfterTest: This 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: This annotated method will be run before the first test method in the current class is invoked.
  • @AfterClass: This annotated method will be run after all the test methods in the current class have been run.
  • @BeforeMethod: This annotated method will be run before each test method.
  • @AfterMethod: This annotated method will be run after each test method.
  • @Test: The annotated method is a part of a test case.

  Webdriver Example:

package TestNG;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;

public class Annotations Demo 
    {
  @BeforeMethod
  public void bMethod()
  {
	  System.out.println("before each test case i will come");
   }

  @AfterMethod
  public void aMethod() 
  {
	  System.out.println("after each test case i will come");
  }

  @BeforeClass
  public void cbmethod()
  {
	  System.out.println("execute before test2");
   }
  @AfterClass
  public void aclass()
  {
	  System.out.println("execute after test2");
   }

@Test(priority=3)
public void test1() 
{
	System.out.println("inside test1");
}
@Test(priority=1)
public void test2()
{
	System.out.println("inside test2");
}
@Test(priority=2)
public void test3()
{
	System.out.println("inside test3");
}
}