TestNG Pitanja
TestNG Pitanja
TestNG is one of the most widely used testing frameworks for both unit and
automated testing. In this tutorial, we have compiled the top frequently asked
testNG interview questions and answers for both fresher and experienced
professionals.
If you want to learn about TestNG, you can also check our – TestNG Tutorial.
For Selenium-TestNG Interview Questions, check – Top Selenium Interview
Questions.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
1. TestNG provides different assertions that help in checking the expected
and actual results.
2. It provides parallel execution of test methods.
3. We can define the dependency of one test method over others in
TestNG.
4. We can assign priority to test methods in Selenium.
5. It allows grouping of test methods into test groups.
6. It allows data-driven testing using @DataProvider annotation.
7. It has inherent support for reporting.
8. It has support for parameterizing test cases using @Parameters
annotation.
Ques.5. How can we group test cases like separate test cases for Sanity
suite, Regression suite, etc?
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Ans. Using groups attribute in TestNG, we can assign the test methods to
different groups.
Ques.6. How can we exclude a Test method from getting executed via
testng.xml file?
Ans. Using the exclude tag in testng.xml file, we can exclude a particular test
method from getting executed.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
@BeforeSuite – The annotated method will run only once before all tests
in this suite have run.
@AfterSuite – The annotated method will run only once after all tests in
this suite have run.
@BeforeClass – The annotated method will run only once before the first
test method in the current class is invoked.
@AfterClass – The annotated method will run only once after all the test
methods in the current class have been run.
@BeforeTest – The annotated method will run before any test method
belonging to the classes inside the <test> tag is run.
@AfterTest – The annotated method will run after all the test methods
belonging to the classes inside the <test> tag have run.
@BeforeMethod – The annotated method will run before each test
method marked by @Test annotation.
@AfterMethod – The annotated method will run after each test method
marked by @Test annotation.
@DataProvider – The @DataProvider annotation is used to pass test
data to the test method. The test method will run as per the number of
rows of data passed via the data provider method.
Ques.8. What is the order of execution of the test method based on the
different annotations?
Ans. The test methods in TestNG follow the Suite->Test->Class->Method
sequence combined with the Before annotions->Test annotations->After
annotations sequence. So, the order of execution is-
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
@AfterTest
@AfterSuite
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Ques.11. How can we make one test method dependent on others using
TestNG?
Ans. Using dependsOnMethods parameter inside @Test annotation in TestNG
we can make one test method run only after the successful execution of the
dependent test method.
@Test(dependsOnMethods = { "preTests" })
@Test(priority=1)
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
@Test(invocationCount = 10)
public void invocationCountTest(){
//Test logic
}
Ques.16. What is the difference between soft assertion and hard assertion
in TestNG?
Ans. Soft assertions (SoftAssert) allows us to have multiple assertions within a
test method, even when an assertion fails the test method continues with the
remaining test execution. The result of all the assertions can be collated at the
end using softAssert.assertAll() method.
@Test
public void softAssertionTest(){
SoftAssert softAssert= new SoftAssert();
//Assertion failing
softAssert.fail();
System.out.println("Failing");
//Assertion passing
softAssert.assertEquals(1, 1);
System.out.println("Passing");
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
//Collates test results and marks them pass or fail
softAssert.assertAll();
}
Here, even though the first assertion fails still the test will continue with
execution and print the message below the second assertion.
Hard assertions on the other hand are the usual assertions provided by TestNG.
In case of hard assertion in case of any failure, the test execution stops,
preventing the execution of any further steps within the test method.
@Test(timeOut = 1000)
public void timeOutTest() throws InterruptedException {
//Sleep for 2sec so that test will fail
Thread.sleep(2000);
System.out.println("Will throw Timeout exception!");
}
@Test
public void testMethod(){
if(conditionToCheckForSkippingTest)
throw new SkipException("Skipping the test");
//test logic
}
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Ques.19. How can we make sure a test method runs even if the test
methods or groups on which it depends fail or get skipped?
Ans. Using “alwaysRun” attribute of @Test annotation, we can make sure the
test method will run even if the test methods or groups on which it depends fail
or get skipped.
@Test
public void parentTest() {
Assert.fail("Failed test");
}
@Test(dependsOnMethods={"parentTest"}, alwaysRun=true)
public void dependentTest() {
System.out.println("Running even if parent test failed");
}
Here, even though the parentTest failed, the dependentTest will not get skipped
instead it will executed because of “alwaysRun=true”. In case, we remove the
“alwaysRun=true” attribute from @Test then the report will show one failure and
one skipped test, without trying to run the dependentTest method.
Ques.20. How can we pass the parameter to test script using TestNG?
Ans. Using @Parameter annotation and ‘parameter’ tag in testng.xml we can
pass parameters to test scripts.
Sample testng.xml –
<suite name="sampleTestSuite">
<test name="sampleTest">
<parameter name="sampleParamName" value="sampleParamValue"/>
<classes>
<class name="TestFile" />
</classes>
</test>
</suite>
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Sample test script-
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
testNG is ITestListener interface. It has methods like onTestSuccess,
onTestFailure, onTestSkipped etc. We need to implement this interface creating
a listener class of our own. After that using the @Listener annotation we can
use specify that for a particular test class our customized listener class should
be used.
@Listeners(PackageName.CustomizedListenerClassName.class)
public class TestClass {
WebDriver driver= new FirefoxDriver();@Test
public void testMethod(){
//test logic
}
}
//Constructor
public TestClass(String str) {
this.str = str;
}
@Test
public void TestMethod() {
System.out.println(str);
}
}
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
public class TestFactory{
//The test methods in class TestClass will run twice with data "k1" and "k2"
@Factory
public Object[] factoryMethod() {
return new Object[] { new TestClass("K1"), new TestClass("k2") };
}
}
parallel=”{methods/tests/classes}”
thread-count=”{number of thread you want to run simultaneously}”.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Kuldeep Rana
Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in
test automation, performance testing, big data, and CI-CD. He brings
his decade of experience to his current role where he is dedicated to
educating the QA professionals. You can connect with him on LinkedIn.
Interview
Shruti Bhatt
April 27, 2020 at 11:32 am
Reply
Kuldeep Rana
April 27, 2020 at 12:19 pm
Reply
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Vaijayanti Kulkarni
October 20, 2020 at 10:37 pm
Reply
Kuldeep Rana
October 21, 2020 at 9:15 am
Thanks Vaijayanti 🙂
Reply
Nikita
October 21, 2020 at 4:13 am
Reply
Kuldeep Rana
October 21, 2020 at 9:14 am
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Thanks for your feedback 🙂
Reply
Nithya
February 1, 2021 at 7:39 pm
Reply
Mohammad Aafaque
April 12, 2021 at 11:59 pm
Excellent collection!!!
Reply
Samson Edward
July 26, 2021 at 1:43 pm
Excellent Collection nice to have in one website have all the topics are
covered
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Reply
Saimadhuri
October 29, 2021 at 6:00 pm
Superb content. Thanks for covering all the concepts at one place.
Reply
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com