准备测试框架:
python3 + pytest + allure
Allure作用:灵活轻便兼容性强大的报告输出插件,由Java语言开发,可以集成到 Jenkin,支持多语言。
所以需要下载开源码,并在python安装库里适配allure-pytest库。
安装Allure原码:
- allure包下载:点击下载底部的zip包
- 解压 >>进入bin目录>> 运行allure.bat,
- 把bin目录加入PATH环境变量(电脑右键属性>>高级系统变量>>环境变量>>path新建一条路径)
从第一步开始做起。
编写第一个测试文件,文件以test_*.py格式或者*_test.py格式命名。
在命令行输入这行命令检查我们的第一个测试文件。命令:pytest --collect-only
结果:
OK,顺利发现一个测试类,和类下的三个测试方法。
编写好简单的测试用例后,我们就可以查看下测试结果,有以下几种方法:
pytest test_changgeMe.py
上方命令指,用pytest执行模块下所有用例。
还有这个选择性执行的命令,一般用来调试:
# 执行test_changgeMe.py>里的TestAll类
pytest test_changgeMe.py::TestAll
# 执行test_changgeMe.py>里的TestAll类的test_duansheli2方法
pytest test_changgeMe.py::TestAll::test_duansheli2
在目录下定位某个含“new”方法的用例:
pytest -k "new" --collect-only
查询的结果为:
该文件内容:
# -*- coding:utf-8 -*-
import pytest
def test_new():
add = 5 + 3
assert add == 8
pytest.main(['-v','test_pytest.py'])
可看到,确实有含“new”。
通过显示的路径,运行它!
执行通过!
使用这个命令生成测试报告:
pytest test_changgeMe.py -s -q --alluredir=./Result/2022.8
# 格式:
# pytest [测试文件] -s -q --alluredir=./result # = 后接存储测试结果的路径,需提前建好
生成的报告文件:
这样就可以打开了。
查看报告:
绿色代表“通过”。
以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!!