Our Engineers can Write as well

How to use TestNG Framework in Automated Testing?

TestNG is an open source automated testing framework inspired from JUnit and NUnit. The NG in TestNG denotes Next Generation – the name itself is self-explanatory indicating more functionalities are added for agility and user experience.

Here is a quick glance about the key features of TestNG.

Annotations of TestNG:

@BeforeSuite & @AfterSuite               @BeforeClass & @AfterClass               @BeforeTest & @AfterTest
@BeforeGroups & @AfterGroups               @BeforeMethod & @AfterMethod               @DataProvider
@Factory                @Listeners               @Parameters                @Test

How to Enable TestNG?

In the Eclipse, right-click the Project Name -> New -> Other -> Select the TestNG class; from the Pop-up Select Source folder, Package name and Class name; Select Annotations; and Click the Finish button.

If you have created a software application called ‘focus’ and you are attempting to create a TestNG test class for a source class called Users which is under ‘src’ folder under a package structure like ‘com/web/users’, then you will select source folder as focus/src/com/web/users/UserLogin and input a package name as ‘Tests’ under which you want to create your test classes relevant to Users and input Test Class name as ‘UserLoginTest’ or ‘GuestUserAccess’ or anything that you may require.

testng framework
Set Test Priority:

In this framework, multiple sets of @Test Annotations can be added and there is a provision to set priority for executing the test methods with “priority”.

@Test (priority = 2)

public void sample (Integer n, String s) {

//code

}

@Test (priority = 1)

public void form (Integer n, String s) {

//code

}

Disable Test:

If you do not want to execute a specific test method, you can implement the below code written with “enabled”.

@Test (enabled = false)

public void f(Integer n, String s) {

//code

}

Provide Data to Test:

The @DataProvider will supply Data for the @Test Methods. Given below is a sample for DataProvider.

@Test (dataprovider = “Login”)

public void f(Integer n, String s) {

//code

}

@DataProvider

public Object[][] Login() {

return new Object[][] }

Test Suite:

Test Suite is used to execute multiple, related tests together. Once all the tests are executed successfully, the error logs will be displayed in the console.

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >

<suite name = “Test_Suite”>

<test name = “Test_name”>

<classes>

<class name = “package.Sample”/>

<class name = “package.Test”/>

</classes>

</test>

</suite>

Test Report:

The success of any test execution is measured by its reporting potential. Reporting enables testers understand the points of failure as well as the reasons behind. Logging is also equally important to keep a watch on the execution flow or to debug in case of any major failures.

By default, TestNG generates different types of reports for its test execution. We’ll see them in detail below.

TestNG Results Window:

Once a test is executed successfully, TestNG displays results of passed and failed tests in the Results window, along with the time taken to run each test.

testNg result window
Console Report:
testNg console report
Index Report:

The Index Report generates detailed information such as errors, test groups, execution time and step-by-step logs. A sample is given in the below screenshot.

testNg Index report
Summary Report:

This is the summarized report of the tests executed. It contains messages in green highlight (for passed test cases) and red highlight (for failed test cases).

testNg summary report
Major Benefits of TestNG:
  • Helps reduce testing time considerably using annotations

  • Provides the ability to produce HTML Reports and generates logs

  • Helps prioritize and group test cases effortlessly

  • Makes parallel testing and data parameterization possible

Ragul Deep - software testing engineer

Contributed by:

Ragul Deep
Software Testing Engineer

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