一、pytest-sugar:进度条

安装:pip install pytest-sugar

  1. def test_s1():
  2. print("用例 11:登录之后其它动作 111")
  3. def test_s2():
  4. print("用例 22:登录之后其它动作 222")
  5. def test_s3():
  6. print("用例 33:登录之后其它动作 333")
  7. 运行:pytest -s test_demo.py

image.png

二、pytest-rerunfailures:失败重跑

安装:pip install pytest-rerunfailures
这个插件是为了实现我们有时候希望能够重试失败的用例 ,来提升测试的稳定性用的
命令如下:pytest test_demo.py —reruns=重试次数

  1. #!/usr/bin/env python3
  2. # !coding:utf-8
  3. import pytest
  4. import random
  5. def test_simple_assume():
  6. # 每次case运行的值为1或者2,具有随机性
  7. r = random.randint(1,8)
  8. assert r == 1

image.png

三、pytest-repeat:重复运行

安装:pip install pytest-repeat
命令:pytest test_demo.py —count=重复次数

  1. #!/usr/bin/env python3
  2. # !coding:utf-8
  3. import pytest
  4. import random
  5. def test_simple_assume():
  6. # 每次case运行的值为1或者2,具有随机性
  7. r = random.randint(1,8)
  8. assert r == 1

image.png

四、pytest_xdist:并行任务

安装:pip install pytest_xdist
这个插件可以帮助我们实现并发执行测试用例,使用方法也是非常简单
运行:pytest test_demo -n=并发数
pytest系列——常用的第三方插件 - 图4
可以看到,我们使用pytest-xdist确实能够提升不小的测试效率,但是这里还是要温馨提示一下,我们在使用pytest-xdist插件的时候,测试用例的编写一定要遵循一个原则:测试用例和测试用例之间不能有任何依赖,否则并行执行的话就会乱掉

五、pytest-ordering:编排用例

安装:pip install pytest-ordering
大家都知道在pytest中,默认的顺序是按照编写用例的前后顺序,如果想自定义这个用例的执行顺序,就需要用到此插件

  1. import time
  2. import pytest
  3. @pytest.mark.run(order=4)
  4. def test_001():
  5. time.sleep(1)
  6. @pytest.mark.run(order=3)
  7. def test_002():
  8. time.sleep(1)
  9. @pytest.mark.run(order=2)
  10. def test_003():
  11. time.sleep(1)
  12. @pytest.mark.run(order=1)
  13. def test_004():
  14. time.sleep(1)

image.png

六、pytest-timeout:超时机制

安装:pip install pytest-timeout
利用这个插件,我们可以控制用例的执行超时,如果超时的话就记用例失败
运行:pytest test_demo.py timeout=秒数

  1. #!/usr/bin/python3
  2. # -*- coding: UTF-8 -*-
  3. """
  4. @author:durant.zeng
  5. @Description:描述
  6. @file:test_baidu.py
  7. @time:2020/12/17
  8. """
  9. import time
  10. import requests
  11. def test_baidu():
  12. time.sleep(5)
  13. r = requests.get(url='https://baidu.com')
  14. print(r.elapsed.total_seconds())

image.png

七、pytest-tmreport:测试报告

安装:pip install pytest-tmreport
这个测试插件是基于vue+element ui开发的一套模板插件,非常的精美(不过注意需要完美)
运行:pytest —pytest-tmreport-name=测试报告名称.html
pytest系列——常用的第三方插件 - 图7