## 前言
场景:有时候我们需要执行的全部用例或者部分用例想要重复执行多次执行的场景,我们随着这个需求往下看就能得到
解决。
Pytest 给我们提供了了
一个很好的
插件 `pytest-repeat`,简单
添加一些参数就可以实现,下面就让我们完整的学习这个
插件相关的
功能。
## `pytest-repeat` 详解
用法:
-
添加参数 `--count=num`
- 在需要重复执行的用例上使用装饰器 `@pytest.mark.repeat(num)`
###
添加参数方式 `--count=num`
示例:
```python
# !/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)`
代码示例:
```python
# !/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-s
cope`:
说明:`--repeat-s
cope` 参数只能与 `--count=num` 配合使用
```python
def pytest_addoption(parser):
parser.addoption(
'--count',
action='store',
default=1,
type=int,
help='Number of times to repeat each test')
parser.addoption(
'--repeat-s
cope',
action='store',
default='function',
type=str,
choices=('function', 'class', 'module', 'session'),
help='S
cope for repeating tests')
```
--repeat-s
cope的参数有 `choices=('function', 'class', 'module', 'session')`
默认为`function`
- function:每个用例重复运行,全部运行结束后在执行下
一个用例
- class:以类为用例集合单位,重复执行类里的用例,在执行下
一个用例
- module:以模块为单位,重复执行模块里面的用例,在执行下
一个
- session:重复整个测试会话,执行完成测试用例后再执行第二遍
### **让我们看下 `--repeat-s
cope=session`**
示例
代码:
```python
# !/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-s
cope=session test_repeat.py` 命令,查看结果:以会话为单位执行,在进行重复执行

### **再看
一个 `--repeat-s
cope=class`**
示例
代码:
```python
# !/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-s
cope=class test_repeat.py` 命令,查看结果:以类为单位执行,重复执行用例

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

## 总结
- 重复执行用例通过
添加参数 `--count=num` 和 使用装饰器方式 `@pytest.mark.repeat(num)` 方式
-
添加参数 `--count=num` 可以和 `--repeat-s
cope=作用域`配合使用,控制用例重复执行时机
- 装饰器方式 `@pytest.mark.repeat(num)` 方式使用`--repeat-s
cope=作用域` 控制重复执行时机无效
- 当时使用
添加参数 `--count=num`执行时,装饰器的用例重复不生效,以`--count=num`优先
以上为
内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!
>**如果觉得
文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术
文章**
个人微信号:
搜索 【测试之路笔记】
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。