DevLabs Alliance - WhatsApp
DevLabs Alliance Logo

Home / Interview /Top 20 TestNG Inter...

Top 20 TestNG Interview Questions for SDET – 2024

Admin

2023-09-07

DevLabs Alliance Interview

0 mins read

DevLabs Alliance Interview

Similar Blogs

How can a QA or Test engineer become SDET?

0 mins read

2023-09-12


Eclipse Cheat Sheet

0 mins read

2023-09-13


How to write a Good Test Case in Software Testing

0 mins read

2023-09-14


How to Prepare for SDET Interview

0 mins read

2024-03-14


Selenium Career Opportunities Why should you master Selenium WebDriver

0 mins read

2023-09-14


How to Handle Alerts and Pop-ups in Selenium?

0 mins read

2023-09-13


How To Use DataProvider in TestNG – Parameterization

0 mins read

2023-09-13


What Is TestNG Annotations-Benefits, Hierarchy

0 mins read

2023-09-13


How to Download & Install Selenium WebDriver in Eclipse (Step-By-Step)

0 mins read

2023-09-13


What Are The Skills Required for SDET?

0 mins read

2023-09-13


The following set of frequently asked interview questions have been compiled to help students and professionals preparing for Certified SDET professional courses, SDET Foundation Training, Certified SDET Architect courses. You can see the full set of SDET Interview questions here.


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 an 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 make it more powerful. Full form of TestNG is “Testing Next Generation”.



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 a feature to create and execute parallel tests.
  • TestNG is used to create detailed HTML reports.
  • TestNG allows defining dependency of one test method over other methods.
  • TestNG allows assigning priority to test cases.


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 the inclusion and exclusion of tests.
  • It is used to create test groups.
  • It supports the parallel execution of test cases.


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


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


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 the file name as “testing.xml”.
  • Click on Finish button.


This will add testing.xml file under Java project folder.



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 the login page does not work, then it should not test the rest of the 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.


8. What is InvocationCount in TestNG?


InvocationCount is used to execute the same test case multiple times.


For eg.:


@Test(invocationCount = 10)Public void Login()

{}

In this case, Login() method will execute 10 times.



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 milliseconds(ms)


@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.



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 the condition is True, otherwise, it will get fail.


Assert.assertFalse(condition) :

It accepts a Boolean value. The assertion will pass if the condition is False, otherwise, it will get fail.



11. How a test can be disabled in TestNG?


To disable any test case in TestNG, we use the “enabled” attribute.


For eg.:


@Test(enabled= “false”)
Public void LoginTest()
{
}


In this case, LoginTest() method will get disabled.



12. What is assertion and what are the types of asserts in TestNG?


The 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();



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 executed.



14. How can we pass parameter to test script using TestNG?


We can pass the 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);} }


15. How can we create data driven framework using TestNG?


To create a data-driven framework, @DataProvider is used in which data is passed to the associated test method and multiple iterations of tests run for different data values passed from @DataProvider method.


For eg.:


@DataProvider(name = “dlaDataProvider”)
public Object[] [] dataProviderMethod()
{returnnew Object[] [] {{“dev”, “lab”},
{“devlabs”, “alliance”}};}
@Test(dataProvider = “dlaDataProvider”)
public void dlaTest(string s1, string s2)
{system.out.println(s1 + “ “ + s2);}


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 listener 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();
@Testpublic void dlaTestMethod()
{//test logic}}



17. What is the difference between @Factory and @DataProvider annotation?


@Factory method creates instances of test class and runs all the test methods in that class with a different set of data.


@DataProvider is bound to individual test methods and run specific methods multiple times.



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 the 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”>



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.:


@Testpublic void parentTest()
{Assert.Fail(“Failed Test”);}
@Test(dependsOnMethods =
{“parentTest”}, alwaysRun = true)
Public void DependentTest()
{System.out.println(“Test DLA”);}



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.


Meet The Author

DevLabs Alliance Author

Admin


HOD Neoload


DevLabs Alliance TwitterDevLabs Alliance LinkedInDevLabs Alliance Instagram

Author Bio

DevLabs Alliance conducts career transformation workshops & training in Artificial Intelligence, Machine Learning, Deep Learning, Agile, DevOps, Big Data, Blockchain, Software Test Automation, Robotics Process Automation, and other cutting-edge technologies.

INQUIRY

Want To Know More


Email is valid



Phone


By tapping continuing, you agree to our Privacy Policy and Terms & Conditions

“ The hands-on projects helped our team put theory into practice. Thanks to this training, we've achieved seamless collaboration, faster releases, and a more resilient infrastructure. ”
DevLabs Alliance Blogs Page Review
Vijay Saxena

SkillAhead Solutions

Lets get started today!

and get that DREAM JOB

DevLabs Alliance Footer section
DevLabs Alliance LinkedIn ProfileDevLabs Alliance Twitter ProfileDevLabs Alliance Facebook ProfileDevLabs Alliance Facebook Profile
DevLabs Alliance Logo

Gurgaon

USA

1603, Capitol Avenue, Suite 413A, 2659, Cheyenne, WY 82001, USA

DevLabs Alliance ISO 9001

DevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer Section

`Copyright © DevLabs Alliance. All rights Reserved`

|

Refund & Reschedule Policy

Privacy Policy

Terms of Use