pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)
2023-06-13 09:15:28 时间
1 使用场景
- 为了排查某些问题,我们可能需要重复去执行某个用例进行问题分析;
- 一些场景下,自动化测试时候某个用例时好时坏,为了排查这类问题,我们可能需要对用例进行重复执行。
2 pytest-repeat插件
- 为了重复执行用例,我们可以使用
pytest-repeat
插件; - 详细参考官方教程:https://pypi.org/project/pytest-repeat/
2.1 环境要求
- Python 2.7, 3.5+ 或 PyPy;
- pytest 3.6或更高版本。
2.2 插件安装
pip3 install pytest-repeat
3 pytest-repeat使用
3.1 重复测试直到失败
- 将
pytest
的-x
选项与pytest-repeat
结合使用,以强制测试运行程序在第一次失败时停止;
pytest --count=5 -x test_pytest_repeat.py
- 比如以下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/2/28
# 文件名称:test_pytest_repeat.py
# 作用:pytest-repeat插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import random
import time
import pytest
def test_case01():
computer = random.randint(0, 4)
time.sleep(1)
print(computer)
assert computer < 3
- 输出为:
test_pytest_repeat.py
.0
.2
.3
F
================================================== FAILURES ==================================================
______________________________________________ test_case01[3-5] ______________________________________________
def test_case01():
computer = random.randint(0, 4)
time.sleep(1)
print(computer)
> assert computer < 3
E assert 3 < 3
test_pytest_repeat.py:18: AssertionError
========================================== short test summary info ===========================================
FAILED test_pytest_repeat.py::test_case01[3-5] - assert 3 < 3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================== 1 failed, 2 passed in 3.13s =========================================
3.2 用例标记执行重复多次
- 使用
@pytest.mark.repeat(count)
,将代码中某些测试用例标记为执行重复多次; - 比如:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/2/28
# 文件名称:test_pytest_repeat01.py
# 作用:pytest-repeat插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import pytest
@pytest.mark.repeat(8)
def test_case():
print("测试用例执行")
if __name__ == '__main__':
pytest.main(["-s", "test_pytest_repeat01.py"])
- 输出为:
test_pytest_repeat01.py::test_case[1-8] PASSED [ 12%]测试用例执行
test_pytest_repeat01.py::test_case[2-8] PASSED [ 25%]测试用例执行
test_pytest_repeat01.py::test_case[3-8] PASSED [ 37%]测试用例执行
test_pytest_repeat01.py::test_case[4-8] PASSED [ 50%]测试用例执行
test_pytest_repeat01.py::test_case[5-8] PASSED [ 62%]测试用例执行
test_pytest_repeat01.py::test_case[6-8] PASSED [ 75%]测试用例执行
test_pytest_repeat01.py::test_case[7-8] PASSED [ 87%]测试用例执行
test_pytest_repeat01.py::test_case[8-8] PASSED [100%]测试用例执行
============================== 8 passed in 0.04s ==============================
3.3 命令行参数--repeat-scope详解
- 命令行参数作用:可以覆盖默认的测试用例执行顺序,类似
fixture的scope
参数; - 说明:
作用范围 | 说明 |
---|---|
| 默认,每个用例重复执行,再执行下一个用例 |
| 以class为单位,重复执行class里面的用例,再执行下一个 |
| 以模块为单位,重复执行模块里面的用例,再执行下一个 |
| 重复整个测试会话,即所有测试用例的执行一次,然后再执行第二次 |
3.3.1 class示例
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/1
# 文件名称:test_pytest_repeat02.py
# 作用:pytest-repeat插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import pytest
class TestCase01():
def test_01(self):
print("假如我有一个亿,")
class TestCase02():
def test_02(self):
print("我一定带你去履行!")
- 命令行执行:
pytest -s --count=3 --repeat-scope=class test_pytest_repeat02.py
- 输出为:
test_pytest_repeat02.py 假如我有一个亿,
.假如我有一个亿,
.假如我有一个亿,
.我一定带你去履行!
.我一定带你去履行!
.我一定带你去履行!
.
=============== 6 passed in 0.16s ================
3.3.2 module示例
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/1
# 文件名称:test_pytest_repeat03.py
# 作用:pytest-repeat插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import pytest
def test_01():
print("假如我有一个亿,")
def test_02():
print("我一定带你去履行!")
class TestCase():
def test_03(self):
print("如果你有一个亿,可以先借我用用,我再带你去履行!")
- 执行命令:
pytest -s --count=3 --repeat-scope=module test_pytest_repeat03.py
- 输出为:
test_pytest_repeat03.py 假如我有一个亿,
.我一定带你去履行!
.如果你有一个亿,可以先借我用用,我再带你去履行!
.假如我有一个亿,
.我一定带你去履行!
.如果你有一个亿,可以先借我用用,我再带你去履行!
.假如我有一个亿,
.我一定带你去履行!
.如果你有一个亿,可以先借我用用,我再带你去履行!
.
================= 9 passed in 0.15s =================
相关文章
- pytest学习及资料
- Pytest(16)随机执行测试用例pytest-random-order[通俗易懂]
- Pytest(17)运行未提交的git(pytest-picked)
- Pytest(6)重复运行用例pytest-repeat「建议收藏」
- pytest运行_pytest执行多个py文件
- @pytest.mark.parametrize_pytest参数化可变参数
- Pytest和Allure测试框架-超详细版+实战
- pytest的assert_assert断言语句
- pytest-allure_pytest allure
- pytest运行_ios自动清理缓存
- 善用 pytest fixture factory 构建结构优秀的单元测试
- 测试之路 pytest接口自动化-request包中的headers覆盖
- pytest + yaml 框架 - 2.extract 提取结果与接口之间的参数关联
- pytest + yaml 框架 -4.用例参数化parameters功能实现
- pytest + yaml 框架 -14.钉钉机器人通知测试结果
- Pytest学习笔记6——自定义用例顺序
- pytest学习和使用1-pytest安装和版本查看
- 软件测试|Pytest的必会技巧(一)
- pytest学习和使用11-Pytest如何使用自定义标记mark?
- pytest学习和使用19-pytes断言失败后,怎样保持后续的断言继续执行?(pytest-assume)
- pytest学习和使用22-allure特性 丨总览中的Environment、Categories设置以及Flaky test使用
- pytest学习和使用25-当看到allure的动态生成标题和描述的特性时,直言简直太灵活了(allure.dynamic使用)
- pytest详解编程语言