0% found this document useful (0 votes)
11 views12 pages

TESTNG

The document discusses advanced TestNG annotations essential for SDET and automation testers, including attributes like alwaysRun, dependsOnMethods, and invocationCount. It also covers the use of Data Providers for parameterization in TestNG, detailing how to implement them within the same class or inherited from a utility class. Additionally, it explains the concept of partial data providers and how to run data providers in parallel, emphasizing their importance in maintaining reusable and scalable test scripts.

Uploaded by

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

TESTNG

The document discusses advanced TestNG annotations essential for SDET and automation testers, including attributes like alwaysRun, dependsOnMethods, and invocationCount. It also covers the use of Data Providers for parameterization in TestNG, detailing how to implement them within the same class or inherited from a utility class. Additionally, it explains the concept of partial data providers and how to run data providers in parallel, emphasizing their importance in maintaining reusable and scalable test scripts.

Uploaded by

Srinivas Venna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

TESTNGn

[Link]

Top asked advanced TestNG annotations: Set-1 for


SDET/Automation Testing/Developers?

SDET Interview Question and Answers.

Jenkins Interview Questions and Answers. Appium Interview Questions and Answers

Selenium Interview Questions and answers.

GIT Interview Questions and Answers

Must know TestNG annotations for Experienced Automation Testers

 alwaysRun
If set to true, this test method will always be run even if it depends
on a method that failed.

 dependsOnMethods
The list of methods this method depends on.

 @test:
Marks a class or a method as part of the test.

Example:

@Test

public void methodone() {

[Link]("Methodone");
// Failing test explicitly

[Link]();

// alwaysRun attribute will override dependsOnMethods if


dependent method is failed or skipped

@Test(dependsOnMethods = "methodone", alwaysRun=true)

public void methodtwo() {

[Link]("Method two");

 Description:
The description for this method. It is important to give a small
information about the test case.

 Enabled:
Whether methods on this class/method are enabled.
When executing TestNG tests, there are scenarios where you have
to disable a particular test or a set of tests from getting executed.
For example, where a serious bug exists in a feature due to certain
tests belonging to certain scenarios that cannot be executed. As the
issue has already been identified we may need to disable the test
scenarios from being executed.
Disabling a test in TestNG can be done by setting the enable attribute
of the @Test annotation to false. This will disable the test method
from being executed as part of the test suite.

Note: If this attribute is set for the Test annotation at class level,
all the public methods inside the class will be disabled.

Example:

@Test( description = "Verify Login with valid credentials", enabled=false )

Public void login(){}

 invocationCount
The number of times this method should get invoked.

 threadPoolSize
The size of the thread pool for this method. The method will be
invoked from multiple threads as specified by invocationCount.

Note: this attribute is ignored if invocationCount is not specified

 timeOut
The maximum number of milliseconds this test should take.

Earlier we discussed how to run classes, methods, and tests in


parallel or in multi-threaded mode. TestNG also provides the
flexibility to configure a test method to be run in a multi-threaded
environment. This is achieved by configuring it while using
the @Test annotation on a method.

[Link]

public class AutoTest

{
@Test(threadPoolSize = 3, invocationCount = 6, timeOut = 1000)

public void testMethod()

Long id = [Link]().getId();

[Link]("Test method executing on thread with id


as: " + id);

The method is configured to run in multi-threaded mode by using


the threadPoolSize attribute along with the Test annotation. The value
of the threadPoolSize is set to 3; this configures the test method to
be run in three different threads.

The other two attributes, invocationCount and timeOut, configures the


test to be invoked a multiple number of times and fail if the
execution takes more time.

 expectedExceptions
The list of exceptions that a test method is expected to throw. If no
exception or a different than one on this list is thrown, this test
will be marked a failure.

public class ExceptionTesting {

@Test(expected = [Link])
public void testWithException() {
int i = 1 / 0;
}
How To Use TestNg Data Provider
[Link]
utm_source=rss&utm_medium=rss&utm_campaign=use-testng-data-provider

TestNg (Testing framework for the next generation) is developed specifically for Java
programming Language. It is similar to Junit but offers additional functionalities like
additional annotations, parallel execution support etc. In this post, we will delve into the
TestNg data provider and its usage in detail.

Table of Contents
 What is Parameterization?
 How to Perform Parameterization Using TestNG
 What is TestNg Data Provider?
 DataProvider Syntax
o Prerequisite:
 How to Use DataProvider In TestNg
o 1) Use TestNG data provider Within the Same Class
o 2) Inherited DataProvider in TestNG
 Sample DataProvider Class
 Test Class
 How to use TestNg Data Providers partially
o
 What is a partial TestNG data provider?
 How can we partially pass the data to the method using the data provider?
 What are indices in the Data provider?
 Where can we use it (in which scenario)?
 How To Run Data Provider in Parallel (Not The Test Cases)
o How to maintain Thread count in data provider
 Conclusion

What is Parameterization?
Parameterization is the important concept of data-driven testing which refers to passing
dynamic inputs to methods or scripts to test the same logic against multiple data sets
and configurations. In Test automation, while performing cross-browser execution we
need to pass different configurations at run time, this is one of many examples of
parameterization.

How to Perform Parameterization Using


TestNG
We use below mentioned syntax in TestNG Java for parameterization.

@Parameters({"username", "password"})
What is TestNg Data Provider?
DataProvider is a TestNg Annotation used to pass the parameters in Test functions.
Data providers are widely used for data-driven testing which means the same test can
be executed with a different set of data. It is a very powerful feature of TestNG. There
are a few important points to note while using a data provider:

1) TestNg DataProvider name is always string type in nature.

2) It marks a method for supplying the data to another method.

3) Annotated method returns an array of objects i.e. object[][].

4) Data providers can be used in the same class or in a different class.

DataProvider Syntax
The TestNG data provider is the inbuilt function and returns a 2D list of objects that
follows the mentioned syntax.

@DataProvider (name = "name_of_dataprovider")


public Object[][] methodName()
{
return new Object [][] { values}
}
Prerequisite:
1. Java installation and configuration.

2. IDE

3. Maven project with Selenium Jars.

4. TestNg Dependency.

How to Use DataProvider In TestNg


Using a data provider is very easy. It can be utilized in multiple ways as per the
framework’s requirements. Let’s see how many ways we can utilize TestNG data
providers in our test automation framework.

1) Use TestNG data provider Within the Same Class


This is the most straightforward way to use a data provider. We have to declare a
method annotated by @DataProvider and then use this method in the tests using the
‘dataProvider‘ attribute in the @Test annotation. The steps are as follows:

1. Create a new test class inside the maven project and name it “DemoDataProvider”.

2. Define a method inside the “DemoDataProvider” class that uses


the @DataPovider annotation and returns a 2D object array.

3. Create a test method with the @Test notation that maps to the previous method and
prints out the data.

public class DemoDataProvider {

@DataProvider(name = "dataproviderdemo")
public Object[][] dataProviderMethod() {
return new Object[][]{{"data one"}, {"data two"}};
}

@Test(dataProvider = "dataproviderdemo")
public void testMethod(String data) {
[Link]("Data is: " + data);
}
}
2) Inherited DataProvider in TestNG
data provider and Test method can also be written in separate classes. This approach is
very useful for maintaining code reusability and readability. Suppose we have multiple
data providers for different test methods then we can create a utility class that can
keep all the data providers and this class can be used by using the attribute data
provider class.

Sample DataProvider Class


public class DataProviders {

@DataProvider(name="LoginTestDataProvider")
public Object[][] getInvalidCredentials()
{
return new Object[][] {
{"Admin@[Link]","admin123"},
{"test@[Link]","abc123"}
};
}

@DataProvider(name="EmailDataProvider")
public Object[][] getEmail()
{
return new Object[][] {
{"Admin@[Link]"},
};
}
}
In the above code, a utility class is created which contains multiple data providers. This
class can be inherited in the test class mentioned below.

Test Class
Create a test class and inside the @Test annotation pass the utility class name and the
required data provider name for the test function.

public class HomePageTest


{
@Test(dataProvider="LoginTestDataProvider",dataProviderClass =
[Link])
public void SignInWithInvalidUser(String email,String password)
{
[Link]();
[Link](email)
.enterPassword(password)
.clickSubmit();
}
}

How to use TestNg Data Providers


partially
What is a partial TestNG data provider?
The data provider can have multiple sets of data and out of all those multiple sets of
data we want to use only a few sets of data, How can we achieve this requirement? This
can be achieved using a partial data provider.

How can we partially pass the data to the method


using the data provider?
We use indices to pass the partial data. Indices are nothing but indexes of which data
we need to pass.

What are indices in the Data provider?


Indices are the one parameter inside the data provider annotation that accepts the
integer array. We pass the index value of the data set that we want to use partially for
our data provider. If we don’t use the indices then the entire data provided data will be
supplied to the test method.
Where can we use it (in which scenario)?
Practically our data provider can have huge data sets let’s say we have 100 sets of data
and 4-5 data set tests are failing. It is not an ideal approach to execute all 100 test
cases again for debugging, instead, we can use the concept of the partial data provider
and execute only the failing data set. For that, we need to use indices and provide the
index value of our intended data set.

@DataProvider(indices = {0,3})

public String[][] dp1()


{
String [][]data = new String[][]
{
{"Admin@[Link]","admin123"},
{"test1@[Link]","abc123"},
{"test2@[Link]","abc123"},
{"test3@[Link]","abc123"},
};
return data;
}
In the above code snippet, we have a data provider that supplies data for 4 users. I
need to pass the details of only the first and fourth users in my test method. To achieve
this I have provided the index {0, 3} with the @dataprovider annotation.

While executing the test script now data provider will provide data only for the first and
fourth users and the rest will be ignored.
How To Run Data Provider in Parallel (Not
The Test Cases)
Suppose we want to execute the data provider in parallel mode to feed the test data to
test methods parallelly rather than sequentially. Use attribute (parallel=true) else it
will execute in sequential order by default.

How to maintain Thread count in data provider


Suppose we have 100 sets of Test data and we set parallel=true then 100 browsers
will open, which may cause issues related to the network or any other execution-related
issues so we need to control how many sets of data can be executed parallelly. This can
be handled using the data provider thread count mechanism.

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


<!DOCTYPE suite SYSTEM "[Link]
<suite name="Suite" data-provider-thread-count="2">
<test name="Test">
<classes>
<class name="[Link]"/>
</classes>
</test>
</suite>
Created an XML file and added the attribute “data-provider-thread-count” at the
suite level with the desired value. This will control how many browsers will open at a
time and what set of data will feed to those browsers. For example, if we define data-
provider-thread-count=“3” and we have 9 data sets then data will be divided and fed as
3 data sets per browser.

To read more about testNg you can visit the official documentation.

Conclusion
TestNg data providers are very useful in maintaining the reusability and scalability of
test scripts. It also helps in maintaining a thread-safe environment along with
parameterization and data-driven testing.

Sample TestNG Project from GIT:


[Link]

TestNG Annotations:

[Link]

[Link]

Package classes:

AfterClass

AfterGroups

AfterMethod

AfterSuite

AfterTest

BeforeClass

BeforeGroups

BeforeMethod

BeforeSuite

BeforeTest

CustomAttribute

DataProvider

Factory

Guice

IAnnotation

IConfigurationAnnotation

IDataProviderAnnotation
IFactoryAnnotation

IIgnoreAnnotation

IListenersAnnotation

IObjectFactoryAnnotation

IParameterizable

IParametersAnnotation

ITestAnnotation

ITestOrConfiguration

Ignore

Listeners

NoInjection

ObjectFactory

Optional

Parameters

Test

TestInstance

You might also like