pytest-html 测试报告
安装: :pip3 install pytest-html 使⽤⽅式: 1.命令⾏⽅式 2.配置⽂件⾥ 命令: --html=⽣成报告路径/报告名字.html --junitxml=⽣成报告路径/报告名字.xml 应⽤: 命令⾏ pytest --html=xx.html # 在当前⽬录⽣成xx.html⽂件 pytest --html=./report/result.html # 当前⽬录会⾃动创建report⽂件夹,⽂件夹内⽣成result.html # 注意:等号两侧不能出现空格
pytest-rerunfailures 失败重试
安装:pip3 install pytest-rerunfailures 使⽤⽅式: 命令⾏ 配置⽂件 命令:--reruns n (n:重试的次数 ) 注意:重试插件不能和setup_class⼀起使⽤,⾮第⼀个测试⽅法失败,会出现逻辑错误
↑↑↑ 以上两种配置文件参考 ↑↑↑:
[pytest] # command line addopts = -s --html=./report/res.html --reruns 2 # search dir testpaths = ./scripts # search file python_files = test_rerun_pro.py # search class python_classes = Test* # search functions python_functions = test*
pytest-ordering 控制测试函数执⾏顺序:
安装: pip3 install pytest-ordering 使⽤: @pytest.mark.run(order=x) x:全为正数或全为负数,值越⼩,修饰的测试⽅法优先级越⾼ x:正数和负数同时存在时,正数修饰的测试⽅法优先级⾼ x:为0时,优先级最⾼ x:为负数时,优先级低于未被修饰的测试⽅法 x:为正数是,优先级⾼于未被修饰的测试⽅法
跳过测试方法:
@pytest.mark.skipif(condtion, reason) # 使⽤场景较少 condtion:必填 reson:必填 组合: 1.条件为True+有原因 执⾏跳过 2.条件为False+有原因 不执⾏跳过 3.只有原因 也执⾏跳过 4.⽆条件+⽆原因 也执⾏跳过 5.只有条件 报error错误
执⾏预期失败操作:
@pytest.mark.xfail(conditon, reason) # 使⽤场景较少 condtion:必填 reson:必填 注意:预期失败⽅法会默认执⾏⼀次测试⽅法 组合: 1.条件为真+有原因 执⾏预期失败 2.条件为假+有原因 不执⾏预期失败 3.⽆条件 + 有原因 执⾏预期失败 4.⽆条件 + ⽆原因 执⾏预期失败 5.有条件 + ⽆原因 报error异常 预期失败执⾏结果: 1.测试⽅法本身断⾔通过,结果 xpassed 2.测试⽅法本身断⾔失败, 结果 xfailed
parametrize参数化:
# 单个参数
@pytest.mark.parametrize("参数", [参数值1,参数值2,参数值3])
结果:
⽅法会运⾏列表⻓度
代码
import pytest
class Test_001:
# 遍历参数值列表,⽅法运⾏次数 = 列表⻓度
@pytest.mark.parametrize("data", [1, 2, 3, 4])
def test_001(self, data): # paramtrize参数名=测试⽅法参数名,名称必须⼀致
"""判断列表中值 不等于 2"""
print("ndata:", data)
assert data != 2
# 多个参数
@pytest.mark.parametrize("参数1, 参数2", [(参数1值,参数2值),(参数1值,参数2值)....])
代码:
import pytest
class Test_002:
# 多个参数在⼀个字符串中
# 测试⽅法参数位置可以调换
# parametrize中参数名 必须等于 测试⽅法参数名
@pytest.mark.parametrize("a,b,c", [(1, 2, 3), (3, 4, 5), (5, 6, 11)])
def test_002(self, a, b, c):
"""两个数相加的和计算 a+b == c"""
print("na:", a)
print("nb:", b)
print("nc:", c)
assert a + b == c



