pytest-repeat
- 场景
- 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来
- 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,或者针对某个模块的用例重复执行多次
- 安装
- pip install pytest-repeat
- pip3 install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
- 运行要求
- Python 2.7、3.4+或PyPy
- py.test 2.8或更高版本* 使用方式
- 使用方式
- count=2
- count 2
- pytest --html=report.html --self-contained-html -s --reruns=5 --count=2 test_request.py
重复测试直到失败(重要!!!)
如果需要验证偶现问题,可以一次又一次地运行相同的测试直到失败,这个插件将很有用
可以将pytest的 -x 选项与pytest-repeat结合使用,以强制测试运行程序在第一次失败时停止
pytest --count=1000 -x test_file.py
from time import sleep
import pytest
def test_example():
import random
flag = random.choice([True, False])
print(flag)
sleep(1)
assert flag
if __name__ == "__main__":
pytest.main(['-v', '-s','--count=10', '-x','test_2.py'])
pytest -v -s --count=10 -x test_2.py
如果要在代码中将某些测试用例标记为执行重复多次,可以使用 @pytest.mark.repeat(count)
pytest-html
- 场景
- 生成漂亮清晰的HTML报告
- 安装
- pip install pytest-html
- pip3 install pytest-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
- 运行要求
- Python 3.6+
快速入门
pytest --html=report.html #会在当前目录下创建一个report.html的测试报告
合并CSS
上面命令生成的报告,css是独立的,分享报告的时候样式会丢失,为了更好的分享发邮件展示报告,可以把css样式合并到html里
pytest --html=report.html --self-contained-html
注意事项
- 在将文件或链接添加到独立报告时,插件会发出warnings;
- 在html测试报告中可能无法按预期显示文件或链接
from time import sleep
import pytest
def test_example():
import random
flag = random.choice([True])
print(flag)
sleep(1)
assert flag
if __name__ == "__main__":
pytest.main(['-v', '-s','--count=5', '-x','--html=report.html','--self-contained-html','test_2.py']) #--html=report.html --self-contained-html
pytest -v -s --count=10 -x --html=report.html --self-contained-html test_2.py