nGrinder中快速編寫groovy腳本01-腳本結構

在前面的文章中,咱們已經知道,nGrinder支持groovy和Jython腳本,今天學習一下ngrinder中的groovy腳本結構。

ngrinder中的groovy腳本結構相似 junit,同時在junit的基礎之上封裝了本身的註解,用來控制腳本的運行。java

1、運行邏輯圖以下:

clipboard.png

2、各註解的使用比較

註解 描述 應用範圍 用例
@BeforeProcess 定義在進程被調用以前應執行的行爲 static method 加載被線程共享的資源文件,定義 GTest 等
@AfterProcess 定義在進程被終止以前應執行的行爲 static method 關閉資源文件
@BeforeThread 定義在每一個線程被調用以前應執行的行爲 member method 登陸目標系統,創建線程內的一些值,例如,Cookie 處理
@AfterThread 定義在每一個線程被終止以前應執行的行爲 member method 退出系統
@Before 定義每一個被 @Test 註解的方法被執行前應執行的行爲 member method 每一個被 @Test 註解的方法的共享邏輯、變量設置
@After 定義每一個被 @Test 註解的方法被執行後應執行的行爲 member method 不多使用
@Test 定義測試行爲,被執行屢次 member method 測試體

3、關注點

在ngrinder中,一般使用單進程多線程就足夠大部分測試了,因此:cookie

  • 咱們最須要關注的就是 @Test ,這個是循環體;
  • 其次是 @Before ,這裏設置多個循環體的共享變量;
  • 再其次是 @BeforeThread@AfterThread ,用於設置每一個線程執行先後的行爲。

4、具體代碼結構

@RunWith(GrinderRunner)  // 每一個測試類都要加這個註解
class TestRunner {
    @BeforeProcess  // 在每一個進程啓動前執行
    public static void beforeProcess() {
        // 加載資源文件、初始化 GTest 等
    }
    @BeforeThread  // 在每一個線程執行前執行
    public void beforeThread() {
        // 登陸、設置 cookie 之類
    }
    
    @Before  // 在每一個 @Test 註解的方法執行前執行
    public void before() {
        // 設置變量、多個 @Test 方法共用的邏輯等
    }
    @Test  // 在測試結束前不斷運行。各個 @Test 註解的方法異步執行。
    public void foo() {
        // ...
    }
    
    @Test
    public void bar() {
        // ...
    }
    
    @After  // 在每一個 @Test 註解的方法執行後執行
    public void after() {
        // 不多用到
    }
    
    @AfterThread
    public void afterThread() {
        // 登出之類
    }
    
    @AfterProcess  // 在每一個進程結束後執行
    public static void afterProcess() {
        // 關閉資源
    }
相關文章
相關標籤/搜索