In the Previous tutorials, we explained how to add TestNG to eclipse and how to set up for creating the first test using TestNG. In this article, we will understand what annotations in TestNG are and how they are used:
When we create a test in TestNG, we divide the code into
different annotations based on which section needs to be executed and in which
order.
Let us take an example, We have three different tests to be automated. Each of the test starts with Login into application, In the next step, the user has either to view e-mails, write e-mail or verify successful login only. In the final step, we have to log out of application.
To code this, we have to login into application in each of
the three test, so we can define this method or code in @BeforeTest.
Next we will define all the tests to be executed with
annotation @Test and can assign group of test to be executed using group with
test.
Once the test is executed, user will log out from application, this we will tag with annotation @AfterTest.
Below code explains how the three annotations can be used in the test. There are many more annotations which we can add in the test to give logical structure to the test.
package testNG;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class NewTest {
@BeforeTest
public void Login()
{
System.out.println("Login into application");
}
@Test(groups = { "testemail" })
public void mailval()
{
System.out.println("test e-mail");
}
@Test(groups = { "testemail" })
public void emailval()
{
System.out.println("e-mailvalidated");
}
@AfterTest
public void Logout()
{
System.out.println("Logout from application");
}
}
Annotations in TestNG
Below table explains the different annotation used in TestNG:
Annotation
|
Description
|
@BeforeSuite
|
Annotated method to be executed before all tests in the suite
have run.
|
@AfterSuite
|
Annotated method to be executed after all tests in the suite has
run.
|
@BeforeTest
|
Annotated method to be executed before any test method belonging
to the classes inside the <test> tag is run.
|
@AfterTest
|
Annotated method to be executed after all the test methods
belonging to the classes inside the <test> tag have run.
|
@BeforeGroups
|
The list of groups that this configuration method will run
before. This method is guaranteed to run shortly before the first test method
that belongs to any of these groups is invoked.
|
@AfterGroups
|
The list of groups that this configuration method will run
after. This method is guaranteed to run shortly after the last test method
that belongs to any of these groups is invoked.
|
@BeforeClass
|
The annotated method to be executed before the first test method
in the current class is invoked.
|
@AfterClass
|
The annotated methods to be executed after all the test methods
in the current class have been run.
|
@BeforeMethod
|
The annotated method to be executed before each test
method.
|
@AfterMethod
|
The annotated method to be executed after each test method.
|
article on annotations in testing selenium very informative and learned a lot
ReplyDeleteSelenium Training
Selenium Training in Chennai