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