接口测试采用python+pytest+requests+allure
allure-pytest 环境准备
windows环境相关:
python 3.6版本 pytest 3.6.3版本 allure-pytest 2.8.6 最新版
使用pip安装pytest和allure-pytest,加上—index-url地址,下载会快一些
pip install pytest==3.6.3 —index-url https://pypi.douban.com/simple pip install allure-pytest==2.8.6 —index-url https://pypi.douban.com/simple
安装完成之后,打开一个你之前写的pytest脚本,看能不正常使用,如果报错:AttributeError: module ‘allure’ has no attribute ‘severity_level’ 这个是之前 pytest-allure-adaptor 这个插件与 allure-pytest 不能共存,卸载掉 pytest-allure-adaptor
pip uninstall pytest-allure-adaptor
allure命令行工具
allure是一个命令行工具,需要去github上下载最新版https://github.com/allure-framework/allure2/releases
下载完成之后,解压到本地电脑
把bin目录添加到环境变量Path下
把bin目录添加到环境变量Path下
用例demo
conftest.py内容
import pytest
@pytest.fixture(scope="session")
def login():
print("用例先登录")
test_allure_demo.py内容
import allure
import pytest
@allure.step("步骤1:点xxx")
def step_1():
print("111")
@allure.step("步骤2:点xxx")
def step_2():
print("222")
@allure.feature("编辑页面")
class TestEditPage():
'''编辑页面'''
@allure.story("这是一个xxx的用例")
def test_1(self, login):
'''用例描述:先登录,再去执行xxx'''
step_1()
step_2()
print("xxx")
@allure.story("打开a页面")
def test_2(self, login):
'''用例描述:先登录,再去执行yyy'''
print("yyy")
复制
运行用例
cd到test_allure_demo.py所在的目录文件,命令行执行
pytest —alluredir ./report/allure_raw
备注:report为测试报告路径 allure_raw为生成的关于allure文件
D:\soft\code\xuexipytest>pytest --alluredir ./report/allure_raw
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-5.3.1, py-1.5.4, pluggy-0.13.1
rootdir: D:\soft\code\xuexipytest
plugins: allure-pytest-2.8.6, forked-0.2, html-1.19.0, metadata-1.7.0, repeat-0.7.0, xdist-1.23.2
collected 9 items
case\test_allure_demo.py .. [ 22%]
case\test_x.py ...... [ 88%]
case\test_y.py . [100%]
============================== 9 passed in 0.21s ==============================
复制
执行完成后,在当前目录下,report目录会生成一个allure_raw的原始文件,这个只是测试报告的原始文件,不能打开成html的报告
打开html的报告需要启动allure服务,启动命令如下
allure serve report/allure_raw
启动服务,它会自动给个端口,直接用默认浏览器打开了
D:\soft\code\xuexipytest>allure serve report/allure_raw
Generating report to temp directory...
Report successfully generated to C:\Users\dell\AppData\Local\Temp\6056757827461248074\allure-report
Starting web server...
2019-12-08 00:41:09.921:INFO::main: Logging initialized @2228ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.1.125:35346/>. Press <Ctrl+C> to exit
复制
查看报告
浏览器上打开的报告内容
点 EN 按钮可以查看中文报告
打开测试套件,可以查看报告的详情,显示的还是很详细的
上述文章参考:pytest文档29-allure-pytest(最新最全,保证能搞成功!) - 腾讯云开发者社区-腾讯云
补充实操过程的坑:
1、出现了
AttributeError: module ‘allure’ has no attribute ‘severity_level’ 报错,卸载掉 pytest-allure-adaptor 重新运行会报 另一个问题:
no module named "_pytest.resultlog"
原因:因为安装了pytest-rerunfailures(这个插件是失败重跑插件),然后导致_pytest.resultlog该模块被删除
结果:不可以pytest-rerunfailures与 pytest 6.1.0以上的版本一起使用。
解决办法:
①升级pytest-rerunfailures版本到9.1.1
②卸载pytest-rerunfailures使用pytest-reportlog代替
③降低pytest版本到6.1.0以下
解决方案参考文档:记:no module named "_pytest.resultlog" - QiKa - 博客园
个人采取的解决方法是升级pytest-rerunfailures版本,上述问题确实解决
然后就是命令行运行 pytest —alluredir ./report/allure_raw 报错
导入错误的问题,不知道问题出在哪里,未解决,转而运行 py.test运行命令
$ py.test test/ --alluredir ./result/
无果,cmd提示没有$ 指令
参考用 Pytest+Allure 生成漂亮的 HTML 图形化测试报告 - 码农教程
采用方法3:在执行pytest命令时,添加allure命令参数:—alluredir=Outputs/allure (相对于pytest命令所在目录的测试报告目录):
if __name__ == '__main__':
pytest.main(["-s", "-v", "--html=Outputs/test_report/pytest.html",
"--alluredir=Outputs/allure"]) # allure文件生成的目录
备注:Outputs/test_report/pytest.html 为测试报告生成路径
3、等待pytest执行完所有的测试用例,在Outputs/allure下会生成一些文件。
在cmd命令行当中,执行:allure serve 测试结果文件目录,就会生成漂亮的html报告。
后面即可看到自动打开的漂亮的测试报告啦!!!!
方法三参考:allure与pytest集成配置详解_云淡风轻-测试仔的博客-优快云博客_pytest集成allure