300+ REAL TIME TestNG Interview Questions and Answers

TestNG Interview Questions :-

1. What is TestNG?

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as: Annotations. Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc…)

2. What are the TestNG features?

TestNG framework introduces some new features that make it more powerful and easier to use, such as:

1. Annotations: An annotation is a form of metadata in java which can be applied to various elements of java source code so that any tool, debugger or application program can use these annotations. Classes, methods, variables, parameters and packages may be annotated. For your information, please note that the annotations in java source code are compiled into byte-code with other class members. And programmers can use reflection to query this metadata information to decide the appropriate action to perform in any particular context.

A metadata is data about data. Metadata adds some additional flags on your actual data. So that in runtime JVM can identify these flags and use this metadata information to take appropriate action.
2. Provides arbitrarily big thread pools to run tests in their own thread, one thread per test class, etc.
3. Ensure that your code is thread-safe.
4. Flexible test configuration.
5. Supports data-driven testing (with @DataProvider).
6. Support for parameters.
7. Powerful execution model (Independent of TestSuite).
8. Easy integration with a variety of tools and plug-ins (Eclipse, Maven, Jenkins etc.).
9. Embeds Bean Shell for further flexibility.
10. Uses default JDK functions for runtime and logging (no dependencies).
11. Dependent methods for application server testing.

3. List out the advantages of TestNG over Junit?

Advantages of JUnit over TestNG includes

  1. Compare to JUnit annotations, TestNG are easy to understand
  2. Unlike JUnit, TestNG does not require to declare @BeforeClass and @AfterClass
  3. Method name constraint is not there in TestNG
  4. TestNG allows you the grouping of test cases easily which is not possible in JUnit
  5. TestNG supports following three additional setup: @Before/AfterSuite, @Before/AfterTest and @Before/AfterGroup
  6. TestNG does not need to extend any class
  7. In TestNG, it is possible to run selenium test cases in parallel
  8. Based on group TestNG allows you to execute the test cases
  9. TestNG allows you to determine the dependent test cases; each test case is autonomous to other test case

4. What are the basic steps required in writing TestNG tests?

The basic steps required in writing TestNG includes

  • Write down the business logic of your test and insert TestNG annotations in your code
  • In a build.xml or testing.xml, add the information about your test
  • Run TestNG

5. List out various ways in which TestNG can be invoked?

TestNG can be invoked in different ways like

  • Using Eclipse
  • With ant
  • From the command line
  • Using IntelliJ’s IDEA

6. What is testing.xml file used for?

File testing.xml captures your entire testing in XML. This file makes it easy to define all your test suites and their parameters in one file, which you can verify in your code repository or e-mail to coworkers. It also makes easy to pull out subsets of your tests or split several runtime configurations.

7. In TestNG how can you disable a test?

To disable the test case you don’t want, you can use annotations @Test(enabled = false).

8. What is Time-Out test in TestNG?

The Time-Out test in TestNG is nothing but time allotted to perform unit testing. If the unit test fails to finish in that specific time limit, TestNG will abandon further testing and mark it as a failed.

9. What is exception test?

TestNG gives option for tracing the Exception handling of code. You can test whether a code throws the expected results or not. The expectedExceptions parameter is availed along with @Test annotation.

10. Mention what does the “suite test” does in TestNG?

“Suite Test” is done when you have to run few unit test together, “ Suite Test” bundle this unit test together. XML file is used to run the suite test.

11. What is parametric testing?

Parameterized testing allows developers to execute the same test over and over again using different values. In two different ways TestNG allows you to pass parameters directly to your test methods.

  • With testing.xml
  • With Data Providers

12. Explain how can you run the JUnit tests using TestNG?

You can run the JUnit tests using TestNG by

  • Placing JUnit library on the TestNG classpath, so it can locate and use JUnit classes
  • Change your test runner from JUnit to TestNG in Ant and then run TestNG in “mixed mode” . This will bring all your test in the same
  • This approach also enables you to convert your existing JUnit test to TestNG.

13. What does @Test(invocationCount=?. and (threadPoolSize=?. indicates?

  • @Test (threadPoolSize=?): The threadPoolSize attributes tell TestNG to form a thread pool to run the test method through multiple threads. With threadpool, the running time of the test method reduces greatly.
  • @Test(invocationCount=?): The invocationcount tells how many times TestNG should run this test method

14. Mention different ways in which you can produce reports for TestNG results?

There are two ways to produce a report with Test NG, they are

Listeners: For a listener class to implement, the class has to implement the org.testng./TestListener interface. These classes are informed at runtime by TestNG when the test begins, finsishes, skips, passes or fails.

Reporters: For a reporting class to implement, the class has to implement an org.testng/Reporter interface. When the whole suite run ends, these classes are called. When called, the object consisting the information of the whole test run is delivered to this class.

15. What is Group Test in TestNG?

It is a new feature included in TestNG, it allows you to dispatch methods into proper portions and perform grouping of test methods. With group test, you can not only declare methods that belong to groups, but you can also specify groups that contain other groups. Groups are determined in your testing.xml file using the group test.

16. Explain in what ways does TestNG allows you to specify dependencies?

TestNG allows you to specify dependencies in two ways

  • Using attributes dependsOnMethods in @Test annotations
  • Using attributes dependsOnGroups in @Test annotations

17. What it means when you have to pass parameters using data-providers in TestNG?

When you have to pass complex parameter or parameters that are to be created from Java, in such instances parameters can be passed using Dataproviders. The annotation for data provider is @DataProvider. This annotation has only single string attribute, if the name is not declared; the Data provider’s name automatically defaults to the method’s name. A data provider yields back an array of objects.

18. Explain how you can execute tests in TestNG?

The tests in TestNG are executed using TestNG class. For running tests in TestNG framework, class is the main entry point. Users can make their own TestNG object and invoke it in many different ways like

  • On an already existing testing.xml
  • On a synthetic testing.xml created entirely from Java
  • By directly setting the test classes

19. How does TestNG allow you to state dependencies?

TestNG supports two ways to declare the dependencies.

  1. Using attributes dependsOnMethods in @Test annotations.
  2. Using attributes dependsOnGroups in @Test annotations.

20. What are the different ways to produce reports for TestNG results?

TestNG offers following two ways to produce a report.

Listeners: For a listener class, the class has to implement the org.testng./TestListener interface. TestNG notifies these classes at runtime when the test enters into any of the below states.
e.g. Test begins, finishes, skips, passes or fails.

Reporters: For a reporting class to implement, the class has to implement an org.testng/Reporter interface. When the whole suite run ends, these classes are called. When called, the object consisting the information of the whole test run is delivered to this class.

21. What does the test timeout mean in TestNG?

The timeout test in TestNG is nothing but the time allotted to perform unit testing. If the unit test fails to finish in that specific time limit, TestNG will abandon further testing and mark it as a failed.

22. In TestNG how can you disable a test?

To disable the test case, you can use the following annotation.

@Test(enabled = false).

23. List out different ways to run TestNG?

You can run TestNG in the following ways.

  1. Start directly from the Eclipse IDE, or
  2. Run using the IntelliJ’s IDEA IDE.
  3. Run with ant build tool.
  4. Launch from the command line.

TestNG Interview Questions and Answers pdf free download ::

Leave a Reply

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