Top 17 TestNG Interview Questions - Javatpoint
Top 17 TestNG Interview Questions - Javatpoint
ADVERTISEMENT BY ADRECOVER
A list of top frequently asked TestNG Interview Questions and answers are given
below.
1) What is TestNG?
TestNG stands for "Testing Next Generation". It is an` automation testing framework
used for java programming language developed by Credric beust, and it comes after the
inspiration from the JUnit framework. TestNG consists of all the features of JUnit
framework but also contains some more additional features that make TestNG more
powerful.
https://www.javatpoint.com/testng-interview-questions 1/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
Multiple test cases can be grouped easily by converting them into a testng.xml
file, in which you can set the priority of each test case that determines which test
case should be executed first.
With the help of TestNG, you can execute the multiple test cases on multiple
browsers known as cross-browser testing.
The TestNG framework can be easily integrated with other tools such as Maven.
Jenkins, etc.
WebDriver does not generate the reports while TestNG generates the reports in a
readable format.
TestNG simplifies the way the test cases are coded. We do not have to write the
static main method. The sequence of actions is maintained by the annotations
only.
https://www.javatpoint.com/testng-interview-questions 2/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
TestNG allows you to execute the test cases separately. For example, if you have
six test cases, then one method is written for each test case. When we run the
program, five methods are executed successfully, and the sixth method is failed.
To remove the error, we need to run only the sixth method, and this can be
possible only through TestNG. Because TestNG generates testng-failed.xml file in
the test output folder, we will run only this xml file to execute the failed test case.
You can run the test script in TestNG by clicking right click on the TestNG class, click on
"Run As" and then select "TestNG test".
Precondition annotations
Precondition annotations are executed before the execution of test methods The
Precondition annotations are @BeforeSuite, @BeforeClass, @BeforeTest,
@BeforeMethod.
Test annotation
Test annotation is specified before the definition of the test method. It is specified
as @Test.
Postcondition annotations
The postcondition annotations are executed after the execution of all the test
methods. The postcondition annotation can be @AfterSuite, @AfterClass,
@AfterTest, @AfterMethod.
https://www.javatpoint.com/testng-interview-questions 3/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterSuite
@AfterTest
@AfterClass
@AfterMethod
If we do not prioritize the test methods, then the test methods are selected
alphabetically and executed. If we want the test methods to be executed in the sequence
we want, then we need to provide the priority along with the @Test annotation.
package com.javatpoint;
import org.testng.annotations.Test;
https://www.javatpoint.com/testng-interview-questions 4/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
@Test(priority=2)
System.out.println("Test1");
@Test(priority=1)
System.out.print("Test2");
The group is an attribute in TestNG that allows you to execute the multiple test cases.
For example, if we have 100 test cases of it_department and 10 test cases of
hr_department, and if you want to run all the test cases of it_department together in a
single suite, this can be possible only through the grouping.
package com.javatpoint;
import org.testng.annotations.Test;
@Test(groups="it_department")
https://www.javatpoint.com/testng-interview-questions 5/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
@Test(groups="it_department")
@Test(groups="it_department")
@Test (groups="hr")
System.out.print("I am hr");
testng.xml
<suite name="Suite">
https://www.javatpoint.com/testng-interview-questions 6/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
<groups>
<run>
<include name="it_department"/>
</run>
</groups>
<classes>
<class name="com.javatpoint.Test_methods"></class>
</classes>
</test>
When we want to run the test cases in a specific order, then we use the concept of
dependency in TestNG.
dependsOnMethods
The dependsOnMethods attribute tells the TestNG on which methods this test will
be dependent on, so that those methods will be executed before this test method.
package com.javatpoint;
import org.testng.annotations.Test;
https://www.javatpoint.com/testng-interview-questions 7/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
@Test
System.out.println("Login page");
@Test(dependsOnMethods="login")
System.out.println("Home page");
dependsOnGroups
It is similar to the dependsOnMethods attribute. It allows the test methods to
depend on the group of test methods. It executes the group of test methods
before the dependent test method.
package com.javatpoint;
import org.testng.annotations.Test;
@Test(groups="test")
System.out.println("testcase1");
https://www.javatpoint.com/testng-interview-questions 8/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
@Test(groups="test")
System.out.println("testcase2");
@Test(dependsOnGroups="test")
System.out.println("testcase3");
While running test cases, there can be a case when some test cases take much more
time than expected. In such a case, we can mark the test case as a failed test case by
using timeOut.
TimeOut in TestNG allows you to configure the time period to wait for a test to get
completely executed. It can be configured in two levels:
https://www.javatpoint.com/testng-interview-questions 9/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
The above @Test annotation tells that the test method will be given 700 ms to complete
its execution otherwise it will be marked as a failed test case.
An invocationCount in TestNG is the number of times that we want to execute the same
test.
package com.javatpoint;
import org.testng.annotations.Test;
@Test(invocationCount=5)
System.out.println("testcase1");
Output
https://www.javatpoint.com/testng-interview-questions 10/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
It allows you to group the test cases and can be executed as per the
requirements.
It allows you to integrate the TestNG framework with tools such as Jenkins.
12) How to pass the parameter in test case through testng.xml file?
We can also pass the value to test methods at runtime, we can achieve this by sending
parameter values through the testng.xml file. We can use the @Parameter annotation:
@Parameter("param-name");
package com.javatpoint;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;
@Parameters({"text"})
@Test
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
driver.get("http://www.google.com/");
driver.findElement(By.name("q")).sendKeys("javatpoint tutorial");
testng.xml file
<suite name="Suite">
https://www.javatpoint.com/testng-interview-questions 12/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
<classes>
<class name="com.javatpoint.Web"></class>
</classes>
</test>
https://www.javatpoint.com/testng-interview-questions 13/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
https://www.javatpoint.com/testng-interview-questions 14/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
We can disable the test case from running by using the enabled attribute. We can assign
the false value to the enabled attribute, in this way we can disable the test case from
running.
package com.javatpoint;
import org.testng.annotations.Test;
@Test(enabled=false)
System.out.println("testcase1");
@Test
System.out.println("testcase2");
https://www.javatpoint.com/testng-interview-questions 15/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
Soft Assertion: In case of Soft Assertion, if TestNG gets an error during @Test, it will
throw an exception when an assertion fails and continues with the next statement after
the assert statement.
Hard Assertion: In the case of Hard Assertion, if TestNG gets an error during @Test, it
will throw an AssertException immediately when an assertion fails and stops execution
after the assert statement.
package com.javatpoint;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
@Test
soft_assert.assertTrue(false);
System.out.println("soft assertion");
https://www.javatpoint.com/testng-interview-questions 16/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
@Test
Assert.assertTrue(false);
System.out.println("hard assertion");
Output
TestNG provides different kinds of listeners which can perform different actions whenever
the event is triggered. The most widely used listener in TestNG is ITestListener interface.
The ITestListener interface contains methods such as onTestSuccess, onTestfailure,
onTestSkipped, etc.
https://www.javatpoint.com/testng-interview-questions 17/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
If the test case is failed, then what action should be performed by the listener.
If the test case is passed, then what action should be performed by the listener.
If the test case is skipped, then what action should be performed by the listener.
package com.javatpoint;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(com.javatpoint.Listener.class)
@Test
Assert.assertTrue(true);
@Test
Assert.assertTrue(false);
https://www.javatpoint.com/testng-interview-questions 18/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
Listener.java
package com.javatpoint;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
@Override
@Override
@Override
@Override
https://www.javatpoint.com/testng-interview-questions 19/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
@Override
@Override
@Override
}}
Output
https://www.javatpoint.com/testng-interview-questions 20/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
The @Factory annotation is useful when we want to run multiple test cases through a
single test class. It is mainly used for the dynamic execution of test cases.
testcase1.java
package com.javatpoint;
import org.testng.annotations.Test;
@Test
System.out.println("testcase 1");
https://www.javatpoint.com/testng-interview-questions 21/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
testcase2.java
package com.javatpoint;
import org.testng.annotations.Test;
@Test
System.out.println("testcase 2");
Factory.java
import org.testng.annotations.Factory;
@Factory
tests[0]=new Testcase1();
https://www.javatpoint.com/testng-interview-questions 22/24
3/6/2021 Top 17 TestNG Interview Questions - javatpoint
tests[1]=new Testcase2();
return tests;
@Factory: It is annotation used by the TestNG to execute the test methods present in
the same test class using different instances of the respective class.
https://www.javatpoint.com/testng-interview-questions 23/24