0% found this document useful (0 votes)
154 views3 pages

TestNG Parameterization Guide

TestNG is a testing framework that provides rich annotations, better reporting, prioritization of test cases, skipping tests, dependency control, grouping, parallel execution, and data-driven testing. Key annotations include @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeClass, @AfterClass, @BeforeMethod, @AfterMethod, and @Test. The @DataProvider annotation allows data-driven testing by supplying data to test methods. The preserve-order attribute controls test execution order, which is preserved by default. The testng.xml file can specify packages to execute tests from.

Uploaded by

johhar saai
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)
154 views3 pages

TestNG Parameterization Guide

TestNG is a testing framework that provides rich annotations, better reporting, prioritization of test cases, skipping tests, dependency control, grouping, parallel execution, and data-driven testing. Key annotations include @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeClass, @AfterClass, @BeforeMethod, @AfterMethod, and @Test. The @DataProvider annotation allows data-driven testing by supplying data to test methods. The preserve-order attribute controls test execution order, which is preserved by default. The testng.xml file can specify packages to execute tests from.

Uploaded by

johhar saai
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/ 3

4/27/2020 Gmail - testNG

johhar saai <[email protected]>

testNG
1 message

Automation Tester <[email protected]> Thu, Jan 23, 2020 at 9:56 AM


To: johhar saai <[email protected]>, [email protected]

TESTNG:

Advantages:
1)it provides rich annotation support
2)it generates better html/xslt reports
3)you can prioritize the testcases(@Test methods)
4)you can skip the testcases
5)you can set dependancy among the test methods
6)you can group the test cases
7)you can run the testcases parallally
8)you can do datadriven testing

Annotations:
@BeforeSuite:The annotated method will be run before all tests in this suite have run.

@BeforeSuite
public void beforeSuite(){
//write the code
}

@AfterSuite:The annotated method will be run after all tests


in this suite have run.
@AfterSuite
public void afterSuite(){
//code here
}
@BeforeTest:The annotated method will be run before any test
method belonging to the classes inside the <test> tag is run.
@BeforeTest
public void beforeTest(){

//code here;
}
@AfterTest: The annotated method will be run after all the
test methods belonging to the classes inside the <test> tag have run.

@AfterTest
public void afterTest(){
//code here;
}
@BeforeClass:The annotated method will be run before the
first test method in the current class is invoked
@BeforeClass
public static void beforeClass(){
//precondition code for test
}
@AfterClass:The annotated method will be run after all
the test methods in the current class have been run
@AfterClass
public static void afterClass(){
destroycode;
}
@BeforeMethod: The annotated method will be run before each
test method
@BeforeMethod
public void setup(){
precondition code for every testcode
}

@AfterMethod:The annotated method will be run after each


test method
@AfterMethod
public void tearDown(){
destroycode;
}

@Test:only test steps should be written.this annotated method


will be run
after the Before related annotations

@Test
public void methodTest(){
https://mail.google.com/mail/u/0?ik=36f75d6d43&view=pt&search=all&permthid=thread-f%3A1656491705914775966&simpl=msg-f%3A16564917059… 1/3
4/27/2020 Gmail - testNG
//only test steps;
}

@Parameters:Describes how to pass parameters to a @Test method


value:The list of variables used to fill the parameters of
this method.
To do this, we need to declare parameters tag in xml file
using 'name' and 'value' attribute.
Where the name attribute of the tag defines name of the
parameter and the value attribute defines the value of the
parameter.
parameter names are decalred inside the class and above the
method
@Parameters({"parameter1","parameter2","parameter3"..})

//parameter values will be supplied from testng.xml file


testng.xml format
-------------------------------------------
<suite name="suitename">
<test name="testname">
<parameter name="parameter1" value="value of parameter1"/>
<parameter name="parameter2" value="value of parameter2"/>
<classes>
<class name="packagename.classname"/>
<class name="packagename.classname"/>
</classes>
</test>
</suite>

=====================================
@DataProvider:we can do datadriven testing with this
annotation.
It is used for supplying data for a test method.
The annotated method must return an Object[][]
where each Object[] can be assigned the parameter list
of the test method. The @Test method that wants to receive
data from this DataProvider needs to use a dataProvider
name
equals to the name of this annotation.

public class DataproviderDemo{

@DataProvider(name="nameof the dataprovider")


public Object[][] methodName(){

Object[][] data=new Object[rows][cols];


//rows tells you number of times test has to be executed
//columns will tell you number of parameters in the test mehod
Object[][] data=new object[3][2];
//first row
data[0][0]="uname1";
data[0][1]="pwd1";
//2nd row
data[1][0]="uname2";
data[1][1]="pwd2";

//3rd row
data[2][0]="uname3";
data[2][1]="pwd3";
return data;
}
@Test(dataProvider="dataprovidername/dataprovidermethodname")
public void loginTest(String uname,String pwd){

Sop(uname+" "+pwd);
}
}

}
=============================================================================
preserveorder:
If you want your classes / methods to be run in an

unpredictable order, then we should go for


preserve-order attribute in testng. In TestNg bydefault
the preserve-order attribute will be set
to 'true', this means, TestNG will run your tests in the
order they are found in the XML file.

xml file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Preserve order test runs">
<test name="Regression on preserveorder" preserve-order="false">
https://mail.google.com/mail/u/0?ik=36f75d6d43&view=pt&search=all&permthid=thread-f%3A1656491705914775966&simpl=msg-f%3A16564917059… 2/3
4/27/2020 Gmail - testNG

<classes>
<class name="preserve.FetchSearchResults"/>
<class name="preserve.LocaleTesting"/>
<class name="preserve.GmailOnlinePeople"/>
</classes>
</test>
</suite>
======================================================================
TestNG XML example to execute with package names
In testng.xml file we can specify the specific package name
(s) which needs to be executed.
In a project there may be many packages, but we want to
execute only the selected packages.
The below is the example testng.xml which will execute the
specific packages.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="package suite 1" verbose="1" >
<test name="package run test" >
<packages>
<package name="preserveorder" />
</packages>
</test>
</suite>

https://mail.google.com/mail/u/0?ik=36f75d6d43&view=pt&search=all&permthid=thread-f%3A1656491705914775966&simpl=msg-f%3A16564917059… 3/3

You might also like