Java多进程测试用例_Pytest xdist/Pytest并行多进程执行测试用例,pytestxdistpytestparallel...
如果想分布式執(zhí)行用例,用例設計必須遵循以下原則:
1.用例之間都是獨立的,
2.用例a不要去依賴用例b
3.用例執(zhí)行沒先后順序,
4.隨機都能執(zhí)行每個用例都能獨立運行成功每個用例都能重復運行,不影響其它用例
這跟就我們平常多個人工測試一樣,用例都是獨立的,可以隨機分配不同人員執(zhí)行,互相不依賴,用例之間也不存在先后順序
pytest-xdist/pytest-parallel安裝及查看是否安裝
安裝
pip install pytest-xdist
pip install pytest-parallel
查看
pip show pytest-xdist
pytest-xdist
多cpu并行執(zhí)行用例,直接加個-n參數(shù)即可,后面num參數(shù)就是并行數(shù)量,比如num設置為3
-n auto 自動偵測系統(tǒng)里的CPU數(shù)目
-n num 指定運行測試的處理器進程數(shù)
> pytest -n 3
正常運行需要消耗時間:7.12 seconds
E:\YOYO\web_conf_py>pytest
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23.2, metadata-1.7.0, html-1.19.0, forked-0.2
collected 7 items
baidu\test_1_baidu.py .. [ 28%]
baidu\test_2.py .. [ 57%]
blog\test_2_blog.py ... [100%]
========================== 7 passed in 7.12 seconds ===========================
設置并行運行數(shù)量為3,消耗時間:3.64 seconds,大大的縮短了用例時間
E:\YOYO\web_conf_py>pytest -n 3
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23.2, metadata-1.7.0, html-1.19.0, forked-0.2
gw0 [7] / gw1 [7] / gw2 [7] #####代表啟動進程數(shù)
scheduling tests via LoadScheduling
....... [100%]
========================== 7 passed in 3.64 seconds ===========================
使用pytest-xdist插件也能生成html報告,完美支持pytest-html插件
pytest -n 3 --html=report.html --self-contained-html
對比說明:
pytest-parallel
比
pytst-xdist
相對好用,功能支持多。
pytst-xdist不支持多線程,而
pytest-parallel
支持
python3.6
及以上版本,如果想做多進程并發(fā)在
linux
或者
mac
上做,在
Windows
上不起作用(
Workers=1
),如果做多線程
linux/mac/windows
平臺都支持,進程數(shù)為
workers
的值。
pytest-parallel常用配置命令如下
–workers (optional)
*:多進程運行需要加此參數(shù), *是進程數(shù)。默認為1。
–tests-per-worker (optional)
*:多線程運行, *是每個worker運行的最大并發(fā)線程數(shù)。默認為1
pytest test.py --workers 3
:3個進程運行
p
ytest test.py --tests-per-worker 4
:4個線程運行
pytest test.py --workers 2 --tests-per-worker 4
:2個進程并行,且每個進程最多4個線程運行,即總共最多8個線程運行。
【特別注意】:
1.pytest-parallel的workers參數(shù)在windows系統(tǒng)下永遠是1,在linux和mac下可以取不同值。
2…pytest-parallel加了多線程處理后,最后執(zhí)行時間是運行時間最長的線程的時間。
3.在windows下想用多進程的選pytst-xdist; 想用多線程的選pytest-parallel
擴展:
重復執(zhí)行用例:
使用
--count
命令行選項指定要運行測試用例和測試次數(shù)
py.test --count=10 test_file.py
pytest baidu/test_1_baidu.py -s --count=5
會第一個用例運行
5
次 然后運行第二個用例5次 如果希望 第一個用例 第二個用例這樣按順序重復
5
次 要用到
--repeat-scope
pytest tearr_function.py -s --count=5 --repeat-scope=session
如果要在代碼中標記要重復多次的用例 可以使用
@pytest.mark.repeat(count)
這樣執(zhí)行用例時候,就不用帶上
--count
參數(shù),只針對
test_02
重復執(zhí)行
5
次
pytest tearr_function.py -s
果您正在嘗試診斷間歇性故障,那么一遍又一遍地運行相同的測試直到失敗是有用的。您可以將
pytest
的
-x
選項與
pytest-repeat
結合使用,以強制測試運行器在第一次失敗時停止。例如:
> py.test --count=1000 -x test_file.py
這將嘗試運行
test_file.py
1000次,但一旦發(fā)生故障就會停止
總結
以上是生活随笔為你收集整理的Java多进程测试用例_Pytest xdist/Pytest并行多进程执行测试用例,pytestxdistpytestparallel...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android list 替换元素_Py
- 下一篇: 龙芯下代CPU优化架构:12nm工艺下单