TestNG Framework

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

  1. TestNG provides Parallel execution of test Method.
  2. It allows to define dependency of one test method over other method.
  3. It has support for parameterizing test cases using @Parameter annotation.
  4. It has different assertions that helps in checking the expected and actual results.
  5. It allows data driven driven testing using @Data provides annotation.
  6. It can be use Test Method annotation(@test) Instead of Main method for Execution.
  7. 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’

Launch Eclipse

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.

Eclipse Marketplace

Step 3: After that click on libraries and select ‘Add Library’, then select TestNG and click on finish button.

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

Generate 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

  1. TestNG.XML file to configure the complete test suite in a single file
  2. It allows Include or exclude the execution of test methods and test groups
  3. Allows Pass parameters to the testcases
  4. Allows add group dependencies
  5. 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

  1. Invocation count=0
  2. @Ignore
  3. @Test (Enabled=false)
  4. depends on Method
  5. depends on group
  6. Commenting the Test Method (/*——-*/)
  7. 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

  1. With testing.xml(@Parameters)
  2. 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;

}

}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *