前言

平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟。如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候,
我们会用测试人力成本换取时间成本,这个时候多找个小伙伴把任务分成2部分,于是时间缩减一半。如果是十个人一起执行,1000个用例理论上只需100分钟就能完成,时间缩短到了1/10。大大节省的测试时间,为项目节省了时间成本。

同样道理,当我们测试用例非常多的时候,一条条执行,很显然会比较慢,那么如何让测试用例并行执行呢,这就是我们接下来要讲的pytest分布式执行插件pytest-xdist

pytest-xdist

cmd里面使用pip安装,目前版本号Version: 1.23.2

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】

该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,大大的缩短了用例时间

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

转载自:https://www.cnblogs.com/yoyoketang/p/9775646.html

转载于:https://www.cnblogs.com/yxfeng/p/10395860.html

pytest-xdist分布式执行测试用例相关推荐

  1. Java多进程测试用例_Pytest xdist/Pytest并行多进程执行测试用例,pytestxdistpytestparallel...

    如果想分布式执行用例,用例设计必须遵循以下原则: 1.用例之间都是独立的, 2.用例a不要去依赖用例b 3.用例执行没先后顺序, 4.随机都能执行每个用例都能独立运行成功每个用例都能重复运行,不影响其 ...

  2. Python测试框架pytest(22)插件 - pytest-xdist(分布式执行)

    目录 1.安装 2.示例 3.原理和流程 4.解决:多进程运行次数 当测试用例非常多的时候,一条条按顺序执行测试用例,是很浪费测试时间的.这时候就可以用到 pytest-xdist,让自动化测试用例可 ...

  3. Python单元测试框架之pytest---如何执行测试用例

    介绍   pytest是一个成熟的全功能的Python测试工具,可以帮助你写出更好的程序. 适合从简单的单元到复杂的功能测试 l 模块化parametrizeable装置(在2.3,持续改进) l 参 ...

  4. python+appium自动化测试-重复执行测试用例

    在功能测试过程中,经常会遇到一些偶然出现的Bug,需要通过重复执行用例来复现问题,那么,在自动化测试的过程中,对于一些偶然出现的Bug,也可以针对单个用例,或者针对某个模块的用例重复执行多次来复现. ...

  5. Pytest如何重复执行N次脚本

    [原文链接]Pytest如何重复执行N次脚本 在有些场景下,希望将自动化脚本重复执行多次,比如想看看自动化脚本的稳定性,还比如想利用功能测试用例直接进行压力测试,即对某一些脚本反复执行从而对应用产生压 ...

  6. Pytest如何并发执行自动化脚本

    [原文链接]Pytest如何并发执行自动化脚本 当自动化脚本数量非常多的时候,全量脚本的执行耗时很长,此时就希望自动化脚本能够并发执行,第三方插件pytest-xdist 能很好的支持此功能,pyte ...

  7. locust入门 -6 无图模式和分布式执行

    无图模式 无图模式即不通过UI界面进行执行locust测试用例.locust提供了命令参数,我们只需要在执行时添加一个--headless标签即可 "Disable the web inte ...

  8. locust入门 —— 无图模式和分布式执行

    无图模式 无图模式即不通过UI界面进行执行locust测试用例.locust提供了命令参数,我们只需要在执行时添加一个--headless标签即可 " Disable the web int ...

  9. jmeter脚本结合ant执行测试用例并生成测试报告

    前言:本篇主要讲ant执行测试用例并生成测试报告,至于jmeter脚本本篇文章不多说,不懂的小伙伴可查看我的上篇文章:jmeter环境配置.使用以及参数化之CSV Data Set Config 1. ...

最新文章

  1. TensorFlow 发布新版本v1.9(附应用实践教程)
  2. Oracle中不同条件的日期查询
  3. 2017qcon大会的一点想法(安全人才如何不被淘汰?)
  4. 错误 - 无法访问IIS元数据库
  5. zblog php 当前位置,zblogphp导航当前页突出显示的方法
  6. 打开Mybatis核心配置文件SqlMapConfig.xml的代码提示功能
  7. C++ Primer 5th笔记(3)字符串、向量和数组:字符串
  8. 【Oracle】PL/SQL 显式游标、隐式游标、动态游标
  9. 2015/10/9 Python核编初级部分学习总结
  10. [渝粤教育] 西安工业大学 园中画境 中国古典园林造园艺术 参考 资料
  11. Codeforces 1144D Deduction Queries 并查集
  12. 分享一个JAVA专业接口开发利器,牛牛牛新鲜出炉!!!
  13. 专家观点:Docker 架构优缺点大剖析
  14. 数商云供应链集采管理系统解决方案:产品特色、功能、架构全解析
  15. express文件上传中间件Multer详解
  16. bfm人脸模型和3DDFA及其变种
  17. H5页面调起关注微信公众号的方法
  18. 奇闻 为什么Xenophon DAO 社区人人都想戴 绿帽子
  19. 关于Kurento 和 WebRTC-Kurento学习(一)
  20. python3:urllib/urllib2

热门文章

  1. 服务器Raid数据恢复成功案例和raid 5数据恢复算法原理
  2. 4 weekend110的hive入门
  3. Java学习笔记----线程
  4. MATLAB学习笔记(八)
  5. android广告页白屏_Android 启动页面与广告页面的实现-Go语言中文社区
  6. python123第七周小测验_python+request+untitest的接口自动化测试
  7. 批量文件替换_CAD图形文件中如何快速批量替换文字?【AutoCAD教程】
  8. ToPILImage
  9. 步骤一:入门Linux基础\01.Linux 简介和安装\第2章 Ubuntu系统的安装
  10. linux mysql 加固_apache+mysql+php安全加固配置说明