TestNG - Interview Questions


Dear readers, these TestNG Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of TestNG. As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer:

Q: What is TestNG?

A: TestNG is an open source automated testing framework; where NG of TestNG means Next Generation. TestNG is similar to JUnit (especially JUnit 4), but its not a JUnit extension. Its inspired by JUnit. It is designed to be better than JUnit, especially when testing integrated classes.

Q: What are the features of TestNG?

A: Features of TestNG are:

  • Annotations.

  • TestNG uses more Java and OO features.

  • Supports testing integrated classes (e.g., by default, no need to create a new test class instance for every test method).

  • Separate compile-time test code from run-time configuration/data info.

  • Flexible runtime configuration.

  • Introduces ‘test groups’. Once you have compiled your tests, you can just ask TestNG to run all the "front-end" tests, or "fast", "slow", "database", etc...

  • Supports Dependent test methods, parallel testing, load testing, partial failure.

  • Flexible plug-in API.

  • Support for multi threaded testing.

Q: What are the advantages of TestNG over Junit?

A: Advantages of TestNG over Junit are:

  • In Junit we have to declare @BeforeClass and @AfterClass which is a constraint where as in TestNG there is no constraint like this.

  • Additional Levels of setUp/tearDown level are available in TestNG like @Before/AfterSuite,@Before/AfterTest and @Before/AfterGroup

  • No Need to extend any class in TestNG.

  • There is no method name constraint in TestNG as in Junit. You can give any name to the test methods in TestNG.

  • In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test.

  • Grouping of testcases is available in TestNG where as the same is not available in Junit.

  • Execution can be done based on Groups. For ex. If you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. Then if you only want to execute the “Sanity” cases then just tell TestNG to execute the “Sanity” and TestNG will automatically execute the cases belonging to the “Sanity” group

Q: What are the basic steps involved in writing TestNG tests?

A: Writing a test in TestNG basically involves following steps:

  • Write the business logic of your test and insert TestNG annotations in your code.

  • Add the information about your test (e.g. the class name, the groups you wish to run, etc...) in a testng.xml file or in build.xml.

  • Run TestNG.

Q: Give examples of some of the annotations supported by TestNG.

A: TestNG supports the following annotations:

@BeforeSuite, @AfterSuite, @BeforeClass, @AfterClass, @BeforeTest, @AfterTest, @BeforeGroups, @AfterGroups, @BeforeMethod, @AfterMethod, @DataProvider, @Factory, @Listeners, @Parameters, @Test.

Q: What are the benefits of using annotations?

A: Following are some of the benefits of using annotations:

  • TestNG identifies the methods it is interested in by looking up annotations. Hence, method names are not restricted to any pattern or format.
  • We can pass additional parameters to annotations.
  • Annotations are strongly typed, so the compiler will flag any mistakes right away.
  • Test classes no longer need to extend anything (such as TestCase, for JUnit 3).
Q: What are the different ways in which TestNG can be invoked?

A: You can invoke TestNG in several different ways:

  • Using Eclipse

  • With ant

  • From the command line

  • Using IntelliJ's IDEA

Q: Give a an example to invoke TestNG from command line.

A: Assuming that you have TestNG in your class path, the simplest way to invoke TestNG is as follows:

java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...]
Q: What is testng.xml file used for?

A: File testng.xml captures your entire testing in XML. This file makes it easy to describe all your test suites and their parameters in one file, which you can check in your code repository or e-mail to coworkers. It also makes it easy to extract subsets of your tests or split several runtime configurations (e.g., testng-database.xml would run only tests that exercise your database).

Q: What is test suite?

A: A Test suite is a collection of test cases that are intended to test a behavior or set of behaviors of software program. In TestNG, we cannot define a suite in testing source code, but it is represented by one XML file as suite is the feature of execution. This also allows flexible configuration of the tests to be run. A suite can contain one or more tests and is defined by the <suite> tag.

<suite> is a root tag of your testng.xml. It describes a test suite, which in turn is made of several <test> sections.

Q: How can you disable a test in TestNG?

A: Annotation @Test(enabled = false) helps to disable the test case which you want to ignore.

Q: What is group test?

A: Group tests permits you dispatch methods into proper portions and preform sophisticated groupings of test methods. Not only can you declare those methods that belong to groups, but you can also specify groups that contain other groups. Then, TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set. This gives you maximum flexibility in how you partition your tests and doesn't require you to recompile anything if you want to run two different sets of tests back to back.

Q: How to you specify a group in testng.xml?

A: Groups are specified in your testng.xml file using the <groups> tag. It can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.

Q: What is exception test?

A: TestNG provides a option of tracing the Exception handling of code. You can test whether a code throws desired exception or not. The expectedExceptions parameter is used along with @Test annotation. Now, let's see @Test(expectedExceptions) in action.

Q: What is dependency test?

A: Sometimes, you may need to invoke methods in a Test case in a particular order or you want to share some data and state between methods. This kind of dependency is supported by TestNG as it supports the declaration of explicit dependencies between test methods.

TestNG allows you to specify dependencies either with:

  • Using attributes dependsOnMethods in @Test annotations OR

  • Using attributes dependsOnGroups in @Test annotations.

Q: What is difference between dependsOnGroups and dependsOnMethods?

A: Following are the differences:

  • On using groups, we are no longer exposed to refactoring problems. As long as we don’t modify the dependsOnGroups or groups attributes, our tests will keep running with the proper dependencies set up.

  • Whenever a new method needs to be added in the dependency graph, all we need to do is put it in the right group and make sure it depends on the correct group. We don’t need to modify any other method.

Q: What is parametric testing?

A: In most cases, you'll come across a scenario where the business logic requires a hugely varying number of tests. Parameterized tests allow developers to run the same test over and over again using different values.

TestNG lets you pass parameters directly to your test methods in two different ways:

  • With testng.xml

  • With Data Providers

Q: How do you pass parameters with testng.xml?

A: We define the simple parameters in the testng.xml file and then reference those parameters in source files.

Q:What does it mean to pass parameters using dataproviders?

A: When you need to pass complex parameters or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc...), in such cases parameters can be passed using Dataproviders. A Data Provider is a method annotated with @DataProvider. This annotation has only one string attribute: its name. If the name is not supplied, the Data Provider’s name automatically defaults to the method’s name. A Data Provider returns an array of objects.

Q: How can you run the JUnit tests using TestNG?

A: Put JUnit library on the TestNG classpath, so it can find and use JUnit classes, change your test runner from JUnit to TestNG in Ant and then run TestNG in "mixed" mode. This way you can have all your tests in the same project, even in the same package, and start using TestNG. This approach also allows you to convert your existing JUnit tests to TestNG incrementally. Also define property junit="true" in the <test> tag of the testng.xml.

Q: What are different ways in which you can generate the reports of TestNg results?

A: There are two main ways to generate a report with TestNG:

  • Listeners : For implementing a listener class, the class has to implement the org.testng.ITestListener interface. These classes are notified at runtime by TestNG when the test starts, finishes, fails, skips, or passes.

  • Reporters : For implementing a reporting class, the class has to implement an org.testng.IReporter interface. These classes are called when the whole suite run ends. The object containing the information of the whole test run is passed to this class when called.

What is Next ?

Further you can go through your past assignments you have done with the subject and make sure you are able to speak confidently on them. If you are fresher then interviewer does not expect you will answer very complex questions, rather you have to make your basics concepts very strong.

Second it really doesn't matter much if you could not answer few questions but it matters that whatever you answered, you must have answered with confidence. So just feel confident during your interview. We at tutorialspoint wish you best luck to have a good interviewer and all the very best for your future endeavor. Cheers :-)