前言
Pytest是Python的一种单元测试框架,与unittest相比,使用起来更简洁、效率更高,也是目前大部分使用python编写测试用例的小伙伴们的第一选择了。
除了框架本身提供的功能外,Pytest还支持上百种第三方插件,良好的扩展性可以更好的满足大家在用例设计时的不同需求。本文将为大家详细介绍下面6项常用的插件。废话就不多说了我们直接开始吧。

1、失败重跑 pytest-rerunfailures
安装:pip install pytest-rerunfailures
使用:pytest test_class.py --reruns 5 --reruns-delay 1 -vs (失败后重新运行5次,每次间隔1秒)
@pytest.mark.flaky(reruns = 5 ,reruns-delay = 1 ) 指定某个用例
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_calc2.py
@time:2020/09/16
"""
import pytest
@pytest.mark.parametrize('a,b,result', [
(1, 1, 3),
(2, 2, 4),
(100, 100, 200),
(0.1, 0.1, 0.2),
(-1, -1, -2)
], ids=['int', 'int', 'bignum', 'float', 'fushu']) # 参数化
def test_add(a, b, result):
# cal = Calculator()
assert result == a + b
命令行执行:
pytest test_calc2.py --reruns 5 --reruns-delay 1 -vs
结果如下:
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2
collected 5 items
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] FAILED
test_calc2.py::test_add[int1] PASSED
test_calc2.py::test_add[bignum] PASSED
test_calc2.py::test_add[float] PASSED
test_calc2.py::test_add[fushu] PASSED
================================================================================== FAILURES ===================================================================================
_______________________________________________________________________________ test_add[int0] ________________________________________________________________________________
a = 1, b = 1, result = 3
@pytest.mark.parametrize('a,b,result', [
(1, 1, 3),
(2, 2, 4),
(100, 100, 200),
(0.1, 0.1, 0.2),
(-1, -1, -2)
], ids=['int', 'int', 'bignum', 'float', 'fushu']) # 参数化
def test_add(a, b, result):
cal = Calculator()
> assert result == cal.add(a, b)
E assert 3 == 2

本文详细介绍了Pytest的六个常用插件:pytest-rerunfailures(失败重跑)、pytest-assume(多重校验)、pytest-ordering(设定执行顺序)、pytest-dependency(用例依赖)、pytest-xdist(分布式测试)和pytest-html(生成报告)。这些插件扩展了Pytest的功能,以满足不同测试需求,如处理失败测试、设定用例顺序、依赖管理、并行测试和报告生成。
最低0.47元/天 解锁文章
2170

被折叠的 条评论
为什么被折叠?



