




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、tcstng系列教程:并行執(zhí)行測(cè)試-編程開(kāi)發(fā)技術(shù)testng系列教程:并行執(zhí)行測(cè)試木文由importnew楊昆,侖翻譯自lokesh g叩ta。歡迎加入翻譯小組。轉(zhuǎn)載請(qǐng)見(jiàn)文末要 求。并行(多線程)技術(shù)在軟件術(shù)語(yǔ)里被定義為軟件、操作系統(tǒng)或者程序可以并行地 執(zhí)行另外一段程序屮多個(gè)部分或者子組件的能力。testng允許我們以并行(多 線程)的方式來(lái)執(zhí)行測(cè)試。這就意味著基于testng測(cè)試組件的配置,多個(gè)線程 可以被同吋啟動(dòng)然后分別執(zhí)行各自的測(cè)試方法。相對(duì)于傳統(tǒng)的單線程執(zhí)行測(cè)試的 方式,這種多線程方式擁有很大的優(yōu)勢(shì),主要是它可以減少測(cè)試運(yùn)行時(shí)間,并且 可以驗(yàn)證某段代碼在多線程環(huán)境中運(yùn)行的止確性。冃錄1
2、. 并行執(zhí)行測(cè)試的優(yōu)勢(shì)2. 如何并行地執(zhí)行測(cè)試方法3. 如何并行地執(zhí)行測(cè)試類4. 如何并行地執(zhí)行同一測(cè)試套件內(nèi)的各個(gè)測(cè)試組件5. 如何配置需要在多線程環(huán)境屮執(zhí)行的測(cè)試方法并行執(zhí)行測(cè)試的優(yōu)勢(shì)并行(多線程)執(zhí)行測(cè)試可以給用戶帶來(lái)很多好處,主要包括以下兩點(diǎn):1)減少了執(zhí)行時(shí)間:并行測(cè)試也就意味著多個(gè)測(cè)試可以在同一時(shí)間被同時(shí)執(zhí)行, 從而減少了整體測(cè)試所花費(fèi)的時(shí)間。2)允許多個(gè)線程并行地測(cè)試同一個(gè)測(cè)試組件:有了這個(gè)特性,我們就能夠?qū)懗?相應(yīng)的測(cè)試用例來(lái)驗(yàn)證應(yīng)用程序中包含多線程部分的代碼的正確性。以上特性被廣泛地應(yīng)用在qa領(lǐng)域的自動(dòng)化功能測(cè)試方而。通過(guò)簡(jiǎn)單的配置,qa 人員就可以很輕松地使得他們的測(cè)試用例
3、在多個(gè)瀏覽器或者操作系統(tǒng)屮并行地 執(zhí)行。testng提供了三種不同類型的配置方案來(lái)實(shí)現(xiàn)并行測(cè)試。如何并行地執(zhí)行測(cè)試方法tcstng為我們提供了多種方式來(lái)實(shí)現(xiàn)并行測(cè)試,其中一種就是每一個(gè)獨(dú)立的線 程分別執(zhí)行各自的測(cè)試方法。這種方式能夠顯著地減少測(cè)試執(zhí)行時(shí)間,這是因?yàn)?當(dāng)有越多的測(cè)試方法被并行執(zhí)行時(shí),總體測(cè)試消耗時(shí)間將會(huì)越少。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);上述測(cè)試類包含了兩個(gè)測(cè)試方法,每個(gè)測(cè)試方法在執(zhí)行吋都會(huì)在控制臺(tái)屮打印出 一條信息。每個(gè)測(cè)試方法以及它們各自的beforemehod> aftermethod方法都會(huì) 通過(guò)thread. currentthread. gettd()這段代碼打印出執(zhí)行該測(cè)試方法的線程的 ido在項(xiàng)目中新建一個(gè)名為mcthods-tcst-tcstng. xml的文件并將卜述代碼寫(xiě)入該 文件中。<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測(cè)試套件方式運(yùn)行它。你將會(huì)在控制臺(tái) 屮看到以下輸岀信
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可能與你木地控制臺(tái)中輸出的threadtd不同,這 是因?yàn)閠hreadld的值是在程序運(yùn)行時(shí)由jvm動(dòng)態(tài)指派的。從上述測(cè)試結(jié)果中我們可以
9、很清晰地看出:上述兩個(gè)測(cè)試方法以及各口相應(yīng)的 beforemethod和aftermethod方法是在兩個(gè)獨(dú)立的線程屮執(zhí)行的。我們可以通 過(guò)在控制臺(tái)中輸出的threadld來(lái)證明這一點(diǎn)。如何并行地執(zhí)行測(cè)試類在下個(gè)例子小,我們會(huì)說(shuō)明如何并行地執(zhí)行測(cè)試類:同一個(gè)測(cè)試組件(test execution)中的各個(gè)測(cè)試類將會(huì)在獨(dú)立的線程中并行地執(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);在項(xiàng)
14、目中新建一個(gè)名為classes-lest-leslng. xml的文件并將下述代碼寫(xiě)入該 文件中:<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測(cè)試套件方式運(yùn)行它。你將會(huì)在控制臺(tái) 屮看到以下輸出信息: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從上述測(cè)試結(jié)果中我們可以很清晰地看出:上述兩個(gè)測(cè)試類以及各口相應(yīng)的 bcforcclass和aftcrclass方法是在獨(dú)立的兩個(gè)線程中執(zhí)行的。我們可以通過(guò) 在控制臺(tái)屮輸岀的threadld來(lái)證明這一點(diǎn)。如何并行地執(zhí)行同一測(cè)試套件內(nèi)的各個(gè)測(cè)試組件接下來(lái)我們會(huì)一起學(xué)習(xí)如何并行地執(zhí)行同一個(gè)測(cè)試套件內(nèi)的各個(gè)測(cè)試組件,即各 個(gè)測(cè)試組件會(huì)分別在獨(dú)立的線程小執(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);在項(xiàng)目中新建一個(gè)名為suite-test-testng. xml的文件并將以下代碼寫(xiě)入該文 件中:<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測(cè)試套件方式運(yùn)行它。你將會(huì)在控制臺(tái) 屮看到以下輸出信息: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從上述測(cè)試結(jié)果中我們可以很清晰地看出:上述兩個(gè)測(cè)試組件是在獨(dú)立的兩個(gè)線 程中分別執(zhí)行的。我們可以通過(guò)在控制臺(tái)中輸出的threadld來(lái)證明這一點(diǎn)。如何配置一個(gè)需要在多線程環(huán)境中執(zhí)行的測(cè)試方法z前我們討論了如何并行(多線程)地執(zhí)行測(cè)試方法,測(cè)試類以及
25、測(cè)試組件。 testng同時(shí)也提供了一種靈活的方式來(lái)配置需耍在多線程環(huán)境下運(yùn)行的測(cè)試方 法:只要在該測(cè)試方法的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);上述測(cè)試方法是通過(guò)在test注解中配置threadpoolsize這個(gè)屈性來(lái)進(jìn)入多線 程模式的。threadpoolsize被設(shè)為3,這就說(shuō)明了該測(cè)試方法將會(huì)在三個(gè)不同的 線程中同時(shí)執(zhí)彳亍。剩余網(wǎng)個(gè)屬性:invocationcount配置的是該測(cè)試方法
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 教育心理學(xué)在提升學(xué)生社交技巧中的應(yīng)用
- 游戲化在幼兒體育教育中的應(yīng)用研究
- 教育大數(shù)據(jù)提升教學(xué)質(zhì)量的有效工具
- 企業(yè)信息安全文化建設(shè)的重要性與實(shí)踐
- 醫(yī)學(xué)心理學(xué)與疾病預(yù)防的交叉研究
- 智慧城市公共安全信息系統(tǒng)的基石建設(shè)
- 基于教育技術(shù)的全球混學(xué)創(chuàng)新與實(shí)踐分析報(bào)告
- 逆向思維培訓(xùn)課件圖片
- 抖音商戶退款退貨處理效率考核制度
- 抖音商戶直播情感連接策略制度
- 老年康養(yǎng)服務(wù)中心項(xiàng)目可行性研究報(bào)告寫(xiě)作參考范文
- 生物質(zhì)中纖維素、半纖維素和木質(zhì)素含量的測(cè)定
- 枸杞采摘合同
- 渦流探傷儀設(shè)計(jì)方案
- 張家界船舶工業(yè)項(xiàng)目建議書(shū)【模板范本】
- 國(guó)家種畜禽生產(chǎn)經(jīng)營(yíng)許可證管理系統(tǒng)操作指南
- 石油化工管道施工方案
- 四川SG-008技術(shù)、經(jīng)濟(jì)簽證核定單(共2頁(yè))
- 來(lái)料檢驗(yàn)報(bào)告模板
- 腳手架搭設(shè)施工安全技術(shù)交底(共2頁(yè))
- 水利工程旁站監(jiān)理方案【精選文檔】
評(píng)論
0/150
提交評(píng)論