前言

平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟。如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候,

我们会用测试人力成本换取时间成本,这个时候多找个小伙伴把任务分成2部分,于是时间缩减一半。如果是十个人一起执行,1000个用例理论上只需100分钟就能完成,时间缩短到了1/10。大大节省的测试时间,为项目节省了时间成本。

  • pytest 3.6.3
  • pytest-allure-adaptor 1.7.10
  • pytest-forked 0.2
  • pytest-html 1.19.0
  • pytest-metadata 1.7.0
  • pytest-repeat 0.7.0
  • pytest-xdist 1.23.2

同样道理,当我们测试用例非常多的时候,一条条执行,很显然会比较慢,那么如何让测试用例并行执行呢,这就是我们接下来要讲的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

---------------------------------pytest结合selenium自动化完整版-------------------------

全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

作者:上海-悠悠 QQ交流群:874033608

也可以关注下我的个人公众号:yoyoketang

pytest文档27-pytest分布式执行(pytest-xdist)的更多相关文章

  1. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  2. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  3. pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)

    前言 使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being mad ...

  4. Uncode-Schedule首页、文档和下载 - 分布式任务调度组件 - 开源中国社区

    Uncode-Schedule首页.文档和下载 - 分布式任务调度组件 - 开源中国社区 分布式任务调度组件 Uncode-Schedule

  5. 4: ES内执行Groovy脚本,做文档部分更新、执行判断改变操作类型

    ES有内置的Groovy脚本执行内核,可以在命令的Json内嵌入Groovy脚本语句   前提数据:           

  6. pytest文档52-命令行参数--setup-show查看fixture的执行过程

    前言 使用命令行运行 pytest 用例的时候,看不到 fixture 的执行过程. 如果我们想知道fixture的执行过程和先后顺序,可以加上 --setup-show 命令行参数,帮助查看 fix ...

  7. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  8. pytest文档1-环境准备与入门

    前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...

  9. pytest文档56-插件打包上传到 pypi 库

    前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...

随机推荐

  1. vue.js学习 自定义过滤器使用(1)

    在这个教程中,我们将会通过几个例子,了解和学习VueJs的过滤器.我们参考了一些比较完善的过滤器,比如orderBy 和 filterBy.而且我们可以链式调用过滤器,一个接一个过滤.因此,我们可以定 ...

  2. Android 客户端 okhttp3 与服务器之间的双向验证

    [原文]https://blog.csdn.net/leng_wen_rou/article/details/58596142 本篇是Android 客户端基于okhttp3的网络框架 和后台服务器之 ...

  3. PHP递归遍历数组 不破坏数据结构 替换字符

    代码如下: <?php $arr = array('0'=>array("<小刚>","<小晓>","<小飞 ...

  4. scrapy-redis 更改队列和分布式爬虫

    这里分享两个技巧 1.scrapy-redis分布式爬虫 我们知道scrapy-redis的工作原理,就是把原来scrapy自带的queue队列用redis数据库替换,队列都在redis数据库里面了, ...

  5. Bootstrap入门六:表单

    表单主要包含表单域.输入框.下拉框.单选框.多选框和按钮等控件. 1.基本实例 单独的表单控件会被自动赋予一些全局样式.所有设置了 .form-control 类的 <input>.< ...

  6. python脚本获取本机公网ip

    1.获取公网IP地址方式,访问:http://txt.go.sohu.com/ip/soip 2.python脚本实现: #!/usr/bin/python # -*- coding:utf8 -*- ...

  7. [leetcode sort]57. Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  8. 安装部署VMware vSphere 5.5文档 (6-4) 安装配置DB数据库

    部署VMware vSphere 5.5 实施文档 ########################################################################## ...

  9. 利用python实现新浪微博爬虫

    第一个模块,模拟登陆sina微博,创建weiboLogin.py文件,输入以下代码: #! /usr/bin/env python # -*- coding: utf-8 -*- import sys ...

  10. 鸟哥的私房菜:Bash shell(四)-Bash shell的使用环境

    Bash shell(四)-Bash shell的使用环境   是否记得我们登入主机的时候,屏幕上头会有一些说明文字,告知我们的 Linux 版本啊什么的, 还有,登入的时候,我们还可以给予使用者一些 ...