Introduction
TestNG is an open-source test automation framework for Java. The NG in TestNG stands for ‘Next Generation’, created by Cedric Beust, it is used more frequently by developers and testers in test case creation, owing to its ease of using multiple annotations, grouping, dependence, prioritization, and parametrization features. Many plugins are integrated with TestNG.
Advantages of TestNG
- TestNG provides Parallel execution of test Method.
- It allows to define dependency of one test method over other method.
- It has support for parameterizing test cases using @Parameter annotation.
- It has different assertions that helps in checking the expected and actual results.
- It allows data driven driven testing using @Data provides annotation.
- It can be use Test Method annotation(@test) Instead of Main method for Execution.
- Can we call Non static method without creating object but, we can call through the TestNG.
Installation of TestNG
Step 1. Launch Eclipse and click on the help menu then select ‘Eclipse Marketplace’
Step 2: A new window for ‘Eclipse Marketplace’ will open, enter ‘TestNG’ in the find box on the top left and click on the go button and then click on install.
Step 3: After that click on libraries and select ‘Add Library’, then select TestNG and click on finish button.
- Generate TestNG.XML file
Click on TestNG and then select convert to TestNG to generate the ‘TestNG.XML’ file
Execution of TestNG.XML file
- Click on TestNG.xml file and click on run as TestNG suite.
Importance of TestNG.XML file
- TestNG.XML file to configure the complete test suite in a single file
- It allows Include or exclude the execution of test methods and test groups
- Allows Pass parameters to the testcases
- Allows add group dependencies
- Allows to configure parallel execution of test cases
TestNG Class
It is a java class which consist of Test method annotation(@Test)
Test Method– Any method which is developed by using Test annotation is called as Test method, for ex.
Package Demo;
import org. testing. Reporter;
public class Test A
{
@Test
public void test1()
{
Reporter.Log(“1”, true);
}
}
Important Annotations of TestNG
- @ Before Suite
- @ Before Test
- @ Before Class
- @Before Method – Used to perform opening the Application
- @Test- Used to perform action on the browser
- @ After Method- Used to perform to close the Application
- @After Class
- @After Test
- @ After Suite
- @ Find By
- @ Parameter
- @Data provider
How to skip the testcases
- Invocation count=0
- @Ignore
- @Test (Enabled=false)
- depends on Method
- depends on group
- Commenting the Test Method (/*——-*/)
- Throw new skip exception ();
Example: –
@Test (Priority=2, invocation count=3, enabled=false)
public void test 1 ()
{
system.out.printlin(“test 1”);
}
@Test
public void test 1 ()
{
system.out.printlin(“test 2”);
}
Output: test2
Parameterized test
This can be done in two ways
- With testing.xml(@Parameters)
- With Data providers (@ Data providers)
- Create a java test class, say, TestParameterized.java.
- Add test method parameterTest() to your test class.
- Add the annotation @Parameters(“name”) to this method. The parameter would be passed a value from testng.xml
Create a java class file named TestParameterized.java
Example: –
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParameterizedTest1
{
@Test
@Parameters(“Name”)
public void Test parameter (String Name)
{
System.out.println(“Parameterized value is: ” + Name);
}
}
Create testng.xml
Data providers
- It executes Same set of testcases with different inputs and to read data from the excel file
- Return type of data providers is 2dimensional arrary
Example: –
@Data provider
public string [][] dp() throws Encrypted document exception, IO Exception, Invalid Format exception
{
FileInputStreamfis= new FileInputStream(“Path of excel sheet”);
Workbook wb= wb.getsheet(“sheet 1”);
Row firstRow= s.getRow(0);
int row count=s.getpgysical number of Row();
int col count= first Row.getLastcell Num ();
for (int i=1; i { for (int j=0; j { arr[i][j]=s.getRow(i).s.getce;;(j).to string(); } } @Test (data provider=” dp”) public void test (String s1, String s2) { system.out.println(S1); system.out.println(S2); } } How to perform failed testcases automatically Failed testcases can run through by using I Retry Analyzer Example: – Class name retry.java public class retry implements IRetry Analyzer { Int count=0; Int max_count=3; public Boolean retry(ITestResult result) { if (count { count++; return true; } return false; } }