TestNG Tutorial

TestNG Tutorial

Introduction to TestNG

TestNG Is Open Source (Freeware) testing framework which Is inspired by JUnit and NUnit with Introducing a few new features and functionality compared to NUnit and JUnit to make It easy to use and more powerful. 

We can use TestNg with selenium web-driver to configure and run our automation test cases very easily, easy to understand, and to generate different test reports like HTML or XSLT.

Some Major Features of TestNG

ü  Support of @DataProvider annotation to perform data-driven testing, 
ü  To set test case execution dependency.
ü  Separates compile-time test code from run-time configuration/data info.
ü  Flexible runtime configuration
ü  Test case grouping
ü  To generate HTML and XSLT test execution report.

Difference between TestNG and Junit

Few of the major difference between Junit and TestNG are:

Parameter configuration is very tuff
Parameter configuration is very easy.

JUnit does not support group test.
TestNG support group test
@Test(group ={sanity})

These are not supportable.
TestNG support
@BeforeTest, @AfterTest,
@BeforeSuite, @AfterSuite,
@BeforeGroups, @AfterGroups
which are not supported in JUnit.
Test prioritizing and parallel testing is not possible
In testNG test priority is possible using @Test(priority = 1)
Parallel test case is also possible by calling the multiple test in a <suite>

Different annotations supported by TestNG

TestNG supports many different annotations to configure the Selenium WebDriver test. 


@Test annotation describes the method as a test method or part of your test. 

@Test (Priority = 1) 
@ to the priority of the function 
@Test(group = {“smoke”}) 
@ set the method name with a group, when testNG will run “smoke” group only those methods will run which are only belongs to the “smoke” group.


When you want to pass parameters in your test methods, you need to use @Parameters annotation. 

In the .xml file (<parameter name="browser" value="FFX" /> 
In the class, use just above the methods @Test @Parameters ({"browser"}) 


Any method which is marked with @BeforeMethod annotation will be executed before each and every @test annotated method. 


Same as @BeforeMethod, If any method is annotated with @AfterMethod annotation then it will be executed after execution of each and every @test annotated method. 


A method annotated using @BeforeClass will be executed before the first @Test method execution. @BeforeClass annotated method will be executed once only per class so don't be confused. 


Same as @BeforeClass, A method annotated with @AfterClass annotation will be executed once only per class after the execution of all @Test annotated methods of that class. 


@BeforeTest annotated method will be executed before any @Test annotated method of those classes which are inside <test> tag in the testng.xml file. 


@AfterTest annotated method will be executed when all @Test annotated methods complete its execution of those classes which are inside <test> tag in the testng.xml file. 


A method marked with @BeforeSuite annotation will run before all suites from the test. 


@AfterSuite annotated method will start running when the execution of all tests executed from the current test suite. 


When you use @DataProvider annotation for any method that means you are using that method as a data supplier. Configuration of @DataProvider annotated method must be as it always returns Object[][] which we can use in @Test annotated method. 


@BeforeGroups annotated method will run before the first test run of that specific group. 


@AfterGroups annotated method will run after all test methods of that group completes its execution. 


When you want to execute a specific group of test cases with different values, you need to use @Factory annotation. An array of class objects is returned by @Factory annotated method and those TestNG will those objects as test classes. 


@Listeners are used to with test class. It is helpful for logging purposes.

Usage of TestNG.xml file

In selenium WebDriver, we are using a testng.xml file to configure the whole test suite in a single file. 

A few of the tasks which we can specify in the testng.xml file are as below. 

ü  To define a test suite using a set of test cases to run them from a single place. 
ü  To Include or exclude test methods from test execution. 
ü  To specify a group to include or exclude. 
ü  To pass a parameter to use in the test case. 
ü  To specify grouping dependencies. 
ü  To configure and execute parallel test execution. 
ü  To define listeners.

TestNG Assertions

Soft Assert: Soft Assert collects errors during @Test Soft Assert does not throw an exception when an assert fails and would continue with the next step after the assert statement

Hard Assert: Hard Assert throws an AssertException immediately when an assert statement fails and the test suite continues with next @Test.

Let's look into both of these assertions into more detail.

Hard Assertions in TestNG

There are many different assertions available. But in this article, I am going to discuss only the major once.


This assertion is useful to compare expected and actual values in selenium webdriver. If both values match then it's fine and will continue execution. But if fails then immediately it will mark that specific test method as fail and exit from that test method. 

Assert.assertEquals(actual, expected); 
Assert.assertEquals(Actualtext, "Tuesday, 28 January 2014");

assertNotEquals :

Its function is opposite to assertEquals assertion. Means if both sides values will not match then this assertion will pass else it will fail 

Assert.assertNotEquals(actual, expected, message) 

Assert.assertNotEquals(Actualtext, "Tuesday, 28 January 2014", "Expected and actual match in assertion_method_1"); 


assertTrue assertion is generally used for Boolean condition true. It will pass if the condition returns "true". If it will return false then it will fail and skip test execution from that specific method. 

Assert.assertTrue(driver.findelement(By.id(“ ”)).isSelected()); 


It will check the boolean value returned by condition and will pass if the returned value is "False". If the returned value is pass then this assertion will fail and skip execution from the current test method. 


Assert.assertFalse(driver.findelement(By.id(“ ”)).isSelected());


assertNull(object) assertion will check and verify that the object is null. It will pass if the object found null. If the object found not null then it will return an error message like "java.lang.AssertionError: expected [null] but found [true]". Whenever your assertion fails, it will mark that specific test method as fail in TestNG result. 


txt1 = driver.findElement(By.xpath("//input[@id='text1']")); 


assertNotNull assertion is designed to check and verify that values returned by the object are not null. Means it will be pass if the returned value is not null. 


txt1 = driver.findElement(By.xpath("//input[@id='text1']")); 

TestNG Soft Assertions

Using TestNG soft assertion, we can continue our test execution even if the assertion fails that means on the failure of soft assertion, the remaining part of the @Test method will be executed, and an assertion failure will be reported at the end of @Test method. 

To use TestNG soft assertion, you have to use the TestNG SoftAssert class. This class will help to not throw an exception on assertion failure and recording failure. 

If we will use soft assertion then our test execution will remain to continue even If any assertion fails. Another important thing Is our assertion failure will be reported in the report so that we can view It at the end of the test. 

We can use soft assertion when we are using multiple assertions In the same test method and you want to execute all of them even If anyone In between fails. 

//Created object of TestNG SoftAssert class to use Its Properties. 

SoftAssert s_assert = new SoftAssert(); 

//Text on the expected side Is written Incorrect intentionally to get fail this assertion. 

Assert.assertEquals(Actualtext, "Tuesday, 01 January 2014", "1st assert failed."); 
System.out.println("Hard Assertion -> 1st pagetext assertion executed." 

//Text on the expected side Is written Incorrect intentionally to get fail this assertion. 

s_assert.assertEquals(Actualtext, "Tuesday, 01 January 2014", "1st assert failed."); 
System.out.println("Soft Assertion -> 1st pagetext assertion executed.");



My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post