TestNG Framework

Posted By : Hany Yadav | 30-Jun-2020

testing

Loading...

testng

TestNG is one of the unit testing frameworks for JAVA programming language and it is the successor of the JUnit framework used during automating the application. TestNG is used to write and execute scripts in selenium. It completely depends on annotations and is used to control the flow of execution of test methods. It is the platform that generates HTML reports.

TestNG framework can be easily integrated with the following:-

Eclipse

Ant

Maven

Annotations in TestNG framework:-

@BeforeSuite

@BeforeClass

@BeforeTest

@Test

@AfterTest

@AfterClass

@AfterSuite

@BeforeSuite:-

@BeforeSuite is a TestNG annotation method that will run before all the available methods in-suite start their execution.

@BeforeClass:-

@BeforeClass is a TestNG annotation method that will be invoked/run after the execution of all test methods/functions of the current class.

@BeforeTest:-

It is a method that is a precondition for all test cases mentioned in scripts and is invoked/run for each test case available in the script.

@Test:-

It is used to control functions and is applicable to non-static and non-parameterized functions. In TestNG framework, it is possible to set priority in @test annotation and each function is executed on the basis of priority. It converts the functions into test cases, executes them, and generates the report with the function name.

@AfterClass:-

This annotation will be invoked/run after the execution of all test methods/functions of the current class.

@AfterTest:-

It is a method which is a postcondition for all test cases mentioned in scripts and for each test case written in the script, it will be invoked/run.

@AfterSuite:-

@After suite is a TestNG annotation method that will run after all the available methods in suite complete their execution.

Example:-

import org.testing.annotation.Test;

public class Testng {

// Run once

@BeforeClass

public static void BeforeClass() {

System.out.println("@BeforeClass");

}

// Run once

@AfterClass

public static void AfterClass() {

System.out.println("@AfterClass");

}

// Run before TestMethod

@BeforeTest

public void runBeforeTestMethod() {

System.out.println("@Before");

}

// Run after TestMethod

@AfterTest

public void After() {

System.out.println("@After");

}

@Test(priority=2)

public void test_method_1() {

System.out.println("@Test1");

}

@Test(priority-1)

public void test_method_2() {

System.out.println("@Test2");

}

@Test(priority-3)

public void test_method_3() {

System.out.println("@Test3");

}

}

Outout:-

@BeforeClass

@BeforeTest

@Test2

@AfterTest

@BeforeTest

@Test1

@AfterTest

@BeforeTest

@Test3

@AfterTest

@AfterClass

The most widely used programming language in web and mobile applications, javaScript is a light-weight client side scripting language that our developers hold expertise in. We are a Java development company that enables enterprises to streamline operations and drive returns, using next-gen technologies, with our development services. Our services include developing applications for all your ERP needs from CRM, WFM, and HRM to eCommerce, accounting, and wealth management software. We employ a variety of testing methodologies and use an extensive tech stack including Javascript, Node.js, angular, and more to develop end-to-end customized software for your enterprise. Get in touch with our experts to know more about how you can implement these technologies into your business.