0% found this document useful (0 votes)
14 views19 pages

Module 8-Testng

TestNG is an open-source testing framework that enhances JUnit's functionality, allowing for better test organization, reporting, and parallel execution. It features annotations for test management, supports test dependencies, and enables parameterization through TestNG Parameters and DataProviders. TestNG also allows grouping of tests and requires an XML file for executing test suites.

Uploaded by

ashaghadge199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

Module 8-Testng

TestNG is an open-source testing framework that enhances JUnit's functionality, allowing for better test organization, reporting, and parallel execution. It features annotations for test management, supports test dependencies, and enables parameterization through TestNG Parameters and DataProviders. TestNG also allows grouping of tests and requires an XML file for executing test suites.

Uploaded by

ashaghadge199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

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?

• There are a number of benefits but from Selenium perspective, major


advantages of TestNG are :
• It gives the ability to produce HTML Reports of execution
• Annotations made testers life easy
• Test cases can be Grouped & Prioritized more easily
• Parallel testing is possible
• Generates Logs
• Data Parameterization is possible
TESTNG ANNOTATIONS

@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

We can have multiple @Test annotations in a single TestNG file


By default, methods annotated by @Test are executed alphabetically
Methods can be executed in a different order, by using parameter
"priority"
@Test(priority=0)
TestNG will execute the @Test annotation with the lowest priority
value up to the largest
DEPENDENT TESTS
TestNG allows to specify dependencies with attributes dependsOnMethods
@Test(dependsOnMethods={'openBrowser'}
public void closeBrowser()
{
driver.close();
}
@Test Public void openBrowser()
{
driver = new FirefoxDriver();
}
TESTNG SUITE

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

Groups in TestNG denotes the process of grouping different tests


together into a straightforward group and running these tests
together by just running the group in a single command.
GROUPING OF TESTS

<?xml version="1.0" encoding="UTF-8"?>


<suite name="Sample Suite">
<test name="testing">
<groups>
<run>
<include name="Regression"/>
</run>
</groups>
<classes>
<class name="" />
PASSING PARAMETERS IN TESTNG

There are two ways to pass the parameters in TestNG:


TestNG Parameters
TestNG DataProviders
DataProviders pass the different parameters on a single test in a single
execution, whereas parameters pass the parameters just once per execution
in TestNG.
TESTNG PARAMETERS

Parameters in TestNG is similar to annotations in TestNG in their


declaration. Similar to parameters in any other programming language, they
are declared to pass some values onto the function. A simple reason to use
parameters is that they let us run a function many times with different values or
to run different functions with the same values. Parameters pass the values in
the runtime. An example of using the parameters in TestNG can be entering
different values in an input box
@Parameters ({"a", "b"})
where a and b are the values that pass to the function.
TESTNG PARAMETERS

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

@Parameters({“ ”}) ● In .xml file use


<parameter name="" value="" />

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG Parameters Suite">
<test name="Params">
<parameter name="val1" value="2" />
<parameter name="val2" value="3" />
<classes>
<class name="Params" />
</classes>
</test>
</suite>
TESTNG DATAPROVIDERS

• 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

public class Dprovider


{
@DataProvider (name = "data-provider")
public Object[][] dpMethod()
{
return new Object[][] {{2, 3 , 5}, {5, 7, 9}};
}
@Test (dataProvider = "data-provider",dataProviderClass=Dprovider.class)
public void myTest (int a, int b, int result)
{
int sum = a + b; Assert.assertEquals(result, sum);
}
}

You might also like