ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytest测试框架系列 - Pytest pytest-repeat 重复执行用例插件详解

2021-07-11 19:51:59  阅读:208  来源: 互联网

标签:__ 插件 repeat -- 用例 pytest test


## 前言 场景:有时候我们需要执行的全部用例或者部分用例想要重复执行多次执行的场景,我们随着这个需求往下看就能得到解决。 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` 命令,查看结果: ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210710215009400.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ### 使用装饰器方式 `@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` 命令,查看结果: ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=202107102217377.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ## **问题:如果想要设置重复运行用例的时刻,如何设置?** 例如:运行完成一个模块之后再重复执行或者运行完成所有用例之后在重复执行这需求。 查看 `repeat` 源码,发现一个参数可以配置 `--repeat-scope`: 说明:`--repeat-scope` 参数只能与 `--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-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` - function:每个用例重复运行,全部运行结束后在执行下一个用例 - class:以类为用例集合单位,重复执行类里的用例,在执行下一个用例 - module:以模块为单位,重复执行模块里面的用例,在执行下一个 - session:重复整个测试会话,执行完成测试用例后再执行第二遍 ### **让我们看下 `--repeat-scope=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-scope=session test_repeat.py` 命令,查看结果:以会话为单位执行,在进行重复执行 ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210710222303792.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ### **再看一个 `--repeat-scope=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-scope=class test_repeat.py` 命令,查看结果:以类为单位执行,重复执行用例 ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=2021071022272863.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ### **再看一个 `--repeat-scope=module`** 示例代码上述一样,在命令窗口执行 `pytest -v --count=2 --repeat-scope=module test_repeat.py` 命令,查看结果:以模块为单位执行,重复执行用例 ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210710223051664.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ## 总结 - 重复执行用例通过添加参数 `--count=num` 和 使用装饰器方式 `@pytest.mark.repeat(num)` 方式 - 添加参数 `--count=num` 可以和 `--repeat-scope=作用域`配合使用,控制用例重复执行时机 - 装饰器方式 `@pytest.mark.repeat(num)` 方式使用`--repeat-scope=作用域` 控制重复执行时机无效 - 当时使用添加参数 `--count=num`执行时,装饰器的用例重复不生效,以`--count=num`优先 以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处! >**如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章** 个人微信号:搜索 【测试之路笔记】

标签:__,插件,repeat,--,用例,pytest,test
来源: https://blog.51cto.com/king15800/3036572

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有