The article provides a brief description of the TestNG annotations and their associated attributes in Selenium.
About TestNG
TestNG is a framework for automated testing. It is an abbreviation for Next Generation Tests and makes end-to-end testing simple by utilizing annotations (@).
To follow up the documentation, click the link.
Some remarkable features of TestNG:
- Annotations
- Parameterization
- It maintains a threat-free environment for multiple threads.
- It does not require you to create the test suites.
- Support for data-driven testing
- It allows you to set execution priorities for the test methods.
- It has an effective report generation tool, ReportNG.
You can apply annotations to the test methods.
Environment:
- Java
- Selenium
- TestNG
TestNG Annotations
The TestNG annotations enable us to specify the execution order of the methods in the test script. Place them before every method in the test code. The test algorithm does not execute the methods without annotations.
Available annotations:
- Test – This is where the TestNG test cases begin. The TestNG framework uses only the annotated methods.
The test annotation has the following attributes:
Enabled: use this attribute to skip or execute the test case (e.g. @Test(enabled='false')
).
Priority: The attribute enables you to specify the execution order of the test methods (e.g. @Test(priority=2)
).
Description: Under the description, you can describe your test case (e.g. @Test(description="This method is test case")
).
Groups: Use this attribute to group your test cases and specify a group you wish to execute in your TestNG XML file (e.g. @Test(groups={'web analyzer'})
).
dataProvider: The data provider’s name for this test method.
To prepare a test data provider, use the annotation @DataProvider:
@DataProvider (name = "data-provider")
public Object[][] dpMethod(){
return new Object[][] {{"First-Value"}, {"Second-Value"}};
}
Then it used in the test cases as:
@Test (dataProvider = "data-provider")
public void myTest (String val) {
System.out.println("Passed Parameter Is : " + val);
}
You can find an excellent explanation of this attribute at the link.
dependsOnMethods: The list of methods this method depends on ( e.g. @Test(dependsOnMethod="Login")
).
timeOut: The time (in milliseconds) the test should take (e.g. @Test(timeOut = 15000)
).
alwaysRun: To run the test method, even if it is dependent on a failed method, set this attribute to true.
- BeforeSuite, AfterSuite: The annotated method is the last execution method in the test suite.
- BeforeSuite: The annotated method is the first execution method in the test suite. To learn how this annotation works, navigate to the link.
- BeforeTest, AfterTest: The annotated method is the first (last) method in the TestNG file.
- BeforeClass, AfterClass: The annotated method is the first (or last) method in the current class.
- BeforeMethod, AfterMethod: The annotated method will be executed before (or after) each test method.
Was this helpful?
2 / 0