Định nghĩa: Annotation là tính năng giúp add thêm thông tin vào 1 object. Nó có thể được dùng cho class, method, variable, và parameters. TestNG cung cấp rất nhiều loại annotation cho các mục đích khác nhau, trong đó có các annotation phục vụ cho mục đích: xử lý trước và sau method Test. Vì sao cần xử lý trước và sau Test:
Cần tạo môi trường trước khi thực hiện test.
TestNG cung cấp 5 annotation ở dạng Before/After:
I. Khai báo annotation trên 1 class
Thứ tự chạy của chúng sẽ được thể hiện qua ví dụ sau:
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"); } }Và ta cần thêm 1 config cho file testng.xml
Và đây là kết quả:
II. Khai báo annotation khi có extend
Ở phía trên, chúng ta đã xem về cách sử dụng annation trên 1 class, chúng ta sẽ băn khoăn là thế nhưng chúng ta viết theo dạng POM thì có cả BaseTest, không biết thứ tự run annotation sẽ như thế nào?
Ta có class BaseTest:
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:
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"); } }Và file testng.xml:
Đây là kết quả:
Trong một project, không nhất thiết phải sử dụng hết tất cả các annotation này, nhưng ta cần phải biết thứ tự để từ đó control code của mình chạy theo thứ tự mình mong muốn, ví dụ như chụp screentshot ở cuối mỗi test, khởi tạo connection để đọc file Excel…