TestNG 八 併發測試

一、 Concurrenttesting:

下面的例子是輸出進程ID,threadPoolSize用來指明線程池的大小,也就是併發的線程數目是多少

5次調用,有3個線程可調用

@Test(invocationCount = 5, threadPoolSize = 3,groups = { "t9"})
public void smallThreadPool() {
	System.out.println("Thread#: " +Thread.currentThread().getId());
}

頁面輸出

[TestNG]Running:

 E:\android\selenium\test_wdng_java\src\testing.xml

Thread#:13

Thread#:15

Thread#: 14         

Thread#:15

Thread#:15

===============================================

Suite

Totaltests run: 5, Failures: 0, Skips: 0

===============================================

若改成5次調用,有5個線程可調用

Thread#:10

Thread#:14

Thread#:12

Thread#:11

Thread#:13

二、Concurrentrunning of tests

TestNG可以以多線程的模式運行所有的test,這樣可以獲得最大的運行速度,最大限度的節約執行時間。當然,併發運行也是有代價的,就是需要我們的代碼是線程安全的。

併發運行測試的話,需要我們指定運行的配置文件,一個示例如下:

<suite name="Concurrent Suite" parallel="methods" thread-count="2" verbose="1" >
<>……<>
</suite>

1.Parallel=”methods”的意思是指TestNG會將method作爲併發的元子單位,即每個method運行在自己的thread中。如果parallel=”tests”,則指會將test 作爲併發的元子單位

2.Thread-count=”2”是指,運行的時候,併發度爲2,同時會有兩個線程在運行。

例如:

@Test(groups = { "t8"})
public void aThreadPool() {
	System.out.println("#ThreadA: " +Thread.currentThread().getId());
}
@Test(groups = { "t8"})
public void bThreadPool() {
	System.out.println("#ThreadB: " +Thread.currentThread().getId());
}
@Test(groups = { "t8"})
public void cThreadPool() {
	System.out.println("#ThreadC: " +Thread.currentThread().getId());
}

--------------------------------------------------------------------

<testname="Test" parallel="methods"thread-count="2">

 

輸出結果:

#ThreadB:11

#ThreadC:11

#ThreadA:10

===============================================

Suite

Totaltests run: 3, Failures: 0, Skips: 0

===============================================

 

改成<testname="Test" parallel="tests" thread-count="2">

頁面輸出(因爲aThreadPool(),bThreadPool(),cThreadPool()都在一個test下面)

#ThreadA:1

#ThreadB:1

#ThreadC:1

===============================================

Suite

Totaltests run: 3, Failures: 0, Skips: 0

===============================================

本文轉載自http://blog.sina.com.cn/bestfeiyong 


發佈了15 篇原創文章 · 獲贊 6 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章