pytest(13)-多線程、多進程執行用例


有些項目的測試用例較多,測試用例時需要分布式執行,縮短運行時間。

pytest框架中提供可用於分布式執行測試用例的插件:pytest-parallel、pytest-xdist,接下來我們來學習這兩個插件的使用方法。

pytest-parallel

pytest-parallel 同時支持多線程、多進程兩種方式執行測試用例。

安裝

安裝命令:pip install pytest-parallel==0.0.10

注意,雖然最新的版本為 0.1.1,但在windows系統中需要指定安裝 0.0.10 版本,否則使用 pytest-parallel 參數執行用例時會報如下錯誤,其他系統暫未嘗試。

AttributeError: Can't pickle local object 'pytest_addoption.<locals>.label_type.<locals>.a_label_type'

參數說明

pytest-parallel 提供參數執行測試用例,示例如下:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py', '--workers=1', '--tests-per-worker=3'])

參數說明:

  1. --workers=n 指定運行的進程數為 n,默認為1,windows系統中只能為1
  2. --tests-per-worker=m 指定運行的線程數為 m
  3. 若兩個參數都指定,則表示啟動n個進程,每個進程最多啟動m線程執行,總線程數=進程數*線程數
  4. windows系統中不支持 --workers 取其他值,即只能為1,mac或linux系統中可取其他值

使用

接下來舉例進行說明。

測試用例模塊 test_case.py:

import pytest
import time

def test_01():
    print("執行test_01")
    time.sleep(3)

def test_02():
    print("執行test_02")
    time.sleep(4)

def test_03():
    print("執行test_03")
    time.sleep(5)

def test_04():
    print("執行test_04")
    time.sleep(6)

不使用 pytest-parallel 執行用例:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py'])
    

# 執行結果如下:
collected 4 items

testcase\test_case.py
.執行test_01
.執行test_02
.執行test_03
.執行test_04

============================= 4 passed in 18.05s ==============================

使用 pytest-parallel 分布式執行用例:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py', '--workers=1', '--tests-per-worker=3'])


# 執行結果如下:  
collected 4 items
pytest-parallel: 1 worker (process), 3 tests per worker (threads)

執行test_01
執行test_03執行test_02

.執行test_04
...
============================== 4 passed in 9.04s ==============================

從以上結果可以看出來:

  1. 不使用 pytest-parallel 執行 test_case.py 中的測試用例所用時間為18.05s

  2. 使用 pytest-parallel 執行 test_case.py 中的測試用例,當 --workers=1、--tests-per-worker=3 時所用時間為9.04s

pytest-xdist

pytest-xdist 只支持多進程執行測試用例,不支持多線程執行。

安裝

安裝命令:pip install pytest-xdist

參數說明

pytest-xdist 提供參數執行測試用例,示例如下:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py', '-n=4'])

參數說明:

  1. -n= 指定進程數,如 -n=4 表示開啟4個cpu進行執行測試用例。
  2. pytest-xdist支持windows系統使用,同樣也支持mac、linux。

使用

使用 pytest-xdist 分布式執行用例:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py', '-n=4'])

    
# 執行結果如下:
plugins: allure-pytest-2.9.45, forked-1.4.0, html-2.1.1, metadata-1.10.0, ordering-0.6, parallel-0.0.10, rerunfailures-9.1.1, xdist-2.5.0
gw0 I / gw1 I / gw2 I / gw3 I
gw0 [4] / gw1 [4] / gw2 [4] / gw3 [4]

....
============================== 4 passed in 7.19s ==============================

從結果可以看出來,使用 pytest-xdist 執行 test_case.py 中的測試用例,當 -n=4 時所用時間為7.19s

總結

  1. pytest-parallel 支持多線程執行用例,但在windows系統中只支持單個進程執行,即windows中只能--workers=1

  2. pytest-xdist 只支持多進程執行用例,但可以在windows系統中進行參數設置。

  3. 推薦使用 pytest-parallel,因為支持多線程執行,且自動化測試項目一般會搭建在mac或linux系統中運行,--workers 可以取別的值。

在使用過程中可能會遇到其他一些問題,歡迎評論探討。


免責聲明!

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



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