TestNG系列教程:并行執(zhí)行測試-Java開發(fā)Java經(jīng)驗技巧_第1頁
TestNG系列教程:并行執(zhí)行測試-Java開發(fā)Java經(jīng)驗技巧_第2頁
TestNG系列教程:并行執(zhí)行測試-Java開發(fā)Java經(jīng)驗技巧_第3頁
TestNG系列教程:并行執(zhí)行測試-Java開發(fā)Java經(jīng)驗技巧_第4頁
TestNG系列教程:并行執(zhí)行測試-Java開發(fā)Java經(jīng)驗技巧_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、tcstng系列教程:并行執(zhí)行測試-編程開發(fā)技術(shù)testng系列教程:并行執(zhí)行測試木文由importnew楊昆,侖翻譯自lokesh g叩ta。歡迎加入翻譯小組。轉(zhuǎn)載請見文末要 求。并行(多線程)技術(shù)在軟件術(shù)語里被定義為軟件、操作系統(tǒng)或者程序可以并行地 執(zhí)行另外一段程序屮多個部分或者子組件的能力。testng允許我們以并行(多 線程)的方式來執(zhí)行測試。這就意味著基于testng測試組件的配置,多個線程 可以被同吋啟動然后分別執(zhí)行各自的測試方法。相對于傳統(tǒng)的單線程執(zhí)行測試的 方式,這種多線程方式擁有很大的優(yōu)勢,主要是它可以減少測試運行時間,并且 可以驗證某段代碼在多線程環(huán)境中運行的止確性。冃錄1

2、. 并行執(zhí)行測試的優(yōu)勢2. 如何并行地執(zhí)行測試方法3. 如何并行地執(zhí)行測試類4. 如何并行地執(zhí)行同一測試套件內(nèi)的各個測試組件5. 如何配置需要在多線程環(huán)境屮執(zhí)行的測試方法并行執(zhí)行測試的優(yōu)勢并行(多線程)執(zhí)行測試可以給用戶帶來很多好處,主要包括以下兩點:1)減少了執(zhí)行時間:并行測試也就意味著多個測試可以在同一時間被同時執(zhí)行, 從而減少了整體測試所花費的時間。2)允許多個線程并行地測試同一個測試組件:有了這個特性,我們就能夠?qū)懗?相應(yīng)的測試用例來驗證應(yīng)用程序中包含多線程部分的代碼的正確性。以上特性被廣泛地應(yīng)用在qa領(lǐng)域的自動化功能測試方而。通過簡單的配置,qa 人員就可以很輕松地使得他們的測試用例

3、在多個瀏覽器或者操作系統(tǒng)屮并行地 執(zhí)行。testng提供了三種不同類型的配置方案來實現(xiàn)并行測試。如何并行地執(zhí)行測試方法tcstng為我們提供了多種方式來實現(xiàn)并行測試,其中一種就是每一個獨立的線 程分別執(zhí)行各自的測試方法。這種方式能夠顯著地減少測試執(zhí)行時間,這是因為 當(dāng)有越多的測試方法被并行執(zhí)行時,總體測試消耗時間將會越少。package com. howtodoinjava, parallei ism;import org. tcstng. annotations. aftermethod;import org. testng. annotations. beforemethod;import

4、 org. testng. annotations. test;public class parallelmethodtestbeforemethod public void beforemethod() long id = thread, currentthread() getld();system, out. println("before test-method. thread id is: + id); ©testpublic void lestmethodsone() long id = thread, currentthread() getld();system

5、, out. print in (''simple test-method one. thread id is: " + id);test public void testmethodstwo() long id = thread. currentthread(). getld();systemoutprintin("simple test-method two. thread id is: ” + id);©aftermethodpublic void aftermethod() long id = thread, currentthread()

6、 getld();system, out. println("after test-method. thread id is: " + id);上述測試類包含了兩個測試方法,每個測試方法在執(zhí)行吋都會在控制臺屮打印出 一條信息。每個測試方法以及它們各自的beforemehod> aftermethod方法都會 通過thread. currentthread. gettd()這段代碼打印出執(zhí)行該測試方法的線程的 ido在項目中新建一個名為mcthods-tcst-tcstng. xml的文件并將卜述代碼寫入該 文件中。<suite name二test-method

7、suite" parallel二methods" thread-count二2 > <test name二test-method test" group-by-instances二氣rue><classes><class name二com. howtodoinjava. parallelism. parallelmcthodtcstz, /></classes></test></suite>在eclipse屮選屮該文件并月以testng測試套件方式運行它。你將會在控制臺 屮看到以下輸岀信

8、息:before test-method. thread id is: 10before test-method. thread id is: 9simple test-method two.thread id is: 10simple testmethod one.thread id is: 9after testmethod.thread id is: 10after test-method.thread id is: 9注意:上述截圖中的threadtd可能與你木地控制臺中輸出的threadtd不同,這 是因為threadld的值是在程序運行時由jvm動態(tài)指派的。從上述測試結(jié)果中我們可以

9、很清晰地看出:上述兩個測試方法以及各口相應(yīng)的 beforemethod和aftermethod方法是在兩個獨立的線程屮執(zhí)行的。我們可以通 過在控制臺中輸出的threadld來證明這一點。如何并行地執(zhí)行測試類在下個例子小,我們會說明如何并行地執(zhí)行測試類:同一個測試組件(test execution)中的各個測試類將會在獨立的線程中并行地執(zhí)行。parallelclassestestone.javapublic class parallelclassestestonebcforcclasspublic void beforeclass() long id = thread. currentthrea

10、d(). getid();system, out. println("before test-class. thread id is: " + id);test public void testmethodone() long id 二 thread, currentthread() getld();system, out printin("sample test-method one. thread id is: " + id);test public void testmethodtwo() long id 二 thread, currcntthrc

11、ado. gctld();system, out. println(z,sample test-method two. thread id is: " + id);afterclasspublic void afterclass() long id = thread. currentthread(). getid(); system, out. println("after test-class. thread id is: " + id);parallelclassestesttwo. javapublic class parallelclassestesttw

12、o beforeclass public void beforeclass() long id = thread. currentthread (). getld();system.out. println(before test-class. thread id is: " + id); tcstpublic void tcstmcthodonc() long id = thread. currentthread (). getld();system.out. printin("sample test-method one. thread id is: " +

13、id);©testpublic void testmethodtwo() long id 二 thread.currentthread () getld();system, out println(sample test-method two. thread id is: +©afterclasspublic void afterclass() long id = thread, currentthread(). gettd();system, out println("after test-class. thread id is: " + id);在項

14、目中新建一個名為classes-lest-leslng. xml的文件并將下述代碼寫入該 文件中:<suitc namc二tcst-class suite" parallel二classes" thrcod-count二2 > <test name二test-class test" ><classes><classname二com.howtodoinjava, paral1 elism. paral1elclassestestone /><classname二com. howtodoinjava. parall

15、elism. parallelclassestesttwoz,/> </classes></test></suite>在eclipse中選中該文件并且以tcstng測試套件方式運行它。你將會在控制臺 屮看到以下輸出信息:before test-class.thread id is: 10before test-class.thread id is: 9sample test-method one.sample test-method one.sample test-method two.thread id is: 9thread id is: 10th

16、read id is: 10after test-class. thread id is: 10sample test-method two. thread id is: 9after test-class. thread id is: 9從上述測試結(jié)果中我們可以很清晰地看出:上述兩個測試類以及各口相應(yīng)的 bcforcclass和aftcrclass方法是在獨立的兩個線程中執(zhí)行的。我們可以通過 在控制臺屮輸岀的threadld來證明這一點。如何并行地執(zhí)行同一測試套件內(nèi)的各個測試組件接下來我們會一起學(xué)習(xí)如何并行地執(zhí)行同一個測試套件內(nèi)的各個測試組件,即各 個測試組件會分別在獨立的線程小執(zhí)行。pac

17、kage com howtodoinjava. parallelism;import org. testng. annotations. afterclass; import org. testng. annotations. aftertest;import org. testng. einnotations. beforeclass; import org. testng. emnoteitions. bcforctcst; import org. testng. annotations. parameters;import org.testng. annotations. test;pu

18、blic class parallelsuitetest string testname = “";sbeforetestparameters( "test-name" ) public void beforetest(string tcstname) this.testname = testname;long id = thread. currentthread(). getid();id is:threadsystem, out. println("before test " + testname + thread "+ id);

19、©beforeclass public void beforeclass() long id = thread, currentthread(). gettd(); system, out. println("before test-class " + testname + id is:+ id);testpublic void testmethodone() long id = thread.currentthread (). getid(); system, out. println("sample test-method + testname +

20、". thread id is: " + id);©afterclass public void afterclass() long id = thread, currentthread(). gettd(); system, out println("after test-method " + testname + thread id is: " + id);©aftertest public void aftertest() long id = thread. currentthread(). getid();syste

21、m, out. println("after test " + testname + thread id is: "+ id);在項目中新建一個名為suite-test-testng. xml的文件并將以下代碼寫入該文 件中:<su ite name=z,tes t - class suite" paral lel= tests t hread-co unt 二 2> <tcst neunc二test-class test 1><parameter name=,test-name,z value=,/test-method

22、one /> <classes><classname=z,com. howtodoinjava, paral 1 el i sm. paral lelsui tetest /></classcs></test><test name二test-cldss test 2><parameter name二test-name" value二test-method one /> <classes><classname二com. howtodoinjava. parallelism. parallel

23、suitetest,z /></classes></test></suite>在eclipse中選中該文件并且以tcstng測試套件方式運行它。你將會在控制臺 屮看到以下輸出信息:beforetest testone. threadid is:9beforetest testtwo. threadid is:10beforetest-classtest onethreadidis:9beforetest-classtest two.'threadidis:10sampletest-method test one.threadidis:9samp

24、letest-method test two.threadidis:10aftertest-methodtest two.threadidis:10aftertest-methodtest one.threadidis:9aftertest testone. threadid is:9aftertest testtwo. threadid is:10從上述測試結(jié)果中我們可以很清晰地看出:上述兩個測試組件是在獨立的兩個線 程中分別執(zhí)行的。我們可以通過在控制臺中輸出的threadld來證明這一點。如何配置一個需要在多線程環(huán)境中執(zhí)行的測試方法z前我們討論了如何并行(多線程)地執(zhí)行測試方法,測試類以及

25、測試組件。 testng同時也提供了一種靈活的方式來配置需耍在多線程環(huán)境下運行的測試方 法:只要在該測試方法的test注解上配置一些信息,我們就能啟用多線程模式。public class indopendcnttcst©test(threadpoolsize = 3, invocationcount = 6, timeout = 1000) public void testmethod()long id 二 thread, currentthrcod()gctld();system, out. println(z,test method executing on thread with id:" + id);上述測試方法是通過在test注解中配置threadpoolsize這個屈性來進入多線 程模式的。threadpoolsize被設(shè)為3,這就說明了該測試方法將會在三個不同的 線程中同時執(zhí)彳亍。剩余網(wǎng)個屬性:invocationcount配置的是該測試方法

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論