網(wǎng)站首頁 編程語言 正文
前言
如果想分布式執(zhí)行用例,用例設(shè)計(jì)必須遵循以下原則:
1、用例之間都是獨(dú)立的,
2、用例a不要去依賴用例b
3、用例執(zhí)行沒先后順序,
4、隨機(jī)都能執(zhí)行每個(gè)用例都能獨(dú)立運(yùn)行成功每個(gè)用例都能重復(fù)運(yùn)行,不影響其它用例
這跟就我們平常多個(gè)人工測試一樣,用例都是獨(dú)立的,可以隨機(jī)分配不同人員執(zhí)行,互相不依賴,用例之間也不存在先后順序
一、pytest-parallel
安裝:pip install pytest-parallel
常用參數(shù)配置:
- --workers=n:多進(jìn)程運(yùn)行需要加此參數(shù), n是進(jìn)程數(shù)。默認(rèn)為1
- --tests-per-worker=n:多線程需要添加此參數(shù),n是線程數(shù)
如果兩個(gè)參數(shù)都配置了,就是進(jìn)程并行,每個(gè)進(jìn)程最多n個(gè)線程,總線程數(shù):進(jìn)程數(shù)*線程數(shù)
注意:在windows上進(jìn)程數(shù)永遠(yuǎn)為1。
需要使用 if name == “main”:,在dos中運(yùn)行會(huì)報(bào)錯(cuò)
#!/usr/bin/env python # _*_ coding: utf-8 _*_ # @project : API_Service # @File : test_1.py # @Date : 2021/6/15 3:07 下午 # @Author : 李文良 # demo: import pytest def test_01(): print('測試用例1操作') def test_02(): print('測試用例2操作') def test_03(): print('測試用例3操作') def test_04(): print('測試用例4操作') def test_05(): print('測試用例5操作') def test_06(): print('測試用例6操作') def test_07(): print('測試用例7操作') def test_08(): print('測試用例8操作') if __name__ == "__main__": pytest.main(["-s", "test_1.py",'--workers=2', '--tests-per-worker=4'])
二、pytest-xdist
安裝:pip install pytest-xdist
不支持多線程
常用參數(shù)配置:
- -n=*:*代表進(jìn)程數(shù)
多cpu并行執(zhí)行用例,直接加個(gè)-n參數(shù)即可,后面num參數(shù)就是并行數(shù)量,比如num設(shè)置為3
- -n auto 自動(dòng)偵測系統(tǒng)里的CPU數(shù)目
- -n num 指定運(yùn)行測試的處理器進(jìn)程數(shù)
三、對(duì)比說明
pytest-parallel比pytst-xdist相對(duì)好用,功能支持多。
pytst-xdist不支持多線程,而pytest-parallel支持python3.6及以上版本,如果想做多進(jìn)程并發(fā)在linux或者mac上做,在Windows上不起作用(Workers=1),如果做多線程linux/mac/windows平臺(tái)都支持,進(jìn)程數(shù)為workers的值。
pytest-parallel常用配置命令如下
- –workers (optional) *:多進(jìn)程運(yùn)行需要加此參數(shù), *是進(jìn)程數(shù)。默認(rèn)為1。
- –tests-per-worker (optional) *:多線程運(yùn)行, *是每個(gè)worker運(yùn)行的最大并發(fā)線程數(shù)。默認(rèn)為1
pytest test.py --workers 3:3個(gè)進(jìn)程運(yùn)行
pytest test.py --tests-per-worker 4:4個(gè)線程運(yùn)行
pytest test.py --workers 2 --tests-per-worker 4:2個(gè)進(jìn)程并行,且每個(gè)進(jìn)程最多4個(gè)線程運(yùn)行,即總共最多8個(gè)線程運(yùn)行。
四、特別注意
1、pytest-parallel的workers參數(shù)在windows系統(tǒng)下永遠(yuǎn)是1,在linux和mac下可以取不同值。
2、pytest-parallel加了多線程處理后,最后執(zhí)行時(shí)間是運(yùn)行時(shí)間最長的線程的時(shí)間。
3、在windows下想用多進(jìn)程的選pytst-xdist; 想用多線程的選pytest-parallel
原文鏈接:https://blog.csdn.net/weixin_44275820/article/details/112169328
相關(guān)推薦
- 2023-03-20 C#?BeginInvoke實(shí)現(xiàn)異步編程方式_C#教程
- 2024-01-12 間隙鎖(Gap Lock)
- 2022-10-03 Matlab實(shí)現(xiàn)鼠標(biāo)光標(biāo)變成愛心和瞄準(zhǔn)鏡形狀_C 語言
- 2022-06-22 Golang中堆排序的實(shí)現(xiàn)_Golang
- 2022-10-31 .Net中的Http請求調(diào)用詳解(Post與Get)_實(shí)用技巧
- 2023-07-22 macos使用idea進(jìn)行配置找不到隱藏目錄(/usr/local/)
- 2022-10-13 解析React中useMemo與useCallback的區(qū)別_React
- 2022-04-08 Swift使用表格組件實(shí)現(xiàn)單列表_Swift
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支