TestNG Introduction

TestNG is a unit test framework designed for testing needs (developers / Test Engineers). It is inspired from JUnit by adding new functionalities which made TestNG more powerful than other unit test frameworks.

We can use TestNG for the below reasons:
1. To Run test cases in the order which we provide
2. We can run classes without using Main method.
3. To generate reports
4. We can Parameterize Tests
5. Group tests and execute only specific group
6. Implement logging using TestNG Listeners

In Eclipse, it is very easy to Install TestNG Software, In the latest version, you will find an option to install software / Market place. Simple search for TestNG which will display a link to install. Click on Install. Installation will get completed in a short time.
Click here to install TestNG in Eclipse

The below are the different annotations available in TestNG

@BeforeSuite: This annotation method will execute before all tests in this suite

@AfterSuite:This annotation method will execute after all tests in this suite

@BeforeTest: This annotation method will execute before any test method belonging to the classes inside the Test tag is executed.

@AfterTest: This annotation method will execute after all the test methods belonging to the classes inside the Test tag have executed.

@BeforeGroups: The list of groups that this configuration method will execute before. This method is guaranteed to execute 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 execute after. This method is guaranteed to execute shortly after the last test method that belongs to any of these groups is invoked.

BeforeClass: This annotation method will execute before the first test method in the current class is invoked.

@AfterClass: This annotation method will execute after all the test methods in the current class have been executed.

@BeforeMethod: This annotation method will execute before each test method.

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

Below is the basic Example program using the above annotations:

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TesNGExample {

@BeforeTest
public void openDBConnection(){
	System.out.println("I will execute Before any Test method/case executes");
}

@AfterTest
public void CloseDBConnection(){
	System.out.println("I will execute After all Test method/case executes");
}

@Test
public void testCase01(){
	System.out.println("Im in first testcase");
}

@Test
public void testCase02(){
	System.out.println("Im in second testcase");
}

@Test
public void testCase03(){
	System.out.println("Im in third testcase");
}

@BeforeMethod
public void openBrowser(){
	System.out.println("I will be executed before Each Test Method");
}

@AfterMethod
public void CloseBrowser(){
	System.out.println("I will be executed After Each Test Method");
}
}

Below is the Output :

I will execute Before any Test method/case executes

I will be executed before Each Test Method
Im in first testcase
I will be executed After Each Test Method

I will be executed before Each Test Method
Im in second testcase
I will be executed After Each Test Method

I will be executed before Each Test Method
Im in third testcase
I will be executed After Each Test Method

I will execute After all Test method/case executes

PASSED: testCase01
PASSED: testCase02
PASSED: testCase03

Test Frameworks: 

Comments

Thanks guyzz...small and well said..thank u ..:)

Precise..thnks

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.