pytest04、pytest常用插件

01、插件地址:https://plugincompat.herokuapp.com/

02、生成测试报告 插件

--安装
	pip install pytest-html
	
--生成测试报告:
	pytest --html=测试报告路径/***.html

03、在pytest.ini中加入报告生成命令

addopts = -s --html=test_hello.html

--直接执行 pytest即可
--如果这里加上了 -s 则不会生成报告
--只有参数为addopts = --html=test_hello.html 才会生成报告

04、改变测试函数的执行顺序 插件

--pip install pytest-ordering  安装执行顺序插件

--具体使用的测试执行案例如下:
import pytest


# 都是正数的话,越小越先执行,比如order=3, 2, 1的话,order为1的先执行
# 都是负数的话,值越小越先执行
# 0和负数的话,0先执行,剩下的数值越小越先执行
# 0和正数的话,0先执行,剩下的数越小越先执行
# 0 正数 负数,0先执行,正数再执行,负数最后
# 执行顺序:0 > 较小的正数 > 较大的正数 > 较小的负数 > 较大的负数
@pytest.mark.run(order=3)
def test_a():  # 函数测试用例必须test开头
    print("\n----> test_a")
    assert 1

@pytest.mark.run(order=-1)
def test_b():
    print("\n----> test_b")
    assert 1

@pytest.mark.run(order=0)
def test_c():
    print("\n----> test_c")
    assert 1

05、失败重试插件 – 重试全部脚本

--pip install pytest-rerunfailures

--pytest --reruns n  # n表示重新执行的次数,这个会把失败的所有脚本重新执行

--或者在pytest.ini中addopts参数上加入参数 --reruns 2  --reruns-delay m [重试时间为m秒]
import pytest


class Test_ABD:
	def setup_class(self):
		print("-----> setup_class")

    def teardown_class(self):
		print("----> teardown_class")

	def test_a(self):
		print("----> test_a")
		assert 1

	def test_b(self):
		print("----> test_b")
		assert 0

if __name__ == "__main__":
	pytest.main("-s test_abc.py")

06、重试单个测试用例

import pytest


def test_a():  # 函数测试用例必须test开头
    print("\n----> test_a")
    assert 1

def test_b():
    print("\n----> test_b")
    assert 1

# 这里使用装饰器 flaky ,失败后重试5次
# 重试时间为1s
@pytest.mark.flaky(reruns=5, reruns_delay=1)
def test_c():
    print("\n----> test_c")
    assert 0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值