pytest教程之重复执行插件pytest-repeat(多条断言有1条失败后仍继续运行)

平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。这个场景可以借助pytest-repeat插件来重复执行。

1. 安装插件

# 安装pytest-repeat模块
pip3 install pytest-repeat

2. 用例demo

import pytest

class TestRepeat:
    def test_example(self):
        import random
        flag = random.choice([True, False])
        print("用例执行")
        print(flag)
        assert flag

3. 如何运行

# 运行命令
# --count指定运行次数,配合pytest的-x参数一起使用,强制用例运行时在第一次失败后终止运行。如果不加-x参数,则会运行完指定的count数
pytest --count=100 -x test_pytest_repeat.py
4. 运行结果
test_pytest_repeat.py:9: AssertionError
---------------------------------------------------------------------------------------------------------- Captured stdout call -----------------------------------------------------------------------------------------------------------
用例执行
False
=================================================================================================== 1 failed, 3 passed in 0.12 seconds =================================================================================================

5. 另一种使用方式,使用@pytest.mark.repeat(count)装饰器

class TestRepeat:
    @pytest.mark.repeat(5)
    def test_repeat(self):
        print("测试用例执行")

5.1 如何运行

# 运行命令
pytest -s test_pytest_repeat.py
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值