Define
In this article, I will talk about TestNG Annotation, one of the most important things of TestNG that we need to understand. Annotation in TestNG is the comment that defines what will be done next or the functions that will be run next. Annotation is a feature that adds information to an object. It can be used for classes, methods, variables, and parameters. TestNG provides a variety of annotations for different purposes, including annotations for the purpose of: pre- and post-test processing.
Why should we handle it before and after the test?
- Need to create an environment before testing.
- It is necessary to clear all states after the test is performed or to perform actions that are not related to the Test method but necessary such as screenshot, delete session or close connection …
TestNG provides 5 annotations in the form of Before / After:
- BeforeSuite / AfterSuite
- BeforeTest / AfterTest
- BeforeGroups / AfterGroups
- BeforeClass / AfterClass
- BeforeMethod / AfterMethod
Declaring annotation on a class
Their running order will be shown in the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
public class TestAnnotation { @BeforeSuite public void beforeSuite() { System.out.println("Before Suite"); } @AfterSuite public void afterSuite() { System.out.println("After Suite"); } @BeforeTest public void beforeTest() { System.out.println("Before Test"); } @AfterTest public void afterTest() { System.out.println("After Test"); } @BeforeClass public void beforeClass() { System.out.println("Before Class"); } @AfterClass public void afterClass() { System.out.println("After Class"); } @BeforeGroups(groups = { "testOne" }) public void beforeGroupOne() { System.out.println("Before Group testOne"); } @AfterGroups(groups = { "testOne" }) public void afterGroupOne() { System.out.println("After Group testOne"); } @BeforeGroups(groups = { "testTwo" }) public void beforeGroupTwo() { System.out.println("Before Group testTwo"); } @AfterGroups(groups = { "testTwo" }) public void afterGroupTwo() { System.out.println("After Group testTwo"); } @BeforeMethod public void beforeMethod() { System.out.println("Before Method"); } @AfterMethod public void afterMethod() { System.out.println("After Method"); } @Test(groups = { "testOne" }) public void testOneMethod() { System.out.println("Test method One"); } @Test(groups = { "testTwo" }) public void testTwoMethod() { System.out.println("Test method Two"); } } |
And we need to add 1 config to the testng.xml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="First Suite" verbose="1"> <test name="First Test"> <classes> <class name="testannotation.TestAnnotation"> <methods> <include name="testOneMethod" /> </methods> </class> </classes> </test> <test name="Second Test"> <classes> <class name="testannotation.TestAnnotation"> <methods> <include name="testTwoMethod" /> </methods> </class> </classes> </test> </suite> |
And this is the result:
From here, we can understand the order in which they are run:
- Once started, open from Suite> Test> Group> Class> Method
- When finished, close from Method> Class> Group> Test> Suite
Declaring annotation when extend
In the above, we have seen how to use annation on a class, we will wonder but we write in the form of POM, including BaseTest, do not know what the order of run annotation will be like?
Take a look at the example below:
We have the BaseTest class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class BaseTest { @BeforeClass public void beforeBaseClass() { System.out.println("Parent Before Class method"); } @AfterClass public void afterBaseClass() { System.out.println("Parent After Class method"); } @BeforeMethod public void beforeBaseMethod() { System.out.println("Parent Before method"); } @AfterMethod public void afterBaseMethod() { System.out.println("Parent After method"); } } |
Class Test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class TestClass extends BaseTest { @BeforeClass public void beforeChildClass() { System.out.println("Child Before Class method"); } @AfterClass public void afterChildClass() { System.out.println("Child After Class method"); } @BeforeMethod public void beforeChildMethod() { System.out.println("Child Before method"); } @AfterMethod public void afterChildMethod() { System.out.println("Child After method"); } @Test public void testMethod() { System.out.println("Test method under TestClass"); } } |
And testng.xml file:
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Inheritance Suite" verbose="1"> <test name="Inheritance Test"> <classes> <class name="testannotation.TestClass" /> </classes> </test> </suite> |
Here is the result:
The order of running when extend is:
- At the beginning: Parent Before> Child Before
- At the end: Child After> Parent After
In a project, it is not necessary to use all of these annotations, but we need to know the order in which our control code runs in the order we want, such as screentshot capture at the end of each test, initialization. create connection to read Excel file …
Test in TestNG and attributes of Test annotation
Annotation Test :
This is an annotation marking the method or the Class as part of the TestNG Test. If it is marked for Class, then all methods that Public will run, non-public methods will not run. This is where the functions below it will be run like a testcase. For example:
Attributes of Test annotation
alwaysRun: with the default value of false, it will be ignored if it does not have a dependency method. If set to true, the Method will be run even if the dependency method fails. Eg: Test (alwaysRun = true)
enabled: the default value is true. Used to mark whether the method is run or not. If false, that method will be ignored, not run. If true, that method will be run. Eg: Test (enabled = true)
description: Used to add information for the test. This attribute is quite effective when we want to add a description of the test case when the Name method cannot fully describe it. Eg: Test (description = “Print the second test method”)
expectedExceptions: used to determine the exception that a Method may encounter. Eg: Test (expectedExceptions = {IOException.class})
timeOut: specifies the maximum time that the test can run, if the run time is greater than the predefined time, the test result is fail. Eg: Test (timeOut = 500)
dataProvider: fill in data for test method, serve for data-driven testing (will be written in another article)
dataProviderClass: Enter the class name that TestNG will search for the data-provider method mentioned in the dataProvider attribute. By default, it will search for that class or base class.
dependsOnGroups: fill in the list of groups on which the test depends.
dependsOnMethods: fill the list of methods on which the test depends. (will write in another article)
groups: enter the group name, the tests that share the same group name will form a collection. This collection can be run from testng.xml file
In one test, many annotations can be applied together. For example:
1 2 3 4 5 |
@Test(enabled=true, description="Print the second test method") public void secondTest() { System.out.println("Second test method"); } |
The article has a reference website GiangTester Blog