詳解TestNg中的依賴和併發

2021-10-10 05:59:07 字數 3672 閱讀 5757

我在之前的文章十分鐘上手testng講解了testng的基礎功能使用,今天給大家介紹一下testng框架中兩個非常重要的功能:依賴和併發。這也是它強於junit4的兩個重點!

依賴

testng的依賴主要包括方法的依賴和組的依賴。dependsonmethods在被依賴的方法執行完成之後執行當前方法,如果依賴方法測試不通過,那麼當前方法也不會繼續執行了,依賴的方法可以有多個。dependsongroups和依賴方法類似,在被依賴組執行完成之後執行當前組,如果依賴組中的方法沒有測試能過,那麼當前的方法也不會繼續執行了,依賴組可以有多個。

例如:publicclassdependencycase )

publicvoidm3() {

asserttrue(true);

執行結果,如下:

m3依賴m1和m2,因為m2是fail,所以m3沒有執行!如果你希望不論前置方法是否成功執行,有依賴的後置方法都會得到執行,那麼需要在testng方法註解上,加乙個alwaysrun=true屬性。dependsongroups與dependsonmethods類似,在這裡就不在贅述了。

用例併發測試

testng有多種併發方式支援,主要包括:方法的併發,class級的併發,和test級的併發,它們的區別如下:

tests級別:不同test tag下的用例可以在不同的執行緒執行,相同test tag下的用例只能在同乙個執行緒中執行。

classs級別:不同class tag下的用例可以在不同的執行緒執行,相同class tag下的用例只能在同乙個執行緒中執行。

methods級別:所有用例都可以在不同的執行緒去執行。

xml檔案中配置如下

實踐中,很多時候我們在測試類中通過dependonmethods/dependongroups方式,給很多測試方法的執行新增了依賴,以達到期望的執行順序。testng能在多執行緒情況下依然遵循既定的用例執行順序去執行。

有些時候,我們需要對乙個測試用例,比如乙個http介面,執行併發測試,即乙個介面的反覆呼叫。在 @test標籤中指定threadpoolsize和invocationcount可以實現該需求。

例如:@test(threadpoolsize=5,invocationcount=10)

其中threadpoolsize表明用於呼叫該方法的執行緒池容量,該例就是同時起5個執行緒並行執行該方法;invocationcount表示該方法總計需要被執行的次數。該例子中5個執行緒同時執行,當總計執行次數達到10次時,停止。

例項如下:

寫兩個類 threadcase1和threadcase1

publicclassthreadcase1 {

@test

publicvoidm1()throwsinterruptedexception {

thread.sleep(1000);

system.out.println("*****"+thread.currentthread().getid());

asserttrue(true);

@test

publicvoidm2()throwsinterruptedexception {

thread.sleep(1000);

system.out.println("*****"+thread.currentthread().getid());

asserttrue(false);

@test

publicvoidm3()throwsinterruptedexception {

thread.sleep(1000);

system.out.println("*****"+thread.currentthread().getid());

asserttrue(true);

publicclassthreadcase2 {

@test

publicvoidm1()throwsinterruptedexception {

thread.sleep(1000);

system.out.println("*****"+thread.currentthread().getid());

asserttrue(true);

@test

publicvoidm2()throwsinterruptedexception {

thread.sleep(1000);

system.out.println("*****"+thread.currentthread().getid());

asserttrue(false);

@test

publicvoidm3()throwsinterruptedexception {

thread.sleep(1000);

system.out.println("*****"+thread.currentthread().getid());

asserttrue(true);

在配置檔案中指定parallel為class,thread-count值為2

"testngparallel test" parallel="classes "thread-count="2">

"case1">

"com.my.test5.threadcase1"/>

"com.my.test5.threadcase2"/>

檢視執行結果:3292ms完成測試,如果不使用多執行緒則至少需要6s

總結

各個測試框架的基礎功能幾乎都一樣!不同的框架會提供一些特色功能,testng中的依賴和併發就是最為重要的兩個實用功能,也是在關於testng的面試中被問及最多的熱點,相信掌握了他們一定會幫你在面試中加分,贏得面試官的認可。而原創不易,如果文章幫到了你,歡迎**,讓更多的朋友受益!

改進TestNG的測試方法依賴管理

testng是乙個很強大的單元測試工具。和junit3 4版本相比,testng都多了乙個很好的功能 測試依賴 比如我可以寫乙個測試方法如下 test dependsonmethods testmethod2 public void testmethod1 這樣就表示在執行testmethod1測試...

TestNG中DataProvider的用法

提供資料的乙個測試方法。註解的方法必須返回乙個 object 其中每個物件 的測試方法的引數列表中可以分配。該 test 方法,希望從這個 dataprovider 的接收資料,需要使用乙個 dataprovider 名稱等於這個註解的名字。第一種用法 返回object dataprovider n...

Spark 中的寬依賴和窄依賴

spark中rdd的高效與dag圖有著莫大的關係,在dag排程中需要對計算過程劃分stage,而劃分依據就是rdd之間的依賴關係。針對不同的轉換函式,rdd之間的依賴關係分類窄依賴 narrow dependency 和寬依賴 wide dependency,也稱 shuffle dependenc...