ngrinder中的groovy腳本結構類似 junit,同時在junit的基礎之上封裝了自己的註解,用來控制腳本的運行。
一、運行邏輯圖如下:
此處只列出了groovy腳本的邏輯,jython腳本是類似的,在此不再單獨介紹。
二、各註解的使用比較
三、關注點
在ngrinder中,通常使用單進程多線程就足夠大部分測試了,所以:
我們最需要關注的就是 @Test ,這個是循環體;
其次是 @Before ,這裏設置多個循環體的共享變量;
再其次是 @BeforeThread 和 @AfterThread ,用於設置每個線程執行前後的行爲。
四、具體代碼結構
@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() {
// 關閉資源
}