日常咱们手工测试用例很是多时,好比有1千条用例,假设每一个用例执行须要1分钟。若是一个测试人员执行须要1000分钟才能执行完,当项目很是紧急的时候,
咱们会用测试人力成本换取时间成本,这个时候多找个小伙伴把任务分红2部分,因而时间缩减一半。若是是十我的一块儿执行,1000个用例理论上只需100分钟就能完成,时间缩短到了1/10。大大节省的测试时间,为项目节省了时间成本。html
一样道理,当咱们测试用例很是多的时候,一条条执行,很显然会比较慢,那么如何让测试用例并行执行呢,这就是咱们接下来要讲的pytest分布式执行插件pytest-xdistpython
cmd里面使用pip安装,目前版本号Version: 1.23.2linux
pip install pytest-xdist
>pip show pytest-xdist Name: pytest-xdist Version: 1.23.2 Summary: pytest xdist plugin for distributed testing and loop-on-failing modes Home-page: https://github.com/pytest-dev/pytest-xdist Author: holger krekel and contributors Author-email: pytest-dev@python.org,holger@merlinux.eu License: MIT Location: e:\python36\lib\site-packages Requires: execnet, pytest-forked, six, pytest
pytest-xdist官网地址:【Home-page: https://github.com/pytest-dev/pytest-xdist】git
该pytest-xdist插件扩展了一些独特的测试执行模式pytest:github
测试运行并行化:若是有多个CPU或主机,则能够将它们用于组合测试运行。会加快运行速度web
多平台覆盖:您能够指定不一样的Python解释器或不一样的平台,并在全部平台上并行运行测试。
在远程运行测试以前,pytest有效地将您的程序源代码“rsyncs”到远程位置。报告全部测试结果并显示给您的本地终端。您能够指定不一样的Python版本和解释器。
若是您想知道pytest-xdist如何在幕后工做,能够看这里【OVERVIEW】session
多cpu并行执行用例,直接加个-n参数便可,后面num参数就是并行数量,好比num设置为3分布式
pytest -n 3
运行如下代码,项目结构以下oop
web_conf_py是项目工程名称
│ conftest.py
│ __init__.py
│
├─baidu
│ │ conftest.py
│ │ test_1_baidu.py
│ │ test_2.py
│ │ __init__.py
│
├─blog
│ │ conftest.py
│ │ test_2_blog.py
│ │ __init__.py
代码参考:测试
# web_conf_py/conftest.py import pytest @pytest.fixture(scope="session") def start(): print("\n打开首页") return "yoyo" # web_conf_py/baidu/conftest.py import pytest @pytest.fixture(scope="session") def open_baidu(): print("打开百度页面_session") # web_conf_py/baidu/test_1_baidu.py import pytest import time def test_01(start, open_baidu): print("测试用例test_01") time.sleep(1) assert start == "yoyo" def test_02(start, open_baidu): print("测试用例test_02") time.sleep(1) assert start == "yoyo" if __name__ == "__main__": pytest.main(["-s", "test_1_baidu.py"]) # web_conf_py/baidu/test_2.py import pytest import time def test_06(start, open_baidu): print("测试用例test_01") time.sleep(1) assert start == "yoyo" def test_07(start, open_baidu): print("测试用例test_02") time.sleep(1) assert start == "yoyo" if __name__ == "__main__": pytest.main(["-s", "test_2.py"]) # web_conf_py/blog/conftest.py import pytest @pytest.fixture(scope="function") def open_blog(): print("打开blog页面_function") # web_conf_py/blog/test_2_blog.py import pytest import time def test_03(start, open_blog): print("测试用例test_03") time.sleep(1) assert start == "yoyo" def test_04(start, open_blog): print("测试用例test_04") time.sleep(1) assert start == "yoyo" def test_05(start, open_blog): '''跨模块调用baidu模块下的conftest''' print("测试用例test_05,跨模块调用baidu") time.sleep(1) assert start == "yoyo" if __name__ == "__main__": pytest.main(["-s", "test_2_blog.py"])
正常运行须要消耗时间: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 ===========================
设置并行运行数量为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] 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