Module 8-Testng
Module 8-Testng
MODULE 8: TESTNG
Agenda
Introduction to TestNG
Advantages of TestNG
TestNG Annotations
TestNG Priority
Dependent Test
TestNG Suite
Grouping Tests
TestNG Parameters
INTRODUCTION TO TESTNG
TestNG is a testing framework inspired from JUnit and NUnit but introducing
some new functionality that makes it more powerful and easier to use.
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 but still,
it's inspired by JUnit.
It is designed to be better than JUnit, especially when testing integrated
classes. Pay special thanks to Cedric Beust who is the creator of TestNG.
WHY TESTNG?
@Test
The annotated method is a part of a test case
@BeforeMethod
The annotated method will be run before each test method @AfterMethod
The annotated method will be run after each test method
@BeforeClass
The annotated method will be run before the first test method in the current class
is invoked
TESTNG ANNOTATIONS
@AfterClass
The annotated method will be run after all the test methods in the current class
have been 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
TESTNG ANNOTATIONS
@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
ANNOTATIONS EXECUTION
Testsuit
Test
Class
Method
Test
Method
Class
Test
Testsuit
MULTIPLE TEST CASES/PRIORITY
The collection of TestNG Tests together is called a Test Suite. A test suite can
run multiple tests at once by executing the test suite. Additionally, these test
cases can be dependent on each other or may have to be executed in a specific
order independently.
It is important to remember that the we need to create a TestNG XML file for
test suite o be run
GROUPING OF TESTS
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Params
{
@Test @Parameters ({"val1", "val2"})
public void Sum(int v1, int v2)
{
int finalsum = v1 + v2;
System.out.println("The final sum of the given values is " + finalsum);
}
}
Note: TestNG Parameters are run through the TestNG XML file and not from the test case files
directly.
PASSING PARAMETER
• The DataProviders in TestNG are another way to pass the parameters in the test
function, the other one being TestNG parameters. DataProviders pass different
values to the TestNG Test Case in a single execution and in the form of TestNG
Annotations.
DataProvider Syntax:
The TestNG DataProvider is used in the following manner:
@DataProvider (name = "name_of_dataprovider")
public Object[][] dpMethod()
{
return new Object [][] { values}
}
TESTNG DATAPROVIDERS