pytest入门八:重复执行用例与测试报告

pytest-repeat

  • 场景
    • 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来
    • 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,或者针对某个模块的用例重复执行多次
  • 安装
  • 运行要求
    • Python 2.7、3.4+或PyPy
    • py.test 2.8或更高版本* 使用方式
  • 使用方式
    • count=2
    • count 2
    • pytest --html=report.html --self-contained-html -s --reruns=5 --count=2 test_request.py

重复测试直到失败(重要!!!)

如果需要验证偶现问题,可以一次又一次地运行相同的测试直到失败,这个插件将很有用

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

pytest --count=1000 -x test_file.py

from time import sleep
import pytest

def test_example():
    import random
    flag = random.choice([True, False])
    print(flag)
    sleep(1)
    assert flag

if __name__ == "__main__":
    pytest.main(['-v', '-s','--count=10', '-x','test_2.py'])

pytest -v -s --count=10 -x test_2.py 

如果要在代码中将某些测试用例标记为执行重复多次,可以使用 @pytest.mark.repeat(count)

 

pytest-html

  • 场景
    • 生成漂亮清晰的HTML报告
  • 安装
  • 运行要求
    • Python 3.6+

快速入门

pytest --html=report.html #会在当前目录下创建一个report.html的测试报告

合并CSS

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

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

注意事项

  • 在将文件或链接添加到独立报告时,插件会发出warnings;
  • 在html测试报告中可能无法按预期显示文件或链接 
from time import sleep
import pytest



def test_example():
    import random
    flag = random.choice([True])
    print(flag)
    sleep(1)
    assert flag

if __name__ == "__main__":
    pytest.main(['-v', '-s','--count=5', '-x','--html=report.html','--self-contained-html','test_2.py'])  #--html=report.html --self-contained-html

 pytest -v -s --count=10 -x --html=report.html --self-contained-html  test_2.py

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值