当前位置:   article > 正文

Python自动化——pytest常用插件详解_pytest.delay

pytest.delay

前言

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 ) 指定某个用例

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. """
  4. @author:chenshifeng
  5. @file:test_calc2.py
  6. @time:2020/09/16
  7. """
  8. import pytest
  9. @pytest.mark.parametrize('a,b,result', [
  10. (1, 1, 3),
  11. (2, 2, 4),
  12. (100, 100, 200),
  13. (0.1, 0.1, 0.2),
  14. (-1, -1, -2)
  15. ], ids=['int', 'int', 'bignum', 'float', 'fushu']) # 参数化
  16. def test_add(a, b, result):
  17. # cal = Calculator()
  18. assert result == a + b

命令行执行:

pytest test_calc2.py --reruns 5 --reruns-delay 1 -vs

结果如下:

  1. ============================================================================= test session starts =============================================================================
  2. 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
  3. cachedir: .pytest_cache
  4. rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
  5. plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2
  6. collected 5 items
  7. test_calc2.py::test_add[int0] RERUN
  8. test_calc2.py::test_add[int0] RERUN
  9. test_calc2.py::test_add[int0] RERUN
  10. test_calc2.py::test_add[int0] RERUN
  11. test_calc2.py::test_add[int0] RERUN
  12. test_calc2.py::test_add[int0] FAILED
  13. test_calc2.py::test_add[int1] PASSED
  14. test_calc2.py::test_add[bignum] PASSED
  15. test_calc2.py::test_add[float] PASSED
  16. test_calc2.py::test_add[fushu] PASSED
  17. ================================================================================== FAILURES ===================================================================================
  18. _______________________________________________________________________________ test_add[int0] ________________________________________________________________________________
  19. a = 1, b = 1, result = 3
  20. @pytest.mark.parametrize('a,b,result', [
  21. (1, 1, 3),
  22. (2, 2, 4),
  23. (100, 100, 200),
  24. (0.1, 0.1, 0.2),
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/51981
推荐阅读
相关标签
  

闽ICP备14008679号