美文网首页
PyTest常见插件介绍

PyTest常见插件介绍

作者: rookieyu | 来源:发表于2024-04-19 14:25 被阅读0次

pytest-ordering 用例顺序

  • 含义:
    修改系统默认用例执行顺序
  • 安装
pip install pytest-ordering

pytest默认按字母顺序去执行的(小写英文--->大写英文--->0-9数字)
用例之间的顺序是文件之间按照ASCLL码排序,文件内的用例按照从上往下执行。
改变测试用例的执行顺序,用法是加上装饰器

  • 在函数使用
import pytest


class TestDemo():
    @pytest.mark.run(order=2)
    def test01(self):
        print("测试方法1")

    @pytest.mark.run(order=1)
    def test02(self):
        print("测试方法2")
运行结果: image.png

可见测试方法2先执行方法1后执行的。

  • 在类使用
    demo1:
import pytest


@pytest.mark.run(order=2)
class TestDemo():
    def test01(self):
        print("demo1:测试方法1")

    def test02(self):
        print("demo1:测试方法2")

demo2:

import pytest


@pytest.mark.run(order=1)
class TestDemo():
    def test01(self):
        print("demo2:测试方法1")

    def test02(self):
        print("demo2:测试方法2")
运行结果: image.png

由此可见demo2文件先执行。

  • ordering其他运行方式
方式一
第一个执行:@ pytest.mark.first
第二个执行:@ pytest.mark.second
倒数第二个执行:@ pytest.mark.second_to_last
最后一个执行:@pytest.mark.last
方式二
第一个执行:@ pytest.mark.run('first')
第二个执行:@ pytest.mark.run('second')
倒数第二个执行:@ pytest.mark.run('second_to_last')
最后一个执行:@ pytest.mark.run('last')

pytest-dependency 用例依赖

  • 含义:
    主要解决用例之间的依赖关系。如果依赖的上下文失败后续的用例会被标识为跳过执行。
    比如说在tpshop登录的时候,我们需要先获取验证码之后才能登录,那么我可以使用这个方法。
  • 安装
pip install pytest-dependency
  • 使用
import pytest


class TestDemo():
    @pytest.mark.dependency(name="test01")  # 打标记
    def test01(self):
        print("demo1:测试方法1")
        assert 1 == 2

    @pytest.mark.dependency(depends=["test01"])  # 依赖于test01,test01断言成之后才会继续执行
    def test02(self):
        print("demo1:测试方法2")
        assert 1 == 1
image.png

可以看到由于test01断言失败,test02没有执行。

pytest-repeat 重复跑

  • 含义:
    有些用例需要多次执行,比如说登录的时候,密码错误三次会锁定用户信息,那么可以使用重复跑
  • 安装
pip install pytest-repeat
  • 使用一:装饰器 @pytest.mark.repeat(次数)
import pytest


class TestDemo():
    @pytest.mark.repeat(3)
    def test01(self):
        print("重复执行---------")

运行效果:


image.png
  • 使用二:命令行参数 --count=3

    • 在终端运行中配置
    pytest test_demo.py --count=3
    
    • 在main中配置
import pytest


class TestDemo():
  def test01(self):
      print(f"重复执行---------")

if __name__ == '__main__':
  pytest.main(['-sv', '--count=5', __file__])
  • 在ini文件中配置
[pytest]
;添加测试命令行命令
addopts = -s --count=3
;文件搜索路径
testpaths = ./
;文件名称 * 表示多个任意字符
python_files = test*.py
;类名称
python_classes = Test*
;方法名称
python_functions = test*
运行效果: image.png

pytest-rerunfailures 用例失败重跑

  • 含义:
    有的时候脚本运行速度比较快,可能出现异常情况,在多试几次的时候就可以了,那么可以使用当前测试方法
  • 安装
pip install pytest-rerunfailures
  • 使用方式一:装饰器(常用)
import pytest
import random

class TestDemo():
    @pytest.mark.flaky(reruns=50, reruns_delay=2)  # 重跑50次,每次间隔2s
    def test01(self):
        num = random.randint(1, 6)
        print("预期结果为:",num)
        assert 3 == num
执行结果: image.png

可见执行多次用例,最后通过了。

pytest-assume 断言后继续跑

  • 含义:
    当断言失败后用例会停止运行,有可能遇到一种情况,就是你某个断言执行失败也想要做下去。
  • 安装
 pip install pytest-assume
  • 使用
import pytest


class TestDemo():
    def test01(self):
        print("断言前")
        # assert 3 == 2
        pytest.assume(3==2)
        print("断言后")
效果: image.png

可以断言前和断言后都执行了,并且用例断言也是错误的。

pytest-xdist 分布式执行

  • 含义:
    让自动化测试用例可以分布式执行,从而大大节省测试时间。pytest-xdist 是属于进程级别的并发。
    (1)独立运行:用例之间是独立的,并且没有依赖关系,还可以完全独立运行。
    (2)随机执行:用例执行不强制按顺序执行,支持顺序执行或随机执行。
    (3)不影响其他用例:每个用例都能重复运行,运行结果不会影响其他用例
  • 安装
pip install pytest-xdist
  • 使用
pytest test_demo1.py -n NUM    # NUM表示并发的进程数
  • 说明:
    ① 多cpu并行执行用例,直接加个-n参数即可,后面num参数就是并行数量,比如num设置为3
    ② -n auto : 自动侦测系统里的CPU数目
    分配策略
    ① --dist=loadfile 按照测试文件分配:用例之间依赖关系只能存在一个文件中
    ② --dist=loadscope 按照模块分配:用例之间依赖关系只能存在一个模块
    ③ 分配按照测试用例:每条用例之间不能有依赖关系

pytest-xfail 预期失败

  • 含义:
    我知道这个用例是因为某些原因会失败的
  • 使用:
import pytest


class TestDemo():
    @pytest.mark.xfail(True, reason='预期失败,结果成功')
    def test01(self):
        assert 2 == 2

    @pytest.mark.xfail(True, reason='预期失败,结果失败')
    def test02(self):
        assert 3 == 2

    @pytest.mark.xfail(False, reason='预期成功,结果失败')
    def test03(self):
        assert 3 == 2

    @pytest.mark.xfail(False, reason='预期成功,结果成功')
    def test04(self):
        assert 2 == 2
  • 结果: image.png
  • 说明:
    第一个参数 True False,当如果是True的时候xfail会加到被测函数上,如果是False的时候则不会生效
    因此我们可以看到当前用例中 01 04 是可以通过的,02断言失败但是回回显示错误,03会显示断言没有通过。

相关文章

网友评论

      本文标题:PyTest常见插件介绍

      本文链接:https://www.haomeiwen.com/subject/fgdyxjtx.html