当前位置: 首页 > news >正文

辽宁建设科技信息网网站企业校园网站建设

辽宁建设科技信息网网站,企业校园网站建设,网站建设烟台,网站登录模版前言 平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟。如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候,我们会用测试人力成本换取时间成本,这个时候多找个小伙伴把任务…

前言

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

pytest 3.6.3
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 timedef 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 timedef 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.pyimport 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 itemsbaidu\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

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你! 

http://www.yayakq.cn/news/630024/

相关文章:

  • 做一个中英文网站的价格企业推广方式推荐隐迅推
  • 东莞个人做网站北京市建筑工程设计有限责任公司
  • 嘉兴网站建设的前景多多进宝怎么推广赚钱
  • 视觉品牌网站建设大型网站建设动力无限
  • 什么什么设计英文网站网站设计制作哪家好
  • 网站开发精灵wordpress怎么发文章
  • 哈尔滨 微网站设计成都行业网站
  • 站长怎么添加网站内容泉州模板建站定制
  • 中国建设银行泗水支行的网站收到短信说备案被退回但工信部网站上正常啊
  • 泊头网站建设的有哪些查询公司水利平台网站
  • 网站建设行业的趋势网站开发案例php
  • 深圳定制网站制作咨询电话如何做图让网站的图更清晰
  • 电商网站设计系统vi毕业设计代做网站
  • 网站做编辑赚钱台州网站排名优化公司
  • 网站推广好不好深圳装修设计公司排名前十强
  • 网站做支付需要什么备案广州建设网站的公司
  • 设计工作室网站首页做专利费减是哪个网站
  • 做产品网站营销推广简洁网站
  • 门户网站简介深圳品牌做网站公司
  • 模板做的网站如何下载asp艺术学校网站源码
  • 西充建设局网站网页设计包括哪些内容?
  • 莆田网站建设方案优化Wordpress官网类主题
  • 徐州云建站模板什么是网络营销设计
  • 做一个宣传网站的策划书永州城乡建设网站
  • 网站建设群标签好写什么黑客零基础入门
  • 外贸建站代理wordpress注册邮件设置密码
  • 望城建设局网站兼职 做网站
  • 无锡公共工程建设中心网站电话营销系统
  • 内江网站建设公司太原网站建设杰迅科技
  • 文库类网站建设建议及经验女生学ui设计难吗