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

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

1、運行邏輯圖以下:cookie


webp

此處只列出了groovy腳本的邏輯,jython腳本是相似的,在此再也不單獨介紹。多線程

2、各註解的使用比較app

webp

3、關注點異步

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

咱們最須要關注的就是 @Test ,這個是循環體;測試

其次是 @Before ,這裏設置多個循環體的共享變量;spa

再其次是 @BeforeThread 和 @AfterThread ,用於設置每一個線程執行先後的行爲。線程

4、具體代碼結構orm

@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() {

        // 關閉資源

    }

相關文章
相關標籤/搜索