简介
宏哥之前在做接口自动化的时候,用的测试报告是HTMLTestRunner,虽说自定义模板后能满足基本诉求,但是仍显得不够档次,高端,大气,遂想用其他优秀的report框架替换之。一次偶然的机会,在一个QQ群里看到Allure的测试报告,真的是一见钟情,特别的喜欢。但是由于时间的原因就没有自己实践一下,乘着国庆假期,自己特抽时间做了一番探索。
Allure介绍
Allure是一种灵活的轻量级多语言测试报告工具,它不仅可以以简洁的Web报告形式非常简洁地显示已测试的内容,而且还允许参与开发过程的每个人从日常执行中提取最大程度的有用信息。
从开发/质量保证的角度来看,Allure报告可以缩短常见缺陷的生命周期:可以将测试失败划分为bug和残破的测试,还可以配置日志,步骤,固定装置,附件,时间,历史记录以及与TMS的集成以及Bug跟踪系统,因此负责任的开发人员和测试人员将掌握所有信息。
从管理者的角度来看,Allure提供了一个清晰的“全局”,涵盖了所涵盖的功能,缺陷聚集的位置,执行时间表的外观以及许多其他方便的事情。魅力的模块化和可扩展性确保您始终可以微调某些东西,以使魅力更适合您。
一睹Allure风采
在展开Allure详述前,先上一份测试报告,报告主要包含总览、类别、测试套件、图表、时间刻度、功能、包等7大部分,支持自定义诸多信息,包括附件添加、缺陷链接、案例链接、测试步骤、Epic、Feature、Story、Title、案例级别等,相当强大。
想要详细了解Allure的小伙伴和童鞋们,可以访问这个网址:https://demo.qameta.io/allure
总览

类别

测试套

图表

时间刻度

功能

1. pytest的安装:(这里着重介绍Windows)
pytest
是python
的一个第三方单元测试框架,在这里用于生成原始的执行结果。
一定别选最新的,4.0.2亲测可用,否则会跳到坑二;还有项目名千万别以pytest开头
1.1. windows下:
pip install pytest
出现如下图所示,pytest安装成功
1.2. linux下:
pip install pytest
2. 安装pytest-allure-adaptor插件
据了解,安装pytest-allure-adaptor
。这个第三方库已经过时了,无法和现有的pytest
搭配使用。宏哥这个先安装后期遇到问题再去处理。
最新的安装需要下面这个:
allure-pytest是python的一个第三方库。用于连接pytest和allure,使它们可以配合在一起使用。
allure-pytest基于pytest的原始执行结果生成适用于allure的json格式结果。该json格式结果可以用于后续适用allure生成html结果。
2.1. windows下:
pip install pytest-allure-adaptor
出现如下图所示,pytest-allure-adaptor安装成功
3. allure的安装:
3.1. windows下:
前情提示: allure
是基于Java
的一个程序,需要Java1.8+
的环境,没有安装需要去安装一下。
或者到这个网址:https://github.com/allure-framework/allure2/releases/ 、https://bintray.com/qameta/generic/allure2下载你需要的版本的安装包
下载之后,将压缩包解压到一个磁盘中,我这里用的是D盘

3.2. 配置allure
的环境变量

将此路径:D:softwareallure-2.13.0in,用老办法配置到path中

点击确定,保存。这样就可以通过CMD
使用allure
命令
3.3. 编写测试文件
pycharm
新建一个test_demo.py
文件,代码如下:
# coding=utf-8 # 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行 # 2.注释:包括记录创建时间,创建人,项目名称。 ''' Created on 2019-9-29 @author: 北京-宏哥 QQ交流群:707699217 Project:手把手教你搭建Pytest+Allure2.X环境详细教程,生成让你一见钟情的测试报告(非常详细,非常实用) ''' # 3.导入模块 import allure # @allure.MASTER_HELPER.feature("测试Dome") @allure.feature("测试Demo") class TestDome(object): #@@allure.MASTER_HELPER.step("定义被测函数") @allure.step("定义被测函数") def func(self, x): return x+1 #@allure.MASTER_HELPER.story("被测场景") @allure.story("被测场景") #@allure.MASTER_HELPER.severity("blocker") @allure.severity("blocker") #@allure.MASTER_HELPER.step("断言结果") @allure.step("断言结果") def test_func(self): # with allure.MASTER_HELPER.step("断言结果"): #allure.MASTER_HELPER.attach("预期结果", "{}".format(self.func(3))) allure.attach("预期结果", "{}".format(self.func(3))) #allure.MASTER_HELPER.attach("实际结果", "{}".format(5)) allure.attach("实际结果", "{}".format(5)) assert self.func(3) == 5
3.4. 生成测试报告
在pycharm
中打开terminal

输入命令pytest -s --alluredir=report
运行后,无上述错误,同时会生成一个report
文件。其中会有一个json
格式的报告:

allure
将它转成HTML
格式的报告。通过cmd
命令cd
到report
的根目录下,执行allure generate --clean report

回到根目录下,会生成一个allure-report
的文件夹,在pycharm
中打开文件夹,点击index.html
运行

ok,到此为止。可以看到我们的精美的测试报告了

注⚠️:直接用chrome浏览器打开报告,报告可能会是空白页面。
解决办法:
1、在pycharm中右击index.html选择打开方式Open in Browser就可以了。
2、使用Chrome直接打开index.html。
小结
1.安装pytest-allure-adaptor后,运行报错:AttributeError: module 'pytest' has no attribute 'allure'
原因:因为pytest-allure-adaptor库基本被python3放弃了,运行很不友好,反正我运行就是报错
解决方法:
先卸载:pip uninstall pytest-allure-adaptor
再安装:pip install allure-pytest
然后再去对应case的文件夹下面cmd里面运行: pytest -s -q --alluredir report (可以改为你想设的路径,如果是report默认当前目录下),就会生成report文件夹了
2.输入命令pytest -s --alluredir=report
,会遇到以下这个错误:

进入allure
下面的utils
文件,修改以下代码:
# utils文件,可以通过from allure import utlis进入
for suitable_name in suitable_names:
# markers.append(item.get_marker(suitable_name))
markers.append(item.get_closest_marker(suitable_name))

您的肯定就是我进步的动力。如果你感觉还不错,就请鼓励一下吧!记得点波 推荐 哦!!!(点击右边的小球即可!(^__^) 嘻嘻……)