pytest多進程運行用例,縮短測試時間

-n 表示多進程執行用例,對線程數進行設置

  1. 指定線程數,如下所示‘2'表示2個進程同時執行用例,數值要小於等於執行機的cpu核數
  2. auto,執行用例的時候會使用與計算機具有的cpu內核一樣多的進程
前置條件:安裝pytest-xdist 插件,安裝命令“pip install pytest-xdist”
注意:每個進程執行的測試用例是隨機的,不可控,所以測試用例要解耦

 1 import os
 2 
 3 import pytest
 4 
 5 # 整個項目的運行入口
 6 if __name__ == '__main__':
 7     # -n 多進程執行用例,指定進程數
 8     pytest.main(['-n','2','--alluredir','./result','--clean-alluredir'])
 9 
10     # 使用與計算機具有的cpu內核一樣多的進程
11     # pytest.main(['-n', 'auto', '--alluredir', './result', '--clean-alluredir'])
12 
13     # 根據上一步生成的數據源,生成可讀報告
14     os.system('allure generate ./result/ -o ./report_allure/ --clean')

 

指定線程數,並行執行用例:

 

 

使用與計算機具有的cpu內核一樣多的進程並行執行用例:

 

 


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章