TestNG Introduction



TestNG Introduction:

TestNG is a unit test framework designed for testing needs. 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 following 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.

The below are the different annotations available in TestNG

TestNG Annotations:

@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 testCase01");
}

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

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

@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 testCase01
I will be executed After Each Test Method

I will be executed before Each Test Method
Im in testCase02
I will be executed After Each Test Method

I will be executed before Each Test Method
Im in testCase03
I will be executed After Each Test Method

I will execute After all Test method/case executes

PASSED: testCase01
PASSED: testCase02
PASSED: testCase03

Tags: ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18