TestNG FAQ's

TestNG is a testing framework for Java, designed to make automated testing easier and more powerful. It supports features like annotations, grouping, prioritization, parameterization, parallel execution, and reporting. It is widely used in Selenium automation frameworks.

  • Generates default HTML and XML reports.
  • Supports cross-browser and parallel execution.
  • Allows grouping of test cases for easy execution.
  • Supports re-running failed test cases.
  • Automatically generates logs during execution.
  • Enables priority and order control for test methods.
  • Provides parameterization using @DataProvider annotation.

The order is: BeforeSuite → BeforeTest → BeforeGroup → BeforeClass → BeforeMethod → Test → AfterMethod → AfterClass → AfterGroup → AfterTest → AfterSuite.

We do suite-level execution using the testng.xml file by defining <suite> and <test> tags.

If no priority is given, TestNG executes them in alphabetical order by method name.

We can change the execution order using the priority attribute in the @Test annotation. For example: @Test(priority=1). If two methods have the same priority, they execute in alphabetical order. TestNG executes priorities from negative to positive numbers, and if no priority is given, the default is 0.

We use invocationCount in @Test, for example @Test(invocationCount=5), then the same method executes for 5 times.

There are three ways to skip a test in TestNG:

  • Use @Test(enabled = false) on the method.
  • Use @Ignore annotation to disable the test.
  • Add the method name in the <exclude> tag inside testng.xml.
  • Assert (Hard Assert) stops the execution if the assertion fails.
  • Verify (Soft Assert) does not stop execution if the assertion fails; continues to the next step.
  • Assert is mainly used for critical checks where failure should stop the test.
  • Verify is used for non-critical checks to log failures but continue execution.

Parameter in TestNG is a way to pass test data to test methods without hardcoding values. It is done using the @Parameters annotation along with the testng.xml file.

In testng.xml, we define the parameter with a name and value inside the <parameter> tag, and then link it to the test using the @Parameters annotation in the code.

It helps achieve data-driven testing by allowing different test data to run the same test without changing the code.

@DataProvider is used for data-driven testing in TestNG, allowing the same test method to run multiple times with different sets of data.

It helps avoid hardcoding test data and makes tests more flexible and reusable.

The @Test method is linked to the @DataProvider using the dataProvider attribute.

The return type of @DataProvider is a two-dimensional Object[][] array.

Groups in TestNG are used to categorize test methods into logical groups like smoke, regression, sanity, etc. It helps in selective execution of test cases instead of running the entire suite. We use the groups attribute in @Test annotation and include/exclude them in testng.xml.

The <include> and <exclude> tags in testng.xml are used to control which test methods or groups should run or be skipped during execution. <include> tag specifies the methods or groups to execute, while <exclude> specifies those to skip.

SoftAssert allows the test to continue even if an assertion fails, but the test result will fail only after calling assertAll(). To convert SoftAssert into HardAssert, call assertAll() at the end of the test. Without assertAll(), the test will always pass, even if assertions fail.

Parallel execution in TestNG is achieved using the parallel attribute in the testng.xml file. We can execute tests in parallel by methods, classes, or tests. We also set thread-count to define how many threads run simultaneously.

Cross-browser execution is done by parameterizing the browser type using @Parameters in TestNG. We define browser names in testng.xml and handle browser setup in @BeforeTest or @BeforeMethod. TestNG runs the same test on multiple browsers by creating separate test configurations in XML.