TOP TESTNG FRAMEWORK INTERVIEW QUESTIONS & ANSWERS

Question #1) What is the significance of testng.xml file?
Answer: In a Selenium TestNG project, we use testng.xml file to configure the complete test suite into a single file. This file makes it easy to group all the test suites and their parameters in one file. It also gives the ability to pull out subsets of your tests or split several runtime configurations. Few of the tasks which we can group in the testng.xml file are as follows.
  1. Can configure test suite comprising of multiple test cases to run from a single place.
  2. Can include or exclude test methods test execution.
  3. Can mark a group to include or exclude.
  4. Can pass parameters in test cases.
  5. Can add group dependencies.
  6. Can configure parallel test execution.
  7. Can add listeners.
Question #2) How to pass parameter through testng.xml file to a test case?
Answer: You can set the parameter using the below syntax in the testng.xml file.
Here, name attribute represents the parameter name and value signifies the value of that parameter. Then we can use that parameter in the selenium WebDriver software automation test case using the bellow syntax.
Question #3) How to exclude a @Test method from a test case with two @Test methods? Is it possible?
Answer: Yes, you need to add @Test method in the exclude tag of testng.xml file as mentioned below.
Question #4) How to skip a @Test method from execution?
Answer: You can use the below syntax inside @Test method to skip a test case from test execution.
It will throw skip exception and @Test method will be ignored immediately from execution.
Question #5) Can you arrange the below testng.xml tags from parent to child?
<test>
<suite>
<class>
<methods>
<classes>
Answer: The testng.xml file will have the following structure.
  • The parent tag in the testng.xml file is the <suite> tag.
  • <suite> tag can include one or more <test> tags.
  • <test> tag can include the <classes> tag.
  • <classes> tag can include one or more <class> tags.
  • <class> tag wraps the <methods> tag where we define the test methods to include or exclude.
Hence, the correct order of the TestNG tags would be.
<suite>
<test>
<classes>
<class>
<methods>
Question #6) How to define priority of @Test method? Also mention its usage?
Answer:
In your Selenium WebDriver project, you can set priority for TestNG @Test annotated methods as shown in the following example.
Using priority, you can manage @Test method execution sequence as per your requirement. That means @Test method with priority = 0 will run 1st and @Test method with priority = 1 will execute 2nd and so on.
Question #7) Can you specify any 6 assertions of TestNG to be used in a Selenium WebDriver software testing tool.
Answer: There are multiple assertions available In TestNG but generally we use the following assertions in out test cases.
  1. assertEquals
  2. assertNotEquals
  3. assertTrue
  4. assertFalse
  5. assertNull
  6. assertNotNull
Question #8) Why soft assertion is used in Selenium WebDriver and TestNG automation project?
Answer: TestNG soft assertion allows to continue the test execution even if the assertion is failed. That means once the soft assertion fails, remaining part of @Test method is executed and the assertion failure is reported at the end of @Test method.
Question #9) How to apply regular expression in testng.xml file to find @Test methods containing “product” keyword?
Answer:
Refer below example, here we’ve used a regular expression to find @Test methods containing keyword “product”.
Question #10) What are the time unit we specify in test cases and test suites? minutes? seconds? milliseconds? or hours? Give Example.
Answer: Time unit we specify at @Test method level and test suite level which normally set in milliseconds unit.