pytest-xdist分布式执行测试用例
前言
平常我们手工测试用例非常多时,比如有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
pytest-xdist分布式执行测试用例的更多相关文章
- pytest多进程/多线程执行测试用例
前言: 实际项目中的用例数量会非常多,几百上千:如果采用单进程串行执行的话会非常耗费时间.假设每条用例耗时2s,1000条就需要2000s $\approx$ 33min:还要加上用例加载.测试前/后 ...
- Python单元测试框架之pytest 1 ---如何执行测试用例
From: https://www.cnblogs.com/fnng/p/4765112.html 介绍 pytest是一个成熟的全功能的Python测试工具,可以帮助你写出更好的程序. 适合从简 ...
- Selenium_多线程执行测试用例
多线程执行测试用例 这里以百度搜索为例,通过不同的浏览器来启动不同的线程. #!/usr/bin/env python # _*_ coding:utf-8 _*_ __author__ = 'Yin ...
- pytest文档27-pytest分布式执行(pytest-xdist)
前言 平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟.如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候, 我们会用测试人力成本换取时间成本,这个时候多找 ...
- pytest分布式执行(pytest-xdist)
前言 平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟.如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候, 我们会用测试人力成本换取时间成本,这个时候多找 ...
- Pytest(15)pytest分布式执行用例
前言 平常我们功能测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟,如果单个测试人员执行需要1000分钟才能跑完 当项目非常紧急时,会需要协调多个测试资源来把任务分成两部分,于是执行时间 ...
- pytest测试框架 -- skip跳过执行测试用例
跳过执行测试用例 1.@pytest.mark.skip(reason=" ") -- 跳过执行测试函数 可传入一个非必须参数reason表示原因 import pytest@ ...
- Pytest(16)随机执行测试用例pytest-random-order
前言 通常我们认为每个测试用例都是相互独立的,因此需要保证测试结果不依赖于测试顺序,以不同的顺序运行测试用例,可以得到相同的结果. pytest默认运行用例的顺序是按模块和用例命名的 ASCII 编码 ...
- 『德不孤』Pytest框架 — 6、Mark分组执行测试用例
目录 1.Pytest中的Mark介绍 2.Mark的使用 3.Mark的注册和使用 4.使用Mark完成失败重试 5.扩展 1.Pytest中的Mark介绍 Mark主要用于在测试用例/测试类中给用 ...
随机推荐
- .Net新利器Rider的破解安装与使用
准备 介绍 Rider 是 JetBrains 提供的一款用于 .Net 开发的 IDE,相对于 VS,它显得更加轻量(才 500m 左右),并且不管是提示功能还是流畅度都不逊色于 VS 且某方面可能 ...
- 2019春第六周作业Compile Summarize
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 在这里 我在这个课程的目标是 能够熟练掌握指针的用法 这个作业在那个具体方面帮助我实现目标 对指针的使用更加得心应手 参考文献与网址 C语 ...
- react创建项目很慢,最后提示fetch failed的解决方法
$ cnpm install -g create-react-app //创建react全局变量 $ create-react-app my-app //创建一个react项目 国内使用 npm 速度 ...
- python 选课系统
couser.py: import sys,osBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))sys.pa ...
- vector创建二位数组
默认初始化vector vector<vevtor<int> > arr(row, vector<int>(col, 0)); //指定行大小为row,列为col, ...
- go basic
go time and rand: package main import ( "fmt" "math/rand" "time" ) fun ...
- python精进之路 -- open函数
下面是python中builtins文件里对open函数的定义,我将英文按照我的理解翻译成中文,方便以后查看. def open(file, mode='r', buffering=None, enc ...
- Jmeter之Redis读写
Jmeter之Redis读写 奔跑的小小鱼 关注 0.2 2019.03.21 18:25* 字数 1330 阅读 45评论 0喜欢 1 Jmeter插件访问Redis共有3种方式: 1)通过自已 ...
- 2018-2019-2 《网络对抗技术》Exp2 后门原理与实践
2018-2019-2 <网络对抗技术>Exp2 后门原理与实践 1. 后门原理与实践实验说明及预备知识 一.实验说明 任务一:使用netcat获取主机操作Shell,cron启动 (0. ...
- 破圈法求解最小生成树c语言实现(已验证)
破圈法求解最小生成树c语言实现(已验证) 下面是算法伪代码,每一个算法都取一个图作为输入,并返回一个边集T. 对该算法,证明T是一棵最小生成树,或者证明T不是一棵最小生成树.此外,对于每个算法,无论它 ...