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

安装: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),

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。