TestNg QnA
TestNg QnA
TestNg QnA
TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit but
introduces some new functionalities that make it more powerful and easier to use. The
importance of TestNG includes:
• Provides annotations that simplify the structure and readability of test cases.
• Allows for configuration through XML files, which helps manage test suites and test
cases efficiently.
• Supports parallel test execution, which reduces test execution time.
• Generates detailed HTML reports by default, which can be further customized.
• Handles dependencies between test methods, making the test suite more robust.
The testng.xml file is used to configure and control the execution of tests. It allows us to:
Listeners in TestNG are used to modify the default behavior of TestNG. They allow you to:
• Perform actions before and after test methods, test classes, and test suites.
• Capture events like test start, test success, test failure, etc.
• Generate custom reports. Listeners are a concept specific to TestNG and not directly
related to Selenium.
Running the Same Method 100 Times in TestNG with the Same Data
You can achieve this by using the invocationCount attribute in the @Test annotation:
@Test(invocationCount = 100)
public void testMethod() {
// test code
}
• @BeforeSuite, @AfterSuite
• @BeforeTest, @AfterTest
• @BeforeClass, @AfterClass
• @BeforeMethod, @AfterMethod
• @Test
• @DataProvider
• @Factory
Tests can be configured in TestNG using the testng.xml file and annotations within test classes.
What is @DataProvider?
@DataProvider(name = "dataProviderName")
public Object[][] dataProviderMethod() {
return new Object[][] { {"data1"}, {"data2"} };
}
@Test(dataProvider = "dataProviderName")
public void testMethod(String data) {
// test code
}
@Test
public void testMethod() {
System.out.println("Data: " + data);
}
}
The order of test execution in TestNG can be controlled using the priority attribute in the
@Test annotation:
@Test(priority = 1)
public void testOne() {
// test code
}
@Test(priority = 2)
public void testTwo() {
// test code
}
To add/remove test cases in testng.xml, you can include or exclude methods, classes, or
packages:
<test name="TestName">
<classes>
<class name="com.example.TestClass">
<methods>
<include name="testMethod"/>
<exclude name="anotherTestMethod"/>
</methods>
</class>
</classes>
</test>
1. @BeforeSuite
2. @BeforeTest
3. @BeforeClass
4. @BeforeMethod
5. @Test
6. @AfterMethod
7. @AfterClass
8. @AfterTest
9. @AfterSuite
Failed test cases can be rerun using the testng-failed.xml file, which is generated after the
first run.
@Test(invocationCount = 10)
public void testMethod() {
// test code
}
Types of Listeners
• ITestListener
• ISuiteListener
• IReporter
• IAnnotationTransformer
• IInvokedMethodListener
Use of testng.xml
Used to define and configure test suites, tests, classes, methods, listeners, parameters, and
parallel execution settings.
You can have multiple suites in TestNG. Running all suites can be managed in the testng.xml
file.
Yes, you can have multiple suites in one XML file. To run all suites, configure them within the
suite tags.
InvocationCount in TestNG
@Test(invocationCount = 5)
public void testMethod() {
// test code
}
Background in Cucumber
Background is used to define a common set of steps that should be run before each scenario in a
feature file.
Tags are used to run specific sets of scenarios. Use @CucumberOptions to specify tags:
@CucumberOptions(
features = "src/test/resources/features",
glue = {"stepDefinitions"},
tags = "@tag1 and @tag2"
)
• Hooks: Special blocks of code that run before or after each scenario (@Before, @After).
• Tags: Used to filter which scenarios to run.