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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

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),
        (-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值