场景:有时候我们需要执行的全部用例或者部分用例想要重复执行多次执行的场景,我们随着这个需求往下看就能得到解决。
Pytest 给我们提供了了一个很好的插件 pytest-repeat,简单添加一些参数就可以实现,下面就让我们完整的学习这个插件相关的功能。
pytest-repeat 详解用法:
--count=num
@pytest.mark.repeat(num)
--count=num
示例:
# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
def test_repeat():
    assert True
def test_repeat_01():
    assert True
if __name__ == '__main__':
    pytest.main()
在命令窗口执行 pytest -v --count=2 test_repeat.py 命令,查看结果:

@pytest.mark.repeat(num)
代码示例:
# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
@pytest.mark.repeat(2)
def test_repeat():
    assert True
def test_repeat_01():
    assert True
if __name__ == '__main__':
    pytest.main()
在命令窗口执行 pytest -v test_repeat.py 命令,查看结果:

例如:运行完成一个模块之后再重复执行或者运行完成所有用例之后在重复执行这需求。
查看 repeat 源码,发现一个参数可以配置 --repeat-scope:
说明:--repeat-scope 参数只能与 --count=num 配合使用
def pytest_addoption(parser):
    parser.addoption(
        '--count',
        action='store',
        default=1,
        type=int,
        help='Number of times to repeat each test')
    parser.addoption(
        '--repeat-scope',
        action='store',
        default='function',
        type=str,
        choices=('function', 'class', 'module', 'session'),
        help='Scope for repeating tests')
--repeat-scope 的参数有 choices=('function', 'class', 'module', 'session') 默认为function
--repeat-scope=session示例代码:
# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
def test_repeat():
    assert True
def test_repeat_01():
    assert True
if __name__ == '__main__':
    pytest.main()
在命令窗口执行 pytest -v --count=2  --repeat-scope=session test_repeat.py 命令,查看结果:以会话为单位执行,在进行重复执行

--repeat-scope=class示例代码:
# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
class TestRepeat:
    def test_repeat(self):
        assert True
class TestRepeat01:
    def test_repeat01(self):
        assert True
if __name__ == '__main__':
    pytest.main()
在命令窗口执行 pytest -v --count=2  --repeat-scope=class test_repeat.py 命令,查看结果:以类为单位执行,重复执行用例

--repeat-scope=module示例代码上述一样,在命令窗口执行 pytest -v --count=2  --repeat-scope=module test_repeat.py 命令,查看结果:以模块为单位执行,重复执行用例

--count=num 和 使用装饰器方式 @pytest.mark.repeat(num)  方式--count=num 可以和 --repeat-scope=作用域配合使用,控制用例重复执行时机@pytest.mark.repeat(num)  方式使用--repeat-scope=作用域 控制重复执行时机无效--count=num执行时,装饰器的用例重复不生效,以--count=num优先以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!