TestNG Framework Interview Questions

This section covers frequently asked TestNG interview questions.

1. What is TestNG?

TestNG is a testing framework inspired by JUnit and NUnit, providing features for creating and running test suites in Java. It's designed for creating more sophisticated and organized test suites.

2. Advantages of TestNG.

  • Generates detailed test reports.
  • Supports test grouping for organizing test cases.
  • Allows running tests on multiple browsers (cross-browser testing).
  • Integrates well with other tools (like Maven and Jenkins).
  • Clear and easy-to-understand annotations.
  • Supports running individual test cases or groups of test cases.

3. Running TestNG Test Scripts.

Right-click on the TestNG class in your IDE and select "Run As" -> "TestNG test".

4. TestNG Annotations.

TestNG uses annotations to define test methods and their execution order:

  • Pre-condition annotations (@Before...): Executed before tests (@BeforeSuite, @BeforeClass, @BeforeTest, @BeforeMethod).
  • Test annotation (@Test): Defines a test method.
  • Post-condition annotations (@After...): Executed after tests (@AfterSuite, @AfterClass, @AfterTest, @AfterMethod).

5. Execution Sequence of Annotations.

(This section lists the standard execution order of TestNG annotations.)

6. Setting Priorities in TestNG.

Use the priority attribute within the @Test annotation to specify the execution order of test methods.

Example

@Test(priority = 1)
public void testMethod1() { ... }

@Test(priority = 2)
public void testMethod2() { ... }

7. Grouping in TestNG.

The groups attribute in @Test allows grouping tests. You can then run specific groups using the testng.xml file.

Example

@Test(groups = "group1")
public void testMethod1() { ... }

8. Dependencies in TestNG.

The dependsOnMethods and dependsOnGroups attributes specify dependencies between test methods or groups. Methods with dependencies won't run until their dependencies have completed successfully.

9. timeOut in TestNG.

timeOut sets a maximum execution time for a test method. If the method exceeds the timeout, it's marked as failed.

10. invocationCount in TestNG.

invocationCount specifies how many times a test method should be executed.

11. Importance of testng.xml.

testng.xml defines the test suite, including test method order, groups, dependencies, and listeners.

12. Passing Parameters via testng.xml.

Use the @Parameters annotation to pass parameters to test methods from the testng.xml file.

13. Disabling Test Cases.

Use the enabled attribute in @Test (e.g., @Test(enabled = false)).

14. Soft Assertion vs. Hard Assertion.

Assertion Type Behavior
Soft Assertion Continues execution after a failed assertion
Hard Assertion Stops execution immediately on a failed assertion

15. Using the @Listeners Annotation.

The @Listeners annotation allows you to associate custom listener classes with your TestNG test suite. These listeners can perform actions at various points during test execution (e.g., logging test results, taking screenshots on failure, etc.).

Listener Implementation (Java)

//This code would be included here, as provided in the original text.
Output

Success of test cases and its details are : test_to_success
Failure of test cases and its details are : test_to_fail

16. The @Factory Annotation.

The @Factory annotation allows you to create multiple instances of a test class, enabling the execution of multiple test methods dynamically.

Factory Method (Java)

import org.testng.annotations.Factory;

public class Factory1 {
    @Factory
    public Object[] getTestClasses() {
        return new Object[] { new Testcase1(), new Testcase2() };
    }
}

17. @Factory vs. @DataProvider.

Annotation Purpose
@DataProvider Provides data to a test method for multiple executions
@Factory Creates multiple instances of a test class