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】]( )
该pytest-xdist插件扩展了一些独特的测试执行模式pytest:
* 测试运行并行化:如果有多个CPU或主机,则可以将它们用于组合测试运行。会加快运行速度
* --looponfail:在子进程中重复运行测试。每次运行之后,pytest会等待,直到项目中的文件发生更改,然后重新运行以前失败的测试。
重复此过程直到所有测试通过,之后再次执行完整运行。
* 多平台覆盖:您可以指定不同的Python解释器或不同的平台,并在所有平台上并行运行测试。
在远程运行测试之前,pytest有效地将您的程序源代码“rsyncs”到远程位置。报告所有测试结果并显示给您的本地终端。您可以指定不同的Python版本和解释器。
如果您想知道pytest-xdist如何在幕后工作,可以看这里[【OVERVIEW】]( )
## 并行测试
多cpu并行执行用例,直接加个-n参数即可,后面num参数就是并行数量,比如num设置为3
>
> pytest -n 3
>
>
>
运行以下代码,项目结构如下
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,大大的缩短了用例时间
![img](https://img-blog.csdnimg.cn/img_convert/4483a9efc86b1f4c8ab626efa3f64473.png)
![img](https://img-blog.csdnimg.cn/img_convert/4af70296c1b14fb02c91dc0a8e4984e6.png)
![img](https://img-blog.csdnimg.cn/img_convert/b8de06f2614abbacab9b9b8c5f46762e.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**
tgYk-1719233660123)]
[外链图片转存中...(img-zmgXBLGE-1719233660124)]
[外链图片转存中...(img-tS528Cmv-1719233660124)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**