---Advertisement---

TestNG Interview Questions and Answers (Level-1)

By Manisha

Published On:

---Advertisement---

Q1. What is TestNG?

Answer:
TestNG stands for “Test Next Generation”, a powerful open-source testing framework inspired by JUnit and NUnit. It is primarily designed to simplify automated testing for Java developers, especially when working with Selenium WebDriver.
It offers a wide array of features such as:

  • Flexible test configuration
  • Powerful annotations
  • Parallel test execution
  • Data-driven testing
  • Customizable reports

TestNG makes it easier to manage large test suites with advanced capabilities like grouping, prioritization, and dependency between test cases. It plays a crucial role in enhancing the testing features of Selenium automation.


Q2. What are the advantages of using TestNG?

Answer:
Here are the major advantages of TestNG in Selenium automation:

  1. Advanced Assertions: Provides a variety of assertions to validate test results.
  2. Parallel Execution: Enables executing test methods in parallel to save time.
  3. Test Dependency: Allows defining dependencies among test methods using dependsOnMethods.
  4. Priority Handling: You can assign custom execution priority using priority attribute.
  5. Grouping of Tests: Group test cases under Sanity, Regression, Smoke, etc., using groups attribute.
  6. Data-Driven Testing: Supports parameterization using @DataProvider and @Parameters.
  7. Built-in Reporting: Automatically generates HTML and XML reports after test execution.
  8. Cross-Browser Testing: Works seamlessly with cross-browser testing when integrated with Selenium Grid.

TestNG enhances the maintainability and scalability of test automation frameworks.


Q3. How is TestNG different from Selenium WebDriver?

Answer:
Selenium WebDriver is a browser automation tool, while TestNG is a testing framework. Selenium by itself cannot manage test execution flow, logging, reporting, or test grouping.

FeatureSelenium WebDriverTestNG
PurposeAutomates browser actionsManages test execution
AssertionsNot providedAvailable (assertTrue, assertEquals)
ReportingNot availableBuilt-in reports
Test GroupingNot supportedFully supported
Parallel ExecutionNot supported by defaultEasily supported

In essence, Selenium automates interactions with the UI, and TestNG gives the framework to validate, organize, and report on those interactions.


Q4. What is the use of the testng.xml file?

Answer:
The testng.xml
file plays a vital role in configuring and running tests in a structured way. It helps define test suites, control test executions, and integrate with CI/CD tools like Jenkins.

Key Features:

  • Organize test classes and packages
  • Include or exclude test methods
  • Set parallel execution mode
  • Define groups (e.g., Sanity, Regression)
  • Parameterize tests with <parameter> tag
  • Attach listeners for logging and reporting

Example snippet:

xml

<suite name=”MySuite”>

  <test name=”LoginTests”>

    <classes>

      <class name=”com.example.LoginTest”/>

    </classes>

  </test>

</suite>

You can run the testng.xml from IDEs like Eclipse or from command-line using:

bash

java -cp “bin;lib/*” org.testng.TestNG testng.xml


Q5. How can we group test cases like Sanity and Regression in TestNG?

Answer:
TestNG supports grouping test cases using the groups attribute in the @Test annotation. This allows executing only specific test categories like Sanity, Smoke, or Regression.

Example:

java

@Test(groups = {“sanity”})

public void testA() {

  // Sanity test logic

}

@Test(groups = {“sanity”, “regression”})

public void testB() {

  // Shared logic for both suites

}

To run specific groups, define them in testng.xml:

xml

<groups>

  <run>

    <include name=”sanity”/>

  </run>

</groups>

This feature ensures better test categorization and suite control.


Q6. How can we exclude a test method from executing in the testng.xml file?

Answer:
To exclude specific methods from execution, TestNG allows you to use the <exclude> tag inside the testng.xml.

Example:

xml

<suite name=”Suite1″>

  <test name=”Test1″>

    <classes>

      <class name=”com.example.TestClass”>

        <methods>

          <exclude name=”testMethodToBeSkipped”/>

        </methods>

      </class>

    </classes>

  </test>

</suite>

This is useful when a particular test case is unstable or temporarily needs to be skipped without changing code.


Q7. What are the most commonly used TestNG annotations?

Answer:
TestNG provides several annotations to control the execution flow:

AnnotationPurpose
@TestMarks a method as a test case
@BeforeSuite / @AfterSuiteExecutes once before/after entire suite
@BeforeTest / @AfterTestExecutes before/after <test> tag block
@BeforeClass / @AfterClassExecutes before/after all methods in a class
@BeforeMethod / @AfterMethodExecutes before/after each test method
@DataProviderSupplies data for data-driven testing
@ParametersPass parameters from XML to test methods

Each annotation controls test behavior at different levels – suite, class, or method – offering high flexibility.


Q8. What is the order of execution of TestNG annotations?

Answer:
TestNG executes annotations in the following hierarchical order:

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeClass
  4. @BeforeMethod
  5. @Test
  6. @AfterMethod
  7. @AfterClass
  8. @AfterTest
  9. @AfterSuite

This order ensures the setup and teardown activities are executed around the actual test methods in a structured and predictable way.


Q9. What are some common assertions in TestNG?

Answer:
TestNG provides several built-in assertions for validating test outcomes:

  • Assert.assertEquals(actual, expected)
  • Assert.assertNotEquals(actual, expected)
  • Assert.assertTrue(condition)
  • Assert.assertFalse(condition)
  • Assert.assertNull(object)
  • Assert.assertNotNull(object)
  • Assert.fail() – Forces test failure manually

These assertions help verify expected vs actual results, which are fundamental to any automated test case.


Q10. How can we disable a test case in TestNG?

Answer:
You can disable a test method using the enabled=false attribute in the @Test annotation.

Example 1 – Disable a single test:

java

@Test(enabled = false)

public void skippedTest() {

  // Will not run

}

Example 2 – Disable a test in a group:

java

@Test(groups = {“smoke”}, enabled = false)

public void disabledSmokeTest() {

  // Skipped test logic

}

👉The Next 10 Questions-1: TESTNG

---Advertisement---

Leave a Comment