testng之多線程執行(並發執行)


多線程作用:當我們用例較多時通過多線程並行執行測試用例可以提高運行效率。

注意:當我們用例數很少時(比如只有10條),使用多線程運行反而運行效率降低,因為線程的創建和關閉也需要時間。

 

1、不同的測試方法使用不同的線程--方法級別(最常用)

xml文件配置:suite套件配置:

parallel="methods" :表示多線程是在測試方法級別

thread-count="5":表示共創建5個線程

 

總的意思:總共創建5個線程,不同的方法在5個不同的線程上執行。

 

//測試類Testa
public class Testa {

    @Test
    public void testa_1(){
        System.out.println("testaaa-1級別1執行");
        System.out.println("testaaa-1線程id:"+Thread.currentThread().getId());
    }

    @Test
    public void testa_2(){
        System.out.println("testaaa-2級別2執行");
        System.out.println("testaaa-2線程id:"+Thread.currentThread().getId());
    }

}

//測試類Testb

public class Testb {

    @Test
    public void testb_1(){
        System.out.println("testbbb_1級別1執行了");
        System.out.println("testbbb-1線程id:"+Thread.currentThread().getId());
    }

    @Test
    public void testb_2(){
        System.out.println("testbbb_2級別2執行了");
        System.out.println("testbbb-2線程id:"+Thread.currentThread().getId());
    }
}

//通過xml文件配置多線程
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="套件" parallel="methods" thread-count="5">
    <test name="運行所有用例">
        <classes>
            <class name="com.test.finance.testcase.Testa"/>
            <class name="com.test.finance.testcase.Testb"/>
        </classes>
    </test>
</suite>

// ==運行結果==\\

testbbb_2級別2執行了
testaaa-2級別2執行
testbbb-2線程id:14
testaaa-1級別1執行
testbbb_1級別1執行了
testaaa-1線程id:11
testaaa-2線程id:12
testbbb-1線程id:13

 

2、線程級別tests,即不同的test tag下使用不同的線程,同一個test tag下使用相同的線程。

 

//xml文件配置,Testa Testb類屬於一個tests tag
//                    Testc 類屬於一個tests tag

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="套件" parallel="tests" thread-count="5">

    <test name="第一波測試用例">
        <classes>
            <class name="com.test.finance.testcase.Testa"/>
            <class name="com.test.finance.testcase.Testb"/>
        </classes>
    </test>

    <test name="第二波測試用例">
        <classes>
            <class name="com.test.finance.testcase.Testc"/>
        </classes>
    </test>
</suite>


//==運行結果==\\ testaaa-1級別1執行 testccc_1級別1執行了 testaaa-1線程id:11 testccc-1線程id:12 testaaa-2級別2執行 testaaa-2線程id:11 testccc_2級別2執行了 testccc-2線程id:12 testbbb_1級別1執行了 testbbb-1線程id:11 testbbb_2級別2執行了 testbbb-2線程id:11

 

3、線程級別classes,即不同的測試類下使用不同的線程,同一個測試類class下使用相同的線程。

// 通過xml文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="套件" parallel="classes" thread-count="5">

    <test name="第一波測試用例">
        <classes>
            <class name="com.test.finance.testcase.Testa"/>
            <class name="com.test.finance.testcase.Testb"/>
        </classes>
    </test>

</suite>


//===運行結果==\\
testbbb_1級別1執行了
testaaa-1級別1執行
testbbb-1線程id:12
testaaa-1線程id:11

testbbb_2級別2執行了
testbbb-2線程id:12

testaaa-2級別2執行
testaaa-2線程id:11

 

4、直接通過@Test參數控制單個測試方法執行次數+使用的線程數

如果我們需要多次執行摸個測試方法,這時候我們可以通過改用例的@Test中的參數來控制對改用例使用多線程

@Test(invocation=執行次數, threadPoolSize=線程池線程數)

//測試方法

    @Test(invocationCount = 3,threadPoolSize = 3)
    public void testa_1(){
        System.out.println("testaaa-1級別1執行");
        System.out.println("testaaa-1線程id:"+Thread.currentThread().getId());
    }

//單獨運行這個方法的結果
testaaa-1級別1執行
testaaa-1線程id:13
testaaa-1級別1執行
testaaa-1線程id:12
testaaa-1級別1執行
testaaa-1線程id:11

 上面的案例:testa_1()測試方法調用3次,並且啟動3個線程,所以3次執行使用的是不同的線程執行。

 

總結:

1、通過xml文件的配置可以實現對整理測試用例的多線程配置

parallel="methods" :測試方法級別

parallel="tests" : test tag級別

parallel="classes" : class級別

2、通過@Test(invocation, threadPoolSize)參數控制單個用例的多線程執行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM