TestNG Advanced
1. Is it possible to customize the TestNG XML result?
Yes, it is possible to customize the TestNG result XML file using IReporter. By overriding its generateReport method we can modify the structure and adds custom fields. You can generate your own XML format for better reporting and integrations.
2. What are the different ways to run the TestNG tests?
Following are the different ways to run the TestNG tests:
- Running TestNG Test from IDE (Using TestNG Plugin)
- Using Command Line
- Using Testng xml file
- Using Maven
- Using Gradle
- Using programmatically in Java
- Using Jenkins CI/CD Pipeline
3. How to run the ignored tests?
The ignored tests or the tests that are marked as @Test(enabled = false) to ignore them temporarily can be re-run without modifying the test code using IAnnotationTransformer by implementing its method transform. In this method we can modify the annotation attribute value by changing it to annotation.setEnabled(true).
4. How do you execute a TestNG file using pom.xml?
- Add the TestNG dependency in pom.xml.
- Configure the surefire-plugin to execute the testng.xml file.
- Example
XML
Copy
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build>
- Run the tests
mvn test
5. What is the difference between @BeforeClass and @BeforeTest in TestNG?
- @BeforeClass: Runs before the first method in the current class.
- @BeforeTest: Runs before any test methods in a test
block in testng.xml. - @BeforeClass is specific to a class, whereas @BeforeTest is for multiple classes inside the same test tag.
6. What tasks are typically performed under the @BeforeTest annotation?
- Setting up database connections.
- Launching a browser session.
- Configuring properties or environment variables.
- Logging test suite details.
7. How do you rerun failed test cases in TestNG?
- Use the testng-failed.xml file generated in the test-output folder.
- Implement IRetryAnalyzer to rerun failed tests automatically.
- Example:
Java
Copy
import org.testng.IRetryAnalyzer; import org.testng.ITestResult; public class RetryLogic implements IRetryAnalyzer { private int retryCount = 0; private static final int maxRetryCount = 2; public boolean retry(ITestResult result) { if (retryCount < maxRetryCount) { retryCount++; return true; } return false; } }
- Apply it to a test method:
Java
Copy
@Test(retryAnalyzer = RetryLogic.class) public void testMethod() { Assert.assertTrue(false); }
8. How do you group and run test cases in TestNG?
- Use the groups attribute in @Test.
- Example
Java
Copy
@Test(groups = {"regression"}) public void regressionTest() {} @Test(groups = {"smoke"}) public void smokeTest() {}
- Define groups in testng.xml:
XML
Copy
<groups> <run> <include name="regression"/> </run> </groups>
- Run the tests
mvn test -Dgroups=regression
9. What tasks are included under the @BeforeSuite annotation?
10. What is the maximum number of thread pools that can be opened in TestNG?
11. What are the major challenges in parallel testing using TestNG and Selenium Grid?
- Thread synchronization issues.
- Shared resource conflicts (e.g., database connections, file access).
- Session handling issues in Selenium WebDriver.
- Performance bottlenecks due to improper thread handling.
12. How do you execute different test suites using Maven and TestNG?
- Define multiple test suites in pom.xml:
XML
Copy
<configuration> <suiteXmlFiles> <suiteXmlFile>suite1.xml</suiteXmlFile> <suiteXmlFile>suite2.xml</suiteXmlFile> </suiteXmlFiles> </configuration>
- Run the tests
mvn test
Unknown
Copy
13. How do you set the priority of test methods in TestNG?
- Use the priority attribute in @Test annotation.
- Example:
Java
Copy
@Test(priority = 1) public void testMethodA() { System.out.println("Test Method A"); } @Test(priority = 2) public void testMethodB() { System.out.println("Test Method B"); }
- Lower priority values execute first.
14. What is the default priority of test methods in TestNG?
- If no priority is set, TestNG assigns a default priority of zero (0).
- Test methods without a priority are executed in an unpredictable order.
15. Can priority be negative or zero in TestNG?
- Yes, priority can be negative, zero, or positive.
- Example:
Java
Copy
@Test(priority = -1) public void negativePriorityTest() { System.out.println("Executed first due to negative priority"); } @Test(priority = 0) public void zeroPriorityTest() { System.out.println("Executed after negative priority test"); }
16. How do you perform a dry run for your tests in TestNG?
- Dry run means executing tests without performing actual actions.
- You can achieve this by:
- Using a flag (e.g., boolean isDryRun = true) and conditionally skipping actions.
- Using @Test(enabled = false) to skip execution.
- Using TestNG Listeners to log test execution without running actual tests.
17. What is a suite of suites in TestNG? How does it help in execution?
- Suite of suites is a collection of multiple TestNG suites inside a single testng.xml file.
- Helps in executing multiple test suites together for better organization.
- Example:
XML
Copy
<suite name="MasterSuite"> <suite-files> <suite-file path="suite1.xml"/> <suite-file path="suite2.xml"/> </suite-files> </suite>
- Benefits:
- Allows modular execution of tests.
- Helps in large-scale automation projects.
- Reduces redundancy by managing related suites in one file.