Blog

Top-20-TestNG-Interview questions for SDET

Top 20 TestNG Interview Questions for SDET

Share:

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)

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.

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

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() {
return new 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();
@Test
public 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.:

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

More SDET Interview Question by Devlabs Alliance

  1. TOP 20 C# PROGRAMMING INTERVIEW QUESTIONS FOR SDET
  2. TOP 20 JUNIT INTERVIEW QUESTIONS FOR SDET
  3. TOP 50 SELENIUM INTERVIEW QUESTIONS FOR SDET
  4. TOP 50 GIT INTERVIEW QUESTIONS FOR SDET
  5. TOP 20 JAVA PROGRAMMING INTERVIEW QUESTIONS FOR SDET
  6. TOP 20 CUCUMBER INTERVIEW QUESTIONS FOR SDET
  7. TOP 20 BASIC JAVA INTERVIEW QUESTIONS FOR SDET
  8. TOP 20 SOFTWARE TESTING INTERVIEW QUESTIONS FOR SDET
Related Articles
Categories
Latest Articles

Popular Courses

Google Reviews
  • review rating 4  Both trainers (Ravi and Naresh) were excellent in their approach. Session was less of a theory and more of a practical. And even the theory part was quite interesting. A bit issue with the volume of information which I had received in just a span of 2-3 days - it seemed a bit over-loaded. But overall, it has given me a great platform to take it forward from here!

    thumb Varenya Varshney
    6/02/2019
  • review rating 4  It was good session, I left my training room with basic knowledge on all the tools. Tutors have great knowledge and experience on the tools. But three days is not enough time.

    thumb sairam shyamala
    6/30/2019
  • review rating 5  Training provided by the trainers here is completely awesome. Trainer is highly qualified in their areas with practical experience. Also amenities provided during training by institute is great

    thumb Abhishek Tripathi
    6/02/2019
  • review rating 5  Excellent Training program. Training courses and trainers are very good. Trainer will explain you as many time you ask the question. Best thing is hands on based training. You learn by doing. Looking for more courses from here.

    thumb Vijay Kumar
    6/30/2019
  • review rating 4  Training provided by the trainers in DevLabs Alliance is completely awesome. Trainers are highly qualified in their areas with practical experience. Since it is 3 days training we have completely got the opportunity to have hands on.

    thumb Salaja K
    6/30/2019
$('.enrollbtn').on('click', function (){ $( "#announcement" ).tabs( "option", "active", 0 ); $(' a[href="#announcement"]').trigger('click'); });