TestNG Interview Questions

1. What is TestNG?

TestNG is a testing framework for Java that provides a more powerful and flexible way to write and organize test cases compared to traditional testing frameworks like JUnit. It offers features like annotations, test configuration, parallel test execution, and detailed reporting.

2. What are the advantages of TestNG?

The advantages of TestNG include:

  • It supports annotations, which make it easy to write and maintain test cases.
  • It supports grouping, which allows you to run tests in a specific order or to exclude certain tests from execution.
  • It supports parametrization, which allows you to run the same test with different data.
  • It generates reports, which provide detailed information about the test execution.
  • It is extensible, which means that you can add your own custom annotations and listeners.

3. What are the advantages of using TestNG over JUnit?

TestNG offers several advantages over JUnit, including:

  • Native support for data-driven testing through data providers.
  • More flexible test configuration using TestNG XML files.
  • Easy grouping and prioritization of test cases using annotations.
  • Parallel test execution for faster testing.
  • Better reporting with detailed test results.

4. What are the different annotations in TestNG?

TestNG provides several annotations to control the test execution flow. Some commonly used annotations are:

  • @Test: Marks a method as a test method.
  • @BeforeTest: Runs before the test method.
  • @AfterTest: Runs after the test method.
  • @BeforeSuite: Runs before all tests in a suite.
  • @AfterSuite: Runs after all tests in a suite.
  • @DataProvider: Supplies data to test methods for data-driven testing.

5. How do you group test cases in TestNG?

TestNG allows you to group test cases using the "groups" attribute in the @Test annotation. You can create groups and add test methods to those groups. This helps in running specific groups of test cases or excluding certain groups from the test run.

6. How do you prioritize test cases in TestNG?

TestNG allows you to prioritize test cases using the "priority" attribute in the @Test annotation. Test methods with lower priority values are executed first, followed by those with higher priority values.

7. What is the use of data providers in TestNG?

Data providers in TestNG enable data-driven testing, where a test method can be executed multiple times with different sets of data. Data providers supply test data to test methods and help in testing different scenarios using the same test method.

8. How do you handle dependencies between test methods in TestNG?

You can handle dependencies between test methods using the "dependsOnMethods" attribute in the @Test annotation. This ensures that a test method is executed only after the specified dependent test methods pass successfully.

9. How do you perform parallel test execution in TestNG?

TestNG provides built-in support for parallel test execution. You can set the "parallel" attribute in the <test> tag of the TestNG XML file to "methods," "classes," or "tests" to execute test methods, classes, or entire test suites in parallel, respectively.

10. How do you generate HTML test reports in TestNG?

TestNG generates HTML test reports by default. You can find the test reports in the "test-output" folder of your project after the test execution. These reports contain detailed information about test results, including pass/fail status, execution time, and more.

11. How do you handle exceptions in TestNG?

TestNG allows you to handle exceptions in test methods using the "expectedExceptions" attribute in the @Test annotation. You can specify the expected exception type, and TestNG will mark the test method as a pass if the expected exception is thrown during execution.

12. How to run a test case multiple times?

To run a test case multiple times we can use invocationCount attribute and by providing its value as desired number of iteration.

13. How to disable a test method?

To disable a test method you can add enabled=false attribute to @test annotation

14. How to run only a selected number of test cases?

To run only a selected number of test cases, you can add them to desired group. To add a test method to a group, use the groups attribute of @Test annotation(eg. groups={"group1"}). Then run the command mvn test -Dgroups=group1.

15. How to fail a test case if it is taking more than a certain time to finish execution?

You can use timeOut=millisecond (eg timeOut=5000) attribute of @Test annotation to set a time out for any test case.

16. What is assertion and what are different assertion methods you have used?

Assertions are validations or checkpoints for an application where a particular information is compared to see if the actual information is as expected.
Following are some of the commonly used assertion methods:

  • assertEquals(expected value, actual value): Checks if the expected value is equal to the actual value.
  • assertNotEquals(expected, actual): Checks if the expected value is not equal to the actual value.
  • assertTrue(condition): Checks if the given condition is true.
  • assertFalse(condition): Checks if the given condition is false.
  • assertNull(object): Checks if the object is null.
  • assertNotNull(object): Checks if the object is not null

17. What is the difference between Hard Assert and Soft Assert?

A hard assert is the default assertion mechanism in TestNG. When a hard assert statement fails, it immediately stops the execution of the current test method and moves on to the next test method. Any subsequent code in the current test method after a failed hard assertion will not be executed.

Soft Assert is an assertion mechanism that allows you to continue running a test even after an assertion failure occurs. Unlike regular (hard) assertions, which stop the test execution immediately upon failure, soft assertions collect all assertion failures and report them at the end of the test.

18. What is testng.xml file and why it is used?

TestNG XML is a configuration file used in TestNG. It is used to to define and customize the execution of test suites, test cases, and test methods. It provides a way to control various aspects of your test runs, such as specifying which classes, methods or groups to include or exclude, setting up parallel execution, configuring listeners for reporting, and passing parameters to tests.

19. Can you explain the hierarchy of testng.xml file?

A simple configuration xml file will look like the following :

XML
Copy
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Example Suite">
    <test name="Example Test">
        <classes>
            <class name="org.example.ExampleTest"/>
        </classes>
    </test>
</suite>

20. How to run tests in parallel using testng.xml?

To run the tests in parallel using testng.xml, you need to add the add attribute parallel="tests" and thread-count="desired thread count". Here is an example of testng.xml file.

XML
Copy
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Tests" parallel="tests" thread-count="5">
    <test name="Parallel Test 1">
        <classes>
            <class name="org.example.ExampleTest1"/>
        </classes>
    </test>
</suite>

21. How to parameterize a test case?

Parameterization can be done following ways :-

1. Using @Parameters

  1. Using testng.xml file
  2. Programatically
  3. Java system properties

2. Using @DataProvider

22. What are the different ways to perform parallel execution.

TestNG allows parallel execution by which you can run tests simultaneously on different threads. You can run your tests in following ways by distributing to different threads.

  • Parallel suites
  • Parallel tests
  • Parallel classes
  • Parallel methods
  • Parallel instances

23. What are listeners?

TestNG Listeners listen to the events in your test scripts and modify the behavior. Listeners are used mostly for logging or reporting. TestNG provides several listeners in the form of interfaces that allow you to modify TestNG's behavior. These interfaces are broadly called TestNG Listeners

24. What are the different listeners?

Following are the different listeners in TestNG.

  • IAnnotationTransformer
  • IHookable
  • IInvokedMethodListener
  • IMethodInterceptor
  • IReporter
  • ISuiteListener
  • ITestListener