1. What is TestNG?
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. The software testers to efficiently run the automated test scripts created in Selenium Webdriver use TestNG. Its full form is the “Testing New Generation” framework. It is use mostly to assert the results in an Automated Script as well as generate reports for test execution.
2. How TestNG can be install in Eclipse?
Follow the below steps to install TestNG on Eclipse:
- Launch Eclipse and go to Help option present at the top and select -“Install New Software”
- A dialog box will appear, click on Add button
- A new dialog box will appear. Mention Name as TestNG and location as “http://beust.com/eclipse/” and click on Add button.
- Check the TestNG checkbox and click on the “Next” action button. The installation will start and the Eclipse will restart after installation.
- To verify if TestNG is installed successfully or not, go to Window, select Show View and then Other. Select Java and see within Java folder, you will see TestNG. This shows that TestNG is successfully install on the machine
- Right-click on the project in Eclipse -> Select build path -> Configure Build Path.
- Select the library tab -> Click on Add library button -> Select TestNG-> Click on Next -> Click on Finish and Apply and close.
3. What is the sequence of execution of the annotations in TestNG?
The Sequence of execution of the annotations is as follows:
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@Aftertest
@AfterSuite
For more details,click here
4. What is the importance of testng.xml file?
The testng.xml file is important because of the following reasons:
- It defines the order of the execution of all the test cases.
- It allows you to group the test cases and can be execute as per the requirements by mentioning
groups = { “e2etest”, “integerationtest” }
- It executes the selected test cases. It can disable the test cases which we don’t want to execute
@Test(enabled = false)
- In TestNG, listeners can be implemented at the suite level by adding below in xml
listenerclass-name =“com.selenium.testng.ListenerDemo
- It helps to integrate the TestNG framework with tools such as Jenkins.
- It supports parallel testing means provides multiple ways to execute tests in separate threads.
suitename=“TestSuite”thread-count=“3”parallel=“methods
- It allows for cross browser testing which means a web application tests on different browsers and operating systems. Here, mention different browser name in parameter.
<parameter name=”browser” value=”Edge” />
5. What are the types of Asserts in TestNG?
There are two types of assert in TestNG – Hard Assert and Soft Assert
Hard Assert: Hard Assert is the normal assert which is use to do validations in the TestNG class.
We have to use Assert class for hard assert as follows:
Assert.assertEquals(actual value, expected value);
If the hard assert fails, then none of the code is executed after the assert statement.
Soft Assert: If we want to continue the test execution even after the assert statement fails, then we have to use soft assert.
To create a soft assert, we have to create an object of a “softAssert” class as follows:
softAssert soft_assert = new softAssert();
soft_assert.assertAll();
So now if the test case fails, the execution is not terminate when we use soft assert.
For more details, click here
6. What is TestNG Assert and list out some common Assertions supported by TestNG?
TestNG Asserts help us to verify the condition of the test in the middle of the test run. Based on the TestNG Assertions, we will consider a successful test only if it is completed the test run without throwing any exception. Some of the common assertions supported by TestNG are
assertEqual(String actual,String expected)
assertEqual(String actual,String expected, String message)
assertEquals(boolean actual,boolean expected)
assertTrue(condition)
assertTrue(condition, message)
assertFalse(condition)
assertFalse(condition, message)
For more details, click here
7. How to run a group of test cases using TestNG?
Groups are specified in your testng.xml file and can be found either under the or tag. Groups specified in the tag apply to all the tags underneath.
Test Case 1
@Test (groups = { "smokeTest", "functionalTest" })
public void loginTest(){
System.out.println("Logged in successfully");
}
Test Case 2
@Test (groups = { "functionalTest" })
public void loginTest(){
System.out.println("Logged in successfully");
}
TestNG.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "configureselenium">
<test name = "TestNG Demo">
<groups>
<run>
<include name = "e2etest" />
</run>
</groups>
<classes>
<class name = "TestNGDemo.TestNGGroupDemo1" />
<class name = "TestNGDemo.TestNGGroupDemo2" />
</classes>
</test>
</suite>
8. How to set test case priority in TestNG?
We use priority attribute to the @Test annotations. If no priority assigned to a Test Case, then the annotated test methods are, execute as per the alphabetical order of the tests
import org.testng.annotations.*;
public class PriorityTestCase{
@Test(priority=0)
public void testCase1() {
system.out.println("Test Case 1");
}
@Test(priority=1
public void testCase2() {
system.out.println("Test Case 2");
}
}
9. How can we make one test method dependent on others using TestNG?
Using the dependsOnMethods parameter inside @Test annotation in TestNG we can make one test method run only after the successful execution of the dependent test method. Dependency is a feature in TestNG that allows a test method to depend on a single or a group of test methods. Method dependency only works if the “depend-on-method” is part of the same class or any of the inherited base classes (i.e. while extending a class)
@Test
public static void FirstTest() {
System.out.println("This is Test Case 1");
}
@Test(dependsOnMethods = "FirstTest")
public static void SecondTest() {
System.out.println("This is Test Case 2 and will be executed after Test Case 1 sucessfully executed");
}
@Test
public static void ThirdTest() {
System.out.println("This is Test Case 3");
}
@Test
public static void FourthTest() {
System.out.println("This is Test Case 4");
}
}
For more details, click here
10. How can we set the priority of test cases in TestNG?
Using the priority parameter in @Test annotation in TestNG we can define the priority of test cases. The default priority of the test when not specified is integer value 0. Example,
@Test(priority = 1)
11. How to skip a method or a code block in TestNG?
If you want to skip a particular test method, then you can set the ‘enabled’ parameter in test annotation to false.
@Test(enabled = false)
By default, the value of ‘enabled’ parameter will be true. Hence, it is not necessary to define the annotation as true while defining it.
For more details, click here
12. How do you define groups in TestNG?
TestNg allows us to group test methods that is not present in JUnit. We can declare the methods belong to groups as well as specify groups that contain other groups. To group test cases, we need to create testng.xml.
@Test(groups = { "e2etest", "integerationtest" })
Testng.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
<test name = "Test Demo">
<groups>
<run>
<include name = "e2etest" />
</run>
</groups>
<classes>
<class name = "TestNGDemo.TestNGGroupDemo" />
</classes>
</test>
</suite>
For more details, click here
13. How do you exclude a group from the test execution cycle?
Excluding a group in TestNG denotes that this particular group refrains from running during the execution, and TestNG will ignore it. Additionally, the name of the group that we want to exclude is defined in the XML file by the following syntax:
<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Suite1">
<test name = "Test Demo">
<groups>
<run>
<exclude name = "e2etest"/>
</run>
</groups>
<classes>
<class name = "com.selenium.testng.TestNGDemo.TestNGGroupDemo"/>
</classes>
</test>
</suite>
By putting our group “e2etest” inside the exclude tag, we are requesting TestNG to ignore the test cases under the group “e2etest”.
14. How to run test cases in parallel using TestNG?
In testng.xml, if we set ‘parallel’ attribute on the tag to ‘methods’, testNG will run all the ‘@Test’ methods in tag in a separate thread.
The parallel attribute of suite tag can accept four values:
tests – All the test cases inside tag of testng.xml file will run parallel
classes – All the test cases inside a java class will run parallel
methods – All the methods with @Test annotation will execute parallel
instances – Test cases in same instance will execute parallel but two methods of two different instances will run in different thread.
Below is an example to testng.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="3" parallel="methods" >
<test name="GoogleTest">
<classes>
<class name="TestNGDemo.ParallelTestDemo">
</class>
</classes>
</test>
</suite>
For more details,click here
15. What is the use of @Listener annotation in TestNG?
Listener is define as interface that modifies the default TestNG’s behavior. It allows customizing TestNG reports or logs. There are many types of TestNG listeners available. Here are a few listeners:
- IAnnotationTransformer
- IAnnotationTransformer2
- IHookable
- IInvokedMethodListener
- IMethodInterceptor
- IReporter
- ISuiteListener
- ITestListener
For more details, click here
16. How are listeners declared in TestNG?
When you implement one of these interfaces, you can let TestNG know about it with either of the following ways:
- Using in yourtestng.xml file.
@Listeners(com.selenium.testng.TestNGDemo.ListenerDemo.class)
Using the@Listeners annotation on any of your test classes.
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "TestSuite">
<listeners>
<listener class-name ="com.selenium.testng.TestNGDemo.ListenerDemo"/>
</listeners>
<test name ="Test">
<classes>
<class name="com.selenium.testng.TestNGDemo.ListenerTestCases"/>
</classes>
</test>
</suite>
For more details, click here
17. Do TestNG reports need external code to write?
No, there is no need to write any code to generate reports in TestNG. In other words, the reports generation happens by default.
18. What are the two ways to generate a report in TestNG?
We can generate the TestNG reports in two ways:
· Emailable Reports
· Index Reports
19. Where is the emailable report generate and saved in TestNG?
Emailable reports generate under the project folder and test-output subfolder. This report is available as “emailable-report.html” by default.

20. Where is the index report generate and saved in TestNG?
The index report generates under the project folder and test-output subfolder. Moreover, this report is available as “index.html” by default.

21. What is invocationCount in TestNG?
An invocationCount in TestNG is the number of times that we want to execute the same test.
import org.testng.annotations.Test;
public class InvocationCountDemo {
@Test(invocationCount = 5)
public void testcase1() {
System.out.println("testcase1");
}
}
Output
testcase1
testcase1
testcase1
testcase1
testcase1
PASSED: testcase1
PASSED: testcase1
PASSED: testcase1
PASSED: testcase1
PASSED: testcase1
22. How to pass the parameter in test case through testng.xml file?
TestNG has the ability to pass different test data to a test case as arguments that is called parametrization
@Parameters("value")
TestNG.xml looks like as shown below. Here, parameter name is browser name value for browser is “Chrome”. So, this “Chrome” value is pass to Test as parameter and as a result a Google Chrome browser opens.
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "TestSuite">
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="com.selenium.testng.TestNGDemo.TestNGParameterizationDemo">
</class>
</classes>
</test>
</suite>
For more details, click here