Annotation là tính năng giúp thêm thông tin vào một đối tượng. 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:
TestNG cung cấp 10 Annotation ở dạng Before và After và 1 Annotation dạng Test
@Test: Đại diện cho một test case.
@BeforeSuite: Chú thích sẽ được chạy trước khi tất cả các kiểm tra trong bộ kiểm thử đã chạy.
@AfterSuite: Chú thích sẽ được chạy sau khi tất cả các kiểm tra trong bộ kiểm thử đã chạy.
@BeforeTest: Chú thích sẽ được chạy trước khi bất kỳ một @Test nào thuộc trong cùng một class được gọi để chạy.
@AfterTest: Chú thích sẽ được chạy sau khi tất cả các @Test thuộc cùng class đã chạy xong.
@BeforeGroups: Chạy trước các group trong các @Test.
Phương pháp này được đảm bảo chạy ngay trước @Test đầu tiên thuộc bất kỳ nhóm nào được gọi ra.
@AfterGroups: Danh sách các nhóm mà phương pháp cấu hình này sẽ chạy sau.
Phương pháp này được đảm bảo chạy ngay sau khi phương pháp kiểm tra cuối cùng thuộc về bất kỳ nhóm nào được gọi ra.
@BeforeClass: Chú thích sẽ được chạy trước khi @Test đầu tiên trong lớp hiện tại được gọi. (sau @BeforeTest)
@AfterClass: Chú thích sẽ được chạy sau khi tất cả các @Test trong lớp hiện tại đã được chạy hết. (đóng trước @AfterTest)
@BeforeMethod: Chú thích sẽ được chạy trước mỗi @Test. (dùng nhiều)
@AfterMethod: Chú thích sẽ được chạy sau mỗi @Test. (dùng nhiều)
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 một config cho file testng.xml
<!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>
Và đây là kết quả:
![[Selenium Java] Lesson 10: Cách sử dụng chú thích (Annotation) trong TestNG | Anh Tester](/uploads/lesson/selenium_java/testng/testng-10-1.png)
Từ đây, ta có thể hiểu được thứ tự chạy chúng:
Ở 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?
Hãy xem ví dụ dưới đây:
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:
<!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>
Đây là kết quả:
![[Selenium Java] Lesson 10: Cách sử dụng chú thích (Annotation) trong TestNG | Anh Tester](/uploads/lesson/selenium_java/testng/testng-10-2.png)
Thứ tự chạy khi có kế thừa (extends) sẽ là:
Đây là annotation đánh dấu method hoặc Class là 1 phần của TestNG Test. Nếu nó được đánh dấu cho Class thì tất cả các Method mà Public thì sẽ được run, các Method không phải public sẽ không được run.
Ví dụ:
![[Selenium Java] Lesson 10: Cách sử dụng chú thích (Annotation) trong TestNG | Anh Tester](/uploads/lesson/selenium_java/testng/testng-10-3.png)
![[Selenium Java] Lesson 10: Cách sử dụng chú thích (Annotation) trong TestNG | Anh Tester](/uploads/lesson/selenium_java/testng/testng-10-4.png)
![[Selenium Java] Lesson 10: Cách sử dụng chú thích (Annotation) trong TestNG | Anh Tester](/uploads/lesson/selenium_java/testng/testng-10-5.png)
![[Selenium Java] Lesson 10: Cách sử dụng chú thích (Annotation) trong TestNG | Anh Tester](/uploads/lesson/selenium_java/testng/testng-10-6.png)
![[Selenium Java] Lesson 10: Cách sử dụng chú thích (Annotation) trong TestNG | Anh Tester](/uploads/lesson/selenium_java/testng/testng-10-7.png)
NOTE:
@Test(enabled=true, description="Anh Tester - Automation Testing")
public void TestMethod() {
System.out.println("Anh Tester");
}
package TestNG;
import org.testng.annotations.*;
public class SettingPriority {
@Test(priority=1)
public void method1() {
System.out.println("Run method 1");
}
@Test(priority=0) //độ ưu tiên đầu tiên -> chạy method2() đầu tiên
public void method2() {
System.out.println("Run method 2");
}
@Test(priority=2)
public void method3() {
System.out.println("Run method 3");
}
}
Đoạn mã set độ ưu tiên sẽ được đặt trong annotation @Test , với cú pháp @Test(priority={expected_order}) (expected_order : 0 .... N , số càng nhỏ thì độ ưu tiên càng lớn). Trong ví dụ trên thì trình tự thực hiện sẽ là :
Severity: Notice
Message: Undefined variable: new
Filename: post/post_detail.php
Line Number: 384
Backtrace:
File: /home/anhtest2/public_html/application/views/frontend/post/post_detail.php
Line: 384
Function: _error_handler
File: /home/anhtest2/public_html/application/views/frontend/layout/layout_view.php
Line: 569
Function: view
File: /home/anhtest2/public_html/application/core/MY_Controller.php
Line: 34
Function: view
File: /home/anhtest2/public_html/application/controllers/frontend/Post.php
Line: 59
Function: render
File: /home/anhtest2/public_html/index.php
Line: 315
Function: require_once
Severity: Notice
Message: Trying to get property 'slug' of non-object
Filename: post/post_detail.php
Line Number: 384
Backtrace:
File: /home/anhtest2/public_html/application/views/frontend/post/post_detail.php
Line: 384
Function: _error_handler
File: /home/anhtest2/public_html/application/views/frontend/layout/layout_view.php
Line: 569
Function: view
File: /home/anhtest2/public_html/application/core/MY_Controller.php
Line: 34
Function: view
File: /home/anhtest2/public_html/application/controllers/frontend/Post.php
Line: 59
Function: render
File: /home/anhtest2/public_html/index.php
Line: 315
Function: require_once
Anh Tester
Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu