Extremely Important 43+ list of TestNg Interview Questions and Answers

In this tutorial we are going to discuss the exhaustive sets of Critical TestNg interview questions and answers and distributed depending on the difficulty level, where you can better equip yourself quickly on TestNg and should be able to clear any TestNg Interview Questions and answers rounds.

These Sets of Testng interview questions are distributed in the following modules or set :

TestNg Interview Questions and answers
TestNg Interview Questions and answers

TestNg Interview Questions – Basic

TestNg Interview Questions – Intermediate

TestNg Interview Questions – Advance

Testng Interview Questions and Answers || Set 1

What is TestNG?

TestNg basically represents “Testing Next Generation” is a unit testing framework that controls the flow and order of test automation and automation scripts by providing various annotations with their functionalities.

What are the advantages of TestNg?

            A. Through Testng’s various annotations, you can control the flow and order of automation and Test execution in a better approach.

            B. Test Classes or Test script Methods parallel execution can be achieved with TestNg.

           C. TestNg can be easily integrated with different build tools such as Maven, Gradle. Also, it can be integrated with CICD tools such as Jenkins.

           D. TestNG provide details HTML reporting feature and easily integrated with other Test reporting platform such as Allure, Extent Report with features of TestNG Listeners.

          E. All the tests can be triggered by the testng.xml file where you can mention the Test class/Test/Test Package name to be run.

          F. Data driven Testing can be done with the TestNg DataProvider annotation. Also, parameterization Tests can be done through Testng.xml as well, such as while performing cross browser testing, you can parameterize the different browsers for different tests. This feature helps to build the Data Driven Framework with TestNG.

         G. TestNg Provides a way to include/exclude a set of a test from tesngNg.xml with include and exclude attribute.

         H. With TestNg, you can group your tests and dependency injection in between the tests.

         I. TestNg provides many listeners with those you can achieve a lot of things like you can do custom reporting(IReporter), integration with different tools(ITestListener), Change the behavior of TestNG Test annotation in runtime with IAnnotationTransformer and many more.

        J. You can skip the specific test, prioritize your test order, create a time-bound test with TestNg Test annotations.

        K. You can use Hard Assertion as well as Soft Assertion with TestNg for writing Assert statement.

        L. TestNg generates TestNG-failed.xml after each Test execution, so you can you the same generated TestNG-failed.xml to rerun your failed test scripts.

       M. TestNg provides various Test annotation such as @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest.@BeforeSuite,@AfterSuite.

       N. You can run the expected exception Test with TestNg.

       O. You can rerun the failed test with IretryAnalyzer of Testng 

How do you trigger and execute the TestNg test Script?

You can run TestNg Test script in several ways : 

      A. Right Click on Test Class and “run as” and select the option of “TestNg Test.”

      B. Create testng.xml and right on the file and run the xml file.

      C. If you integrate testNg.xml with the build tool such as Maven/Gradle, then you can run from maven or Gradle as well.

      D. If the build tool such as Maven/Gradle is integrated with CICD, then you can run from CICD, i.e., from Jenkins.

State the Testng annotations that are available ?

Majorly used Testng Test annotations are :

@BeforeSuite

@AfterSuite

@BeforeTest

@AfterTest

@BeforeClass

@AfterClass

@BeforeMethod

@AfterMethod

@BeforeGroups

@AfterGroups

@Test

Mention the TestNg annotations execution sequence?

From the Test execution standpoint here is the below sequence for all the available TestNg annotations :

Precondition Annotations :

@BeforeSuite

@BeforeTest

@BeforeClass

@BeforeMethod

Test Annotations :

@Test

PostCondition Annotations: 

@AfterSuite

@AfterTest

@AfterClass

@AfterMethod

How to disable test execution for a test script?

You can use enabled attribute is equals to false in the @Test annotation attribute like mentioned below :

@Test(enabled = false)
public void logout(){
   System.out.println("Sample Test");
}

How can you specify listeners in TestNG xml?

You can use Tesng xml file for mentioning the listeners to be used as part the test script executions, in the below approach:

<suite>

<listeners>

        <listener class-name=”org.uncommons.reportng.HTMLReporter” />

        <listener class-name=”org.uncommons.reportng.JUnitXMLReporter” />

</listeners>

What is the Timeout Test in TestNg?

In this case, the “timeout test” means, if in case the test script takes longer than the specified time period to execute, then testng will abort the test and will mark as a failed test.

@Test(timeOut = 6000) // this time in mulliseconds
public void testShouldPass() throws InterruptedException {
   Thread.sleep(2000);
}

How to achieve the Expected Exception Test with TestNG?

If a Test method throws an exception, which is the same as specified as part of the test annotation expectedExceptions attribute, then TestNg would mark the test as passed.

@Test(expectedExceptions = ArithmeticException.class)
public void TestDivision() {
   int i = 1 / 0;
}

 The above Test method will be passed as it throws the exception expected by TestNG.

What is the difference between @BeforeTest and #@BeforeMethod annotation?

@BeforeTest is executed once before each of the testng <test> tag mentioned in the testng.xml file 

@BeforeMethod is executed before each and every test script method.

What is the advantage of using the testng.xml file?

With the testng.xml file, you can control the flow of execution with single test suite or multiple test suite in a single testng xml file. Some of the important features are :

A. testng.xml file allows us to exclude and include test methods and test group execution.

B. You can pass test data/parameters through testng.xml.

C. You can add dependency between test methods and also a group of test methods

D. You can prioritize the test cases.

E. Parallel test execution of test cases is achieved.

F. You can implement different testng listeners and use them by mentioning those in the testng.xml.

G. If you run your suite with testng.xml, then you will only fail the test for the next iteration by using TestNG-failed.xml is generated after every execution.

H. You can run specific groups of tests using <groups>tag of TestNG xml.

How many types of dependencies can you achieve by using TestNG?

There are two types of dependencies we can achieve with TestNG : 

A. DependsOnMethods : 

By using this dependsOnMethods attribute, you are defining which test method will be dependent on other test methods, So if the depending method is failed or not run, then the dependent test method also will not run.

@Test
public void loginUserProfile() {
   System.out.println("Login user ");
}
@Test(dependsOnMethods = "loginUserProfile")
public void logOutPage_user() {
   System.out.println("Logout page for User");
}

 Here is logOutPage_user test method that will run after the successful execution of the loginUserProfile test.

B. dependsOnGroups : 

In this type of test dependency, It allows us to put the dependency injection for the test methods with a group of test methods.

The flow of execution happens in such a way ie the testGroup first gets triggered and executed and then the dependent test method gets triggered, and once after successful completion of the group test, the dependent test method will get executed.

@Test(groups="AtestGroupName")
public void testcaseOne()
{
   System.out.println("testcaseOne in process");
}
@Test(groups="AtestGroupName")
public void testcaseTwo()
{
   System.out.println("testcaseTwo in process");
}
@Test(dependsOnGroups="AtestGroupName")
public void testcaseThree()
{
   System.out.println("testcaseThree in process");
}

Testng Interview Questions and Answers || Set 2

What is meant by invocationCount in TestNG?

invocationCount is a test annotation attribute by which you can define the number of iteration the test method will be executed in a single execution. 

@Test(invocationCount = 2)
public void testOfInvCount() {
   System.out.println("Invocation count test in progress");
}

 The above test will execute two times as invocationCount is mentioned as 2.

What are listeners in TestNG?

in TestNg the listeners are basically interface in Java which you need to implement in your class. The implemented class will keep on listening to certain events and executes the specific block of code associated with that event.Here when you implement the interface you ultimately implement the unimplemented methods and those block of code or the methods will get executed as and when specific event gets triggered. 

With the help of TestNG listeners, we can perform a lot of run time actions by listening to a different event triggered by the test script execution and their status, or we can do reporting. Also, we can change the implementation of TestNg annotation.

Mention the differences between @Factory and @Dataprovider annotations in TestNg?

@Dataprovider: When you want to execute the same test, but with different diverse sets of data in every run, you can use the dataprovider annotation, and this you can achieve the datadriven testing approach. Here the test method execution happens using the same class instance to which the test method belongs.

@Factory: This will be executed all the test methods present inside a test class using separate and multiple instances of the class.

How to use TestNG Reporter Class for the log generation?

You can log the details and data using the Reporter class, and these logs will be captured by the report generated by TestNG

Reporter.log(” Logging message “);

How to do exception handling in TestNG?

You can mention the type of expected exception in an attribute called expectedExceptions with the @test annotation; in this case, then TestNg would mark the test as passed.

@Test(expectedExceptions = numberFormatException.class)

How to achieve dependency injection via TestNg XML ?

TestNG allows us to inject dependencies between different groups of tests via the TestNG XML file. Through which we can have the dependence of one group onto another.

What are the various assertion approaches for TestNG ?

We can use two types of assertions with TestNg. 

Soft Asserts

Hard Asserts 

Mention some of the commonly used assertions with TestNG 

Some of the widely used assertion methods in TestNG :

  • assertEquals(boolean actual,boolean expected)
  • assertEqual(String actual,String expected)
  • assertEqual(String actual Result,String expected Result , String message)
  • assertTrue(condition)
  • assertFalse(condition)
  • assertFalse(condition, message)
  • assertTrue(condition, message)

What do you understand by the asserts in TestNG?

An assertion is performed to validate the actual test results with respect to the expected test results. In TestNg, we can use hard assert of soft assert. 

Difference between Hard assert and soft assert in TestNg

While working with the Hard assert, If in case we get any failure in the assertion condition then the subsequent test steps will not be executed and would be aborted, and eventually the test will be marked as a failed test case. 

While on the other hand the Soft Assert takes into considering of validating all the assertion points even if there are any failures in any of the assertions . That means the test execution does not get aborted even if one assertion fails.

How to write soft assertion in TestNg 

The below piece of code gives the approach of writing the soft assertion in TestNG

 @Test
   public void assertion() {
   SoftAssert softAssertion = new SoftAssert();
   //Assertion 1 
   softAssertion.assertEquals("exp", "act");
   //Assertion 2 
   softAssertion.assertEquals(123, 123);
   //Assertion 3 
   softAssertion.assertEquals("actual Value", "expected value");
   // At the end considering all the assertion values
   softAssertion.assertAll();
}

How to use regular expression in TestNG groups?

A regular expression can be used in TestNG to execute the groups which have a similar pattern in their naming. 

For example, if you want to run all the groups starting with “testX” as per the name is concerned, then you can use the regular expression as testX.* in the TestNG XML file.

Testng Interview Questions and Answers || Set 3

How do you exclude a group from the test execution cycle?

You can use exclude tag to exclude a group of test case from executing in the below manner in Testng xml file 

<groups>

    <run>

        <exclude name = “excludeDummy”>

        </exclude>

    </run>

</groups>

What are the types of reports generated in TestNG by default?

TestNG generates two kinds of reports after the execution, which are :

  • Emailable Reports
  • Index Reports

Mention the difference between the TestNG test suite and the TestNG test?

TestNG test suite is the collection of test classes and test methods that can be run simultaneously as well as parallelly from the TestNG XML file. 

On the other hand, the TestNG test method is a single test case file or test method.

What is the use of threadPoolSize attribute with the @Test annotation 

Through The threadPoolSize attribute we can define a thread pool with the specific mentioned size by the number for the testmethod to be executed via multiple available threads.

The attribute is being ignored if the invocationCount is not being mentioned.

 @Test(threadPoolSize = 3) 
   public void testCaseOne(){
      System.out.println("testCaseOne in process");
   }

In the above test method,testCaseOne will be invoked from the three different threads.

What does alwaysRun attributes do?

This alwaysRun annotation attribute is used whenever you want to execute the test method irrespective of the dependent parameters on which the test method depends, fails. If you set to true then you need to set the attribute is true.

What are the different listeners that are available?

ITestListener

IReporter 

ISuiteListener

IAnnotationTransformer 

IAnnotationTransformer2

IHookable 

IInvokedMethodListener 

IMethodInterceptor 

What is default value for the TestNG Priority?

The TestNG priority has the default value is zero.

How to re-run TestNg Failed Tests using Auto Retry mechanism ?

TestNg provides one interface called as IRetryAnalyzer listener which you can implement the interface auto re-run your Failed Test scripts by mentioning the class in the testNg.XML file , Here is the below code for implementing the same :

TestNg Auto Retry Failed Tests
Testng interview questions and answers- TestNg Retry Test Script

In the above area you can configure the number of re-try ,maximum counts and also you can mention in which all exceptions you want to re-run Test scripts.

Approach Two : How to re-run TestNg Failed Tests using Auto Retry mechanism

In the below approach you can build 2 classes ie one is Retry class where you can keep the logic of controlling the number of iteration in case of test failures which will implement the interface Testng IRetryAnalyzer.

Another class is basically which will implement the another interface listener IAnnotationTransformer and implement the method transform which internally interacts with the previous class (ie Retry class)

public class Retry implements IRetryAnalyzer {

    int retryCounter = 0;
    
    // The maximum number of failed execution 
    int autoRetryLimit = 2;

    @Override
    public boolean retry(ITestResult iTestResult) {
        if (retryCounter < autoRetryLimit) {
            retryCounter++;
            return true;
        }
        return false;
    }
}

public class CognitiveRetry implements IAnnotationTransformer {


    public void transform(ITestAnnotation annotation, Class testClass, Constructor testCons, Method tMethod){
        IRetryAnalyzer retryAnalyzer=annotation.getRetryAnalyzer();
        if(retryAnalyzer==null){
            annotation.setRetryAnalyzer(Retry.class);
        }
    }
}

and finally add the CognitiveRetry class in the listener for testng.xml file .

<listeners>
    <listener class-name= "com.lambdageeks.cognitiveRetryUtils.CognitiveRetry"/>
</listeners>

How to achieve TestNG itestlistener implementation?

ITestListener is an interface in TestNg which has multiple methods(unimplemented since its an interface) which can be implemented by a class . Each method represents specific functionalities or scenarios , hence depending on your need you can implement those methods .

For an example onTestFailure is a method which you can implement where you want to perform any operations while any test method gets failed , lets say you want to capture the screenshot while in case of any test method failures , so you can write the takescreenshot method inside the onTestFailure , and as the ITestListener is an interface hence testNg will keep on listening on the events (test failures) and whenever there is test failures your screenshot will get captured .

Here is the implementation of capturing screenshot whenever you test script encounters a failures :

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

public class CustomListerners implements ITestListener {

    WebDriver driver=null;
    String filePath = "D:\\LambdaGeeks\\SCREENSHOTS";
    @Override
    public void onTestFailure(ITestResult result) {
        String testMethodName=String.valueOf(result.getName()).trim();
        ITestContext testContext = result.getTestContext();
        WebDriver driver = (WebDriver)testContext.getAttribute("driver");
        captureTheScreenShot(testMethodName, driver);
    }

    public void captureTheScreenShot(String methodName, WebDriver driver) {
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        /*
        Each screenshots will get saved with along with the test Name to have better correlation
         */
        try {
            FileUtils.copyFile(scrFile, new File(filePath+methodName+".jpg"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void onFinish(ITestContext context) {}

    public void onTestStart(ITestResult result) {   }

    public void onTestSuccess(ITestResult result) {   }

    public void onTestSkipped(ITestResult result) {   }

    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {   }

    public void onStart(ITestContext context) {   }

}

And also you need to add this class in listener tag in testng.xml file like we had done in previous question.

How to Implement testng iAnnotationtransformer ?

TestNg provides an interface named as IAnnotationTransformer that provides a method called  “transform” which you can implement and would be triggered in runtime by TestNG , this implementation is used to modify the test annotation behavior of the test class and test methods

In the below segment we would see how we could do that

public class TestClassAnnotations {

    @Test(alwaysRun = true,dependsOnMethods = "testMethodB")
    public void testMethodA() {
        System.out.println("--- Customizing the runtime behavious with ITestAnnotation ---");
    }

    @Test
    public void testMethodB() {
        System.out.println("--- Second TestMethods ---");
        Assert.fail();
    }

}

By default if we run the above code then only one method will get executed which is testMethodA and another method testMethodB will fail because we are intentionally failing this by usinig the Assert.fail() method.

But if we change the Alwaysrun=true annotation to false by using the IAnnotationTransformer then this method will not get executed , below is the code snippet on how to implement the IAnnotationTransformer and use it in the testing.xml to change the behavious of the TestNG annotation

The implementation of the CustomAnnotationTransformers goes here :

public class CustomAnnotationTransformers implements IAnnotationTransformer {

    public boolean isTestRunning(ITestAnnotation iTestAnnotation) {
        if (iTestAnnotation.getAlwaysRun()) {
            return true;
        }
        return false;
    }


    public void transform(ITestAnnotation annotations, Class testClasses, Constructor testConstructors, Method testMethods) {

        if (isTestRunning(annotations)) {
            annotations.setEnabled(false);

        }

    }

}

Here is the listener we need to add in testing.xml file

<listeners>
    <listener class-name= "com.lambdageeks.CustomAnnotationTransformers"/>
</listeners>

How to implement testng iinvokedmethodlistener?

If you want to implement a feature where a some certain method will gets executed before and after each and every Test method of TestNg then that feature could be implemented by the testng IInvokedMethodListener listener.

 

Here is the code snippet to implement the features :

package com.lambdageeks;

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;


public class CustomAnnotationTransformers implements IInvokedMethodListener {

    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {

        System.out.println(" ::: Before Method from IInvokedMethodListener is Triggered for the Test Method named as :  " + method.getTestMethod().getMethodName() + " ::: ");
    }

    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {

        System.out.println(" :::: After Method from IInvokedMethodListener is Triggered for the Test Method named as :  " + method.getTestMethod().getMethodName() + " ::: ");

    }
}

Here is the Test Class for testing the feature :

public class TestClassAnnotations {

    @Test(alwaysRun = true)
    public void testMethoddummy() {
        System.out.println("--- This is a test Method , testing the feature of IInvokedMethodListener Testng Listener  ---");

    }

}

You have to Mention the TestNG iinvokedmethodlistener in the listener tag in the testng.xml like always

<listeners>
    <listener class-name="com.lambdageeks.CustomAnnotationTransformers"/>
</listeners>

The output of the execution would look like this :

::: Before Method from IInvokedMethodListener is Triggered for the Test Method named as :  testMethoddummy :::

— This is a test Method , testing the feature of IInvokedMethodListener Testng Listener  —

 :::: After Method from IInvokedMethodListener is Triggered for the Test Method named as :  testMethoddummy :::

How to implement Data providers in TestNG?

We can implement the DataProvider using TestNg in the below approach :

public class DataProviderDemo {

    @DataProvider(name = "dpName")
    public Object[][] dataProvidersMethodName() {
        return new Object[][]{{"Lambda"}, {"Geeks"}};
    }

    @Test(dataProvider = "dpName")
    public void dataproviderDummyTestMethod(String dataValues) {
        System.out.println("The Data Params with data provider examples : : " + dataValues);
    }
}

If we don’t set the priority of the test method in which order the tests are executed in TestNG?

The tests are executed in the order of Alphabatical order of the TestmethodName..

 

Such as in the below code snippet :

public class SequenceTest {

    @Test()
    public void geeks() {
        System.out.println("Sequence Test , Method ran :geeks ");
    }

    @Test()
    public void lambda() {
        System.out.println("Sequence Test , Method ran : lambda ");
    }

    @Test()
    public void abc() {
        System.out.println("Sequence Test , Method ran :abc");
    }
}

The output would look like this :

Sequence Test , Method ran :abc

Sequence Test , Method ran :geeks

Sequence Test , Method ran : lambda

 

How to run your Test scripts in parallel ?

You can run your Test scripts using TestNg XML file by mentioning the parallel=”methods” thread-count=”2″, here 2 parallel cases will get executed , if you want to executed more threads in parallel.

<suite name="DummyTest" parallel="methods" thread-count="2" >

<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</suite>  

How to integrate TestNg with GRADLE build Tool?

You can run Testng Suite in gradle in different ways:

How to run TestNg Groups using Gradle : You can create a Task in build.gradle file can mention the useTestNG() and mention the below details while running the Test Groups.

TestNg Interview Questions and Answers-TestNg with Gradle

How to run Testng Default listener with Gradle to generate report using TestNg library

TestNg Interview Questions and Answers- Testng With Gradle Default Listeners
TestNg Interview Questions and Answers- Testng With Gradle Default Listeners

If you want to use the custom listener then you can mention that same in the following approach :

TestNg Interview Questions and Answers-TestNG Custom Listeners with Gradle
TestNg Interview Questions and Answers-TestNG Custom Listeners with Gradle

How to run Testng Runner xml file using command prompt ?

You can use TestNg Downloaded location and mention org.testng.TestNg.testNgRunner.xml to run the runner xml file from the command prompt.

java -cp "/opt/testng-7.1.jar:bin" org.testng.TestNG testngRunner.xml

How to Integrate TestNg XML with Maven ?

You can integrate TestNg with Maven with the use of Plugin called maven-surefire-plugin where you can configure to run the testNgrunner.xml file using the configurations :

TestNg Interview Questions and Answers-TestNg-Maven-Surefire Integration

How can you specify the TestNg Test parameter using TestNg and Maven ?

You can specify the Test parameter using Maven SureFire Plugin with TestNg.XML file in the below fashion

TestNg Interview Questions and Answers-Test Parameter

Conclusion : With this we conclude the list of all critical and important TestNg interview questions and answers , to get better grip on TestNg you can go through the exhaustive documentation on TestNg.

To learn more about the exhaustive Selenium tutorial you can visit here.

About Debarghya

Myself Debarghya Roy, I am an Engineering ARCHITECT working with fortune 5 company and an open source contributor, having around 12 years of experience/expertise in various Technology stack.
I have worked with various technologies such as Java,C#,Python,Groovy, UI Automation(Selenium), Mobile Automation(Appium), API/Backend Automation,Performance Engineering(JMeter, Locust), Security Automation(MobSF,OwAsp,Kali Linux, Astra,ZAP etc), RPA,Process Engineering Automation,Mainframe Automation,Back End Development with SpringBoot,Kafka,Redis,RabitMQ,ELK stack, GrayLog, Jenkins and also having experience in Cloud Technologies, DevOps etc.
I live in Bangalore ,India with my wife and have passion towards Blogging , music , playing guitar and my Philosophy of life is Education for All which gave the birth of LambdaGeeks. Lets connect over linked-in - https://www.linkedin.com/in/debarghya-roy/

Leave a Comment

Your email address will not be published. Required fields are marked *