SlideShare a Scribd company logo
Top 20 TestNG Interview Questions
for SDET
By DevLabs Alliance
Visit us at:
www.devlabsalliance.com
Email:
training@devlabsalliance.com
Contact: +91 9717514555
TestNG Interview Questions for SDET
1. What is TestNG?
TestNG is a testing framework used for executing the unit tests in Java.
TestNG is an automated open source testing framework that can be integrated with
Selenium and capable of making Selenium tests easier to understand and provide multiple
capabilities like assertion, report generation, parallel test execution, etc.
It is inspired by JUnit. It has all the features of JUnit and has its own new features which
makes it more powerful.
Full form of TestNG is “Testing Next Generation”.
TestNG Interview Questions for SDET
2. What are the advantages of TestNG over JUnit?
The advantages of TestNG over JUnit are:
• TestNG Annotations are easier to use and understand.
• Test Cases in TestNG can be grouped more easily.
• TestNG provides feature to create and execute parallel tests.
• TestNG is used to create detailed HTML reports.
• TestNG allows to define dependency of one test method over other method.
• TestNG allows to assign priority to test cases.
TestNG Interview Questions for SDET
3. What is the use of testng.xml file?
Testng.xml is used for configuring the whole test suite. The various uses of TestNG are as
follows:
• All the tests in the test suite are triggered by testing.xml
• It is used to pass parameters to test scripts.
• It is used to support inclusion and exclusion of tests.
• It is used to create the test groups.
• It supports the parallel execution of test cases.
TestNG Interview Questions for SDET
4. What are the different annotations available in TestNG?
The different annotations of TestNG are:
• @BeforeTest
• @AfterTest
• @BeforeClass
• @AfterClass
• @BeforeMethod
• @AfterMethod
• @BeforeSuite
• @AfterSuite
• @BeforeGroups
• @AfterGroups
• @Test
TestNG Interview Questions for SDET
5. What is the sequence of execution of annotations in TestNG?
The sequence of execution of annotations is as follows:
• @BeforeSuite
• @BeforeTest
• @BeforeClass
• @BeforeMethod
• @Test
• @AfterMethod
• @AfterClass
• @AfterTest
• @AfterSuite
TestNG Interview Questions for SDET
6. How to create a xml file in TestNG?
To create xml file in TestNG, follow the following steps:
• Right click on Java project folder.
• Go to “New” and select “File” option.
• In New file wizard, specify file name as “testing.xml”.
• Click on Finish button.
This will add testing.xml file under Java project folder.
TestNG Interview Questions for SDET
7. What is a dependency in TestNG?
Dependency is used for some of the methods on which many methods are dependent on.
For eg.: For any application, if login page do not work, then it should not test the rest of
test scenarios.
In this case we would be using the LoginTest method on which other tests are dependent.
@Test(dependsOnMethods=“LoginTest”)
Public void SearchPage()
{
}
Since SearchPage is dependent on LoginTest method, so if LoginTest method fails, then
SearchPage method will not get executed.
TestNG Interview Questions for SDET
8. What is InvocationCount in TestNG?
InvocationCount is used to execute same test case multiple times.
For eg.:
@Test(invocationCount = 10)
Public void Login()
{
}
In this case, Login() method will execute 10 times.
TestNG Interview Questions for SDET
9. What is timeOut in TestNG?
If we want to terminate any method in the test script which is taking too much time to
execute, then we can use “timeOut” attributein TestNG.
Time is provided in miliseconds(ms)
For eg.:
@Test(timeOut = 2000)
Public void Login()
{
}
In this case, the Login() method will get terminated in 2000 ms (2 seconds) and the test
case gets Failed.
TestNG Interview Questions for SDET
10. What are common assertions in TestNG?
The common TestNG assertions are:
• Assert.assertEquals(string actual, string expected) :
If both the strings are equal, then only test case will pass.
• Assert.assertTrue(condition) :
It accepts a Boolean value. The assertion will pass if condition is True, otherwise it will
get fail.
• Assert.assertFalse(condition) :
It accepts a Boolean value. The assertion will pass if condition is False, otherwise it will
get fail.
TestNG Interview Questions for SDET
11. How a test can be disabled in TestNG?
To disable any test case in TestNG, we use “enabled” attribute.
For eg.:
@Test(enabled= “false”)
Public void LoginTest()
{
}
In this case, LoginTest() method will get disabled.
TestNG Interview Questions for SDET
12. What is assertion and what are the types of asserts in TestNG?
Assertion is used to validate the results of test cases.
There are two types of assertion:
• HardAssert: HardAssert is used to validate the test result. If hard assert fails, then none
of the code execution will take place after Assert statement.
Assert.assertEquals(actual value, expected value)
• Soft Assert: Soft Assert is also used to validate the test results but if soft assert fails,
then also execution of code will be continued for next statements.
To create a soft assert, object of “softAssert” class is created:
softAssert sAssert = new softAssert();
sAssert.assertAll();
TestNG Interview Questions for SDET
13. How to set test case priority in TestNG?
Priority attribute is used to set the order of execution of test cases.
If priority is not set then the test scripts are executed in alphabetical order.
For eg.:
Public class PriorityTestCase{
@Test(priority=0)
public void testCase1(){
system.out.println(“DLA Test Case 1”);
}
@Test(priority=1)
public void testCase2(){
system.out.println(“DLA Test Case 2”);
}
}
In this case, testCase1 will be executed first and then testCase2 will get execeuted.
TestNG Interview Questions for SDET
14. How can we pass parameter to test script using TestNG?
We can pass parameter to test scripts by using @Parameter annotation in test and
“parameter” tag in testing.xml.
Sample testing.xml:
<suite name = “dlaTestSuite”>
<test name = “dlaTest”>
<parameter name = “dlaParamName” value = “dlaParamValue”>
<classes>
<class name = “dlaTestFile” />
</classes> </test> </suite>
Sample Test Script:
public class dlaTestFile{
@Test
@Parameters(“dlaParamName”)
public void dlaParameterTest(string paramValue){
System.out.println(sampleParamName);
} }
TestNG Interview Questions for SDET
15. How can we create data driven framework using TestNG?
To create data driven framework, @DataProvider is used in which data is passed to the
associated test method and multiple iteration of tests run for different data values passed
from @DataProvider method.
For eg.:
@DataProvider(name = “dlaDataProvider”)
public Object[] [] dataProviderMethod() {
return new Object[] [] {{“dev”, “lab”}, {“devlabs”, “alliance”}};
}
@Test(dataProvider = “dlaDataProvider”)
public void dlaTest(string s1, string s2) {
system.out.println(s1 + “ “ + s2);
}
TestNG Interview Questions for SDET
16. What is the use of @Listener annotation in TestNG?
TestNG provides some kinds of listeners using which some actions can be performed in
case an event has triggered. Mostly TestNG listeners are used for configuration of reports
and logging.
The most widely used lisetner in TestNG is ITestListener interface. It contains methods like
onTestSuccess, onTestFailure, onTestSkipped etc. To implement this interface, we have to
create a listener class of our own. After that @Listener annotation is used to specify that
for a particular test class our customized listener class should be used.
For eg.:
@Listeners(PackageName.CustomizedListenerClassName.class)
public class dlaTestClass {
WebDriver driver = new FirefoxDriver();
@Test
public void dlaTestMethod(){
//test logic
}}
TestNG Interview Questions for SDET
17. What is the difference between @Factory and @DataProvider
annotation?
@Factory method creates instances of test class and run all the test methods in that class
with different set of data.
@DataProvider is bound to individual test methods and run the specific methods multiple
times.
TestNG Interview Questions for SDET
18. How can we run test cases in parallel using TestNG?
To run the tests in parallel in TestNG, we have to add these two key value pairs in suite-
• Parallel = "{methods/tests/classes}“
• thread-count= "{number of thread you want to run simultaneously}“
For eg.:
<suite name = “DLATestSuite” parallel = “methods” thread-count = “5”>
TestNG Interview Questions for SDET
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?
To run the test method even if test methods or groups on which it depends get fail or
skipped, we use “alwaysRun” attribute of @Test annotation.
For eg.:
@Test
public void parentTest() {
Assert.Fail(“Failed Test”);
}
@Test(dependsOnMethods = {“parentTest”}, alwaysRun = true)
Public void DependentTest() {
System.out.println(“Test DLA”);
}
TestNG Interview Questions for SDET
20. How to handle exceptions in TestNG?
To handle exception in methods we can mention the exception in @Test annotation so
that the test case does not fail.
For eg.: If a test method is expected to have “numberFormatException” exception, then
the test case will fail because of this exception if no try catch block is specified.
But this can be handled in TestNG by using “expectedException” attribute:
@Test(expectedException=numberFormatException.class)
After this the test case will run without failing.
Visit us at: www.devlabsalliance.com
Email: training@devlabsalliance.com
Contact: +91 9717514555

More Related Content

What's hot (20)

PPTX
Test ng tutorial
Srikrishna k
 
PPT
Junit and testNG
Марія Русин
 
PPTX
Introduction of TestNG framework and its benefits over Junit framework
BugRaptors
 
PDF
TestNG - The Next Generation of Unit Testing
Bethmi Gunasekara
 
PPT
testng
harithakannan
 
PPTX
TestNG vs JUnit: cease fire or the end of the war
Oleksiy Rezchykov
 
PDF
TestNG introduction
Denis Bazhin
 
PPTX
TestNG Data Binding
Matthias Rothe
 
PDF
Test ng for testers
Colombo Selenium Meetup
 
PPTX
Thread & concurrancy
Onkar Deshpande
 
PPTX
TestNG with selenium
Gousalya Ramachandran
 
PPT
Unit testing
Murugesan Nataraj
 
PDF
Unit testing best practices
nickokiss
 
PDF
Unit testing with Junit
Valerio Maggio
 
PDF
Unit testing with JUnit
Thomas Zimmermann
 
PPTX
JUNit Presentation
Animesh Kumar
 
PDF
IT Talk TestNG 6 vs JUnit 4
Andrey Oleynik
 
ODP
Test ng
fbenault
 
PPTX
Selenium TestNG
KadarkaraiSelvam
 
ODP
Testing In Java
David Noble
 
Test ng tutorial
Srikrishna k
 
Junit and testNG
Марія Русин
 
Introduction of TestNG framework and its benefits over Junit framework
BugRaptors
 
TestNG - The Next Generation of Unit Testing
Bethmi Gunasekara
 
TestNG vs JUnit: cease fire or the end of the war
Oleksiy Rezchykov
 
TestNG introduction
Denis Bazhin
 
TestNG Data Binding
Matthias Rothe
 
Test ng for testers
Colombo Selenium Meetup
 
Thread & concurrancy
Onkar Deshpande
 
TestNG with selenium
Gousalya Ramachandran
 
Unit testing
Murugesan Nataraj
 
Unit testing best practices
nickokiss
 
Unit testing with Junit
Valerio Maggio
 
Unit testing with JUnit
Thomas Zimmermann
 
JUNit Presentation
Animesh Kumar
 
IT Talk TestNG 6 vs JUnit 4
Andrey Oleynik
 
Test ng
fbenault
 
Selenium TestNG
KadarkaraiSelvam
 
Testing In Java
David Noble
 

Similar to Dev labs alliance top 20 testng interview questions for sdet (20)

PPTX
Top 20 Junit interview questions for sdet
DevLabs Alliance
 
PPTX
Appium TestNG Framework and Multi-Device Automation Execution
pCloudy
 
PPTX
Test ng
Ramakrishna kapa
 
PPTX
IT talk: Как я перестал бояться и полюбил TestNG
DataArt
 
PPTX
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance
 
PPTX
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance
 
PPTX
Dev labs alliance top 50 selenium interview questions for SDET
devlabsalliance
 
PDF
20070514 introduction to test ng and its application for test driven gui deve...
Will Shen
 
PPTX
TestNG Session presented in Xebia XKE
Abhishek Yadav
 
PDF
How To Install TestNG in Eclipse Step By Step Guide.pdf
pCloudy
 
PPTX
TestNG vs Junit
Büşra İçöz
 
PPT
Selenium training in chennai
Thecreating Experts
 
DOC
New selenium rc
girichinna27
 
PPTX
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Edureka!
 
PDF
TestNG Annotations in Selenium | Edureka
Edureka!
 
PDF
Guide To Running Parallel Test Cases in TestNG.pdf
kalichargn70th171
 
PPTX
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance
 
PPTX
Top 20 software testing interview questions for sdet
DevLabs Alliance
 
PPTX
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance
 
PDF
selenium_master.pdf
UdaytejaTiyyala1
 
Top 20 Junit interview questions for sdet
DevLabs Alliance
 
Appium TestNG Framework and Multi-Device Automation Execution
pCloudy
 
IT talk: Как я перестал бояться и полюбил TestNG
DataArt
 
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance
 
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance
 
Dev labs alliance top 50 selenium interview questions for SDET
devlabsalliance
 
20070514 introduction to test ng and its application for test driven gui deve...
Will Shen
 
TestNG Session presented in Xebia XKE
Abhishek Yadav
 
How To Install TestNG in Eclipse Step By Step Guide.pdf
pCloudy
 
TestNG vs Junit
Büşra İçöz
 
Selenium training in chennai
Thecreating Experts
 
New selenium rc
girichinna27
 
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Edureka!
 
TestNG Annotations in Selenium | Edureka
Edureka!
 
Guide To Running Parallel Test Cases in TestNG.pdf
kalichargn70th171
 
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance
 
Top 20 software testing interview questions for sdet
DevLabs Alliance
 
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance
 
selenium_master.pdf
UdaytejaTiyyala1
 
Ad

Recently uploaded (20)

PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
PPTX
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Ad

Dev labs alliance top 20 testng interview questions for sdet

  • 1. Top 20 TestNG Interview Questions for SDET By DevLabs Alliance Visit us at: www.devlabsalliance.com Email: [email protected] Contact: +91 9717514555
  • 2. TestNG Interview Questions for SDET 1. What is TestNG? TestNG is a testing framework used for executing the unit tests in Java. TestNG is an automated open source testing framework that can be integrated with Selenium and capable of making Selenium tests easier to understand and provide multiple capabilities like assertion, report generation, parallel test execution, etc. It is inspired by JUnit. It has all the features of JUnit and has its own new features which makes it more powerful. Full form of TestNG is “Testing Next Generation”.
  • 3. TestNG Interview Questions for SDET 2. What are the advantages of TestNG over JUnit? The advantages of TestNG over JUnit are: • TestNG Annotations are easier to use and understand. • Test Cases in TestNG can be grouped more easily. • TestNG provides feature to create and execute parallel tests. • TestNG is used to create detailed HTML reports. • TestNG allows to define dependency of one test method over other method. • TestNG allows to assign priority to test cases.
  • 4. TestNG Interview Questions for SDET 3. What is the use of testng.xml file? Testng.xml is used for configuring the whole test suite. The various uses of TestNG are as follows: • All the tests in the test suite are triggered by testing.xml • It is used to pass parameters to test scripts. • It is used to support inclusion and exclusion of tests. • It is used to create the test groups. • It supports the parallel execution of test cases.
  • 5. TestNG Interview Questions for SDET 4. What are the different annotations available in TestNG? The different annotations of TestNG are: • @BeforeTest • @AfterTest • @BeforeClass • @AfterClass • @BeforeMethod • @AfterMethod • @BeforeSuite • @AfterSuite • @BeforeGroups • @AfterGroups • @Test
  • 6. TestNG Interview Questions for SDET 5. What is the sequence of execution of annotations in TestNG? The sequence of execution of annotations is as follows: • @BeforeSuite • @BeforeTest • @BeforeClass • @BeforeMethod • @Test • @AfterMethod • @AfterClass • @AfterTest • @AfterSuite
  • 7. TestNG Interview Questions for SDET 6. How to create a xml file in TestNG? To create xml file in TestNG, follow the following steps: • Right click on Java project folder. • Go to “New” and select “File” option. • In New file wizard, specify file name as “testing.xml”. • Click on Finish button. This will add testing.xml file under Java project folder.
  • 8. TestNG Interview Questions for SDET 7. What is a dependency in TestNG? Dependency is used for some of the methods on which many methods are dependent on. For eg.: For any application, if login page do not work, then it should not test the rest of test scenarios. In this case we would be using the LoginTest method on which other tests are dependent. @Test(dependsOnMethods=“LoginTest”) Public void SearchPage() { } Since SearchPage is dependent on LoginTest method, so if LoginTest method fails, then SearchPage method will not get executed.
  • 9. TestNG Interview Questions for SDET 8. What is InvocationCount in TestNG? InvocationCount is used to execute same test case multiple times. For eg.: @Test(invocationCount = 10) Public void Login() { } In this case, Login() method will execute 10 times.
  • 10. TestNG Interview Questions for SDET 9. What is timeOut in TestNG? If we want to terminate any method in the test script which is taking too much time to execute, then we can use “timeOut” attributein TestNG. Time is provided in miliseconds(ms) For eg.: @Test(timeOut = 2000) Public void Login() { } In this case, the Login() method will get terminated in 2000 ms (2 seconds) and the test case gets Failed.
  • 11. TestNG Interview Questions for SDET 10. What are common assertions in TestNG? The common TestNG assertions are: • Assert.assertEquals(string actual, string expected) : If both the strings are equal, then only test case will pass. • Assert.assertTrue(condition) : It accepts a Boolean value. The assertion will pass if condition is True, otherwise it will get fail. • Assert.assertFalse(condition) : It accepts a Boolean value. The assertion will pass if condition is False, otherwise it will get fail.
  • 12. TestNG Interview Questions for SDET 11. How a test can be disabled in TestNG? To disable any test case in TestNG, we use “enabled” attribute. For eg.: @Test(enabled= “false”) Public void LoginTest() { } In this case, LoginTest() method will get disabled.
  • 13. TestNG Interview Questions for SDET 12. What is assertion and what are the types of asserts in TestNG? Assertion is used to validate the results of test cases. There are two types of assertion: • HardAssert: HardAssert is used to validate the test result. If hard assert fails, then none of the code execution will take place after Assert statement. Assert.assertEquals(actual value, expected value) • Soft Assert: Soft Assert is also used to validate the test results but if soft assert fails, then also execution of code will be continued for next statements. To create a soft assert, object of “softAssert” class is created: softAssert sAssert = new softAssert(); sAssert.assertAll();
  • 14. TestNG Interview Questions for SDET 13. How to set test case priority in TestNG? Priority attribute is used to set the order of execution of test cases. If priority is not set then the test scripts are executed in alphabetical order. For eg.: Public class PriorityTestCase{ @Test(priority=0) public void testCase1(){ system.out.println(“DLA Test Case 1”); } @Test(priority=1) public void testCase2(){ system.out.println(“DLA Test Case 2”); } } In this case, testCase1 will be executed first and then testCase2 will get execeuted.
  • 15. TestNG Interview Questions for SDET 14. How can we pass parameter to test script using TestNG? We can pass parameter to test scripts by using @Parameter annotation in test and “parameter” tag in testing.xml. Sample testing.xml: <suite name = “dlaTestSuite”> <test name = “dlaTest”> <parameter name = “dlaParamName” value = “dlaParamValue”> <classes> <class name = “dlaTestFile” /> </classes> </test> </suite> Sample Test Script: public class dlaTestFile{ @Test @Parameters(“dlaParamName”) public void dlaParameterTest(string paramValue){ System.out.println(sampleParamName); } }
  • 16. TestNG Interview Questions for SDET 15. How can we create data driven framework using TestNG? To create data driven framework, @DataProvider is used in which data is passed to the associated test method and multiple iteration of tests run for different data values passed from @DataProvider method. For eg.: @DataProvider(name = “dlaDataProvider”) public Object[] [] dataProviderMethod() { return new Object[] [] {{“dev”, “lab”}, {“devlabs”, “alliance”}}; } @Test(dataProvider = “dlaDataProvider”) public void dlaTest(string s1, string s2) { system.out.println(s1 + “ “ + s2); }
  • 17. TestNG Interview Questions for SDET 16. What is the use of @Listener annotation in TestNG? TestNG provides some kinds of listeners using which some actions can be performed in case an event has triggered. Mostly TestNG listeners are used for configuration of reports and logging. The most widely used lisetner in TestNG is ITestListener interface. It contains methods like onTestSuccess, onTestFailure, onTestSkipped etc. To implement this interface, we have to create a listener class of our own. After that @Listener annotation is used to specify that for a particular test class our customized listener class should be used. For eg.: @Listeners(PackageName.CustomizedListenerClassName.class) public class dlaTestClass { WebDriver driver = new FirefoxDriver(); @Test public void dlaTestMethod(){ //test logic }}
  • 18. TestNG Interview Questions for SDET 17. What is the difference between @Factory and @DataProvider annotation? @Factory method creates instances of test class and run all the test methods in that class with different set of data. @DataProvider is bound to individual test methods and run the specific methods multiple times.
  • 19. TestNG Interview Questions for SDET 18. How can we run test cases in parallel using TestNG? To run the tests in parallel in TestNG, we have to add these two key value pairs in suite- • Parallel = "{methods/tests/classes}“ • thread-count= "{number of thread you want to run simultaneously}“ For eg.: <suite name = “DLATestSuite” parallel = “methods” thread-count = “5”>
  • 20. TestNG Interview Questions for SDET 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? To run the test method even if test methods or groups on which it depends get fail or skipped, we use “alwaysRun” attribute of @Test annotation. For eg.: @Test public void parentTest() { Assert.Fail(“Failed Test”); } @Test(dependsOnMethods = {“parentTest”}, alwaysRun = true) Public void DependentTest() { System.out.println(“Test DLA”); }
  • 21. TestNG Interview Questions for SDET 20. How to handle exceptions in TestNG? To handle exception in methods we can mention the exception in @Test annotation so that the test case does not fail. For eg.: If a test method is expected to have “numberFormatException” exception, then the test case will fail because of this exception if no try catch block is specified. But this can be handled in TestNG by using “expectedException” attribute: @Test(expectedException=numberFormatException.class) After this the test case will run without failing.
  • 22. Visit us at: www.devlabsalliance.com Email: [email protected] Contact: +91 9717514555