平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些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