Pytest框架之 - 常用插件

常用插件

Pytest提供了很多功能插件, 插件列表网址包含很多插件包,大家可依据工作的需求选择使用, 这边介绍三款常用插件


多CPU运行Case pytest-xdist

当cases量很多时,运行时间也会变的很长,如果想缩短脚本运行的时长,就可以用多进程来运行。插件安装如下:

pip install -U pytest-xdist -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

自定义进程数运行测试用例: -n=auto(自动检测到系统的CPU核数)    -n=N(指定运行的CPU核数为N)

    ......
if __name__ == "__main__":
    pytest.main(["-n=N"])

pytest-xdist默认是无序执行的,可以通过参数--dist=loadscope来控制顺序:

1. 将同一个模块.py下的函数和同一个测试类class下的方法来分组,然后将每个测试组发给可以执行的worker,确保同一个组的测试用例在同一个进程中执行

2. 目前无法自定义分组,按类class分组优先于按模块module分组


Case失败重跑 - pytest-rerunfailures

在做UI测试时, 短时的网络波动会导致case运行失败, 这并非是我们期望的结果, 此时就可通过重试运行cases的方式来解决问题

插件安装如下:

pip install -U pytest-rerunfailures -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

自定义重跑次数N与重跑延迟时间M/秒

    ......
if __name__ == "__main__":
    pytest.main(["--reruns=N", "--reruns-delay=M"]) 

注意事项:如果指定了用例的重新运行次数,则在命令行添加--reruns对这些用例是不会生效的 

兼容性问题:

1. 不可以和fixture装饰器一起使用: @pytest.fixture() 

2. 该插件与pytest-xdist的 --looponfail 标志不兼容

3. 该插件与核心--pdb标志不兼容


HTML测试报告 pytest-html

pytest-HTML是一个pytest用于生成XML/HTML测试报告的插件

插件安装如下:

pip install pytest-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

指定HTML测试报告文件路径

    ......
if __name__ == "__main__":
    pytest.main(["--html", "report/report.html"])

上面命令生成的报告css是独立的, 分享报告的时候样式会丢失, 为了更好的分享发邮件展示报告, 可以把css样式合并到HTML里

pytest --html=report/report.html --self-contained-html

多重校验 pytest-assume

Pytest中可用assert断言, 但一个失败后面的断言将不再执行;pytest-assume可以实现对多个断言多重校验的

插件安装如下:

pip install pytest-assume -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

代码示例:

import pytest

def test_01():
    print("=======> start test_01")
    pytest.assume(1 == 2)               # 多重断言
    pytest.assume(2 == 2)
    pytest.assume(3 == 2)
    print("=======> end test_01")

if __name__ == "__main__":
    pytest.main(["-s", "test_pytest_assume.py"])

重复执行测试 pytest-repeat

测试过程中会出现一些偶现的bug, 对于这种问题我们会针对此用例反复执行多次, 最终复现出问题来; pytest-repeat插件就能起到这个作用: 重复执行测试用例

参数--count=N表示执行N次

注:将pytest的 -x 选项与pytest-repeat结合使用,以强制测试运行程序在第一次失败时停止

插件安装如下:

pip install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

代码示例:

import pytest

a = 0

# 重复执行用例插件
def test_01():
    global a
    a += 1
    print(a)
    assert (a != 10)

if __name__ == "__main__":
    pytest.main(["-s", "test_pytest_repeat.py", "--count=20", "-x"])

自定义用例执行顺序 pytest-ordering

pytest默认按字母顺序去执行的(小写英文--->大写英文--->0-9数字)

用例之间的顺序是文件之间按照ASCLL码排序,文件内的用例按照从上往下执行

setup_module->setup_claas->setup_function->testcase->teardown_function->teardown_claas->teardown_module

但可以通过第三方插件pytest-ordering实现自定义用例执行顺序

注:一旦设置了自定义的执行顺序,就必须得延伸@pytest.mark.run(order=1)里面得order字段

插件安装如下:

pip install pytest-ordering -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

示例代码如下:

#!/usr/bin/python3
# coding=utf-8
# Author: 文

import time
import pytest

class Test_Order():
    @pytest.mark.run(order=3)       # 第三执行
    def test_01(self):
        time.sleep(1)
        print("=======> test_01")

    @pytest.mark.run(order=1)       # 第一执行
    def test_02(self):
        time.sleep(1)
        print("=======> test_02")

    @pytest.mark.run(order=2)       # 第二执行
    def test_03(self):
        time.sleep(1)
        print("=======> test_03")

if __name__ == "__main__":
    pytest.main(["test_pytest_order.py", "-s"])

若是未通过@pytest.mark.run(order=n)指定执行顺序则执行顺序是test_01->test_02->test_03

如上通过@pytest.mark.run(order=n)指定执行顺序(按test_02->test_03->test_01),运行结果如下:

G:\python.exe F:/xxx/test_pytest_order.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: F:\xxx, configfile: pytest.ini
plugins: allure-pytest-2.8.22, assume-2.3.3, forked-1.3.0, html-3.0.0, metadata-1.11.0, ordering-0.6, repeat-0.9.1, rerunfailures-9.1.1, xdist-2.1.0
collected 3 items
test_pytest_order.py =======> test_02
.=======> test_03
.=======> test_01
.
============================== 3 passed in 3.03s ==============================
Process finished with exit code 0

方式一

1. 第一个执行:@pytest.mark.first  或者 @pytest.mark.run('first')

2. 第二个执行:@pytest.mark.second  或者 @pytest.mark.run('second')

3. 倒数第二个执行:@pytest.mark.second_to_last 或者 @pytest.mark.run('second_to_last')

4. 最后一个执行:@pytest.mark.last 或者 @pytest.mark.run('last')

方式二

1. 第一个执行:@pytest.mark.run(order=1)

2. 第二个执行:@pytest.mark.run(order=2)

3. 倒数第二个执行:@pytest.mark.run(order=-2)

4. 最后一个执行:@pytest.mark.run(order=-1)

 ~ 不积跬步无以至千里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SitVen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值