移动自动化appium(2)- Allure报告使用详解
6 Allure报告
6.1 Allure介绍
Allure是一个独立的报告插件,生成美观易读的报告,比之前的html好看,目前支持的语言:Java、PHP、Ruby、Python、C#等
6.2 Allure安装
pip3 install allure-pytest
(注意:这里不要使用pytest-allure-adaptor)
有一个帮助文档可以参考:
https://docs.qameta.io/allure/#_about
生成Allure报告的命令:
pytest --alluredir report
执行完这条命令,case运行完毕后,会在当前目录下生成一个report文件夹,里面有一个json文件,就是生成的报告
在pytest.ini中,之前的报告是这样生成的:
addopts = -s --html=report/report.html --reruns 1
如果想用allure,那么改成:
addopts = -s --alluredir report --reruns 1
直接在终端执行pytest就可以了
6.3 json报告转为html报告
上面的命令执行后,生成的是json文件,json文件不那么好看,需要将json转成html,这里要先安装一个插件,步骤:
1、下载allure压缩包,地址:
https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.7.0/allure-2.7.0.zip
2、解压后,将bin目录配置到环境变量
3、进入report的上一级目录,执行命令:allure generate report/ -o report/html --clean
执行上面的操作步骤后,在report文件夹下就多了一个html文件夹,里面的index.html文件就是html报告。
生成html的时候遇到这样的报错:
Exception in thread "main" java.lang.UnsupportedClassVersionError: io/qameta/all
ure/CommandLine : Unsupported major.minor version 52.0
那么可能是你的jdk版本太低了,可以打开cmd,输入java -version,以及javac -version,看一下版本,如果低于1.8,换成1.8版本就好了。
jdk1.8版本下载地址:
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
6.4 allure+pytest实战操作
6.4.1 step用法
@allure.step(“”):
用来描述用例步骤的,简单一点的用法,比如新建一个项目,框架如图:
第一种用法:
在”F:\python\allureDemo\scripts\test_demo.py”文件中,写入下列代码:
import allure class TestLogin: @allure.step("测试登录成功的步骤") def test_login_success(self): print("login success") assert 1
在终端输入pytest运行测试用例,然后从cmd进入到F:\python\allureDemo这个路径,输入allure generate report/ -o report/html --clean,生成测试报告
观察报告结果:
打开生成的报告,点击左侧的“包”或者“功能”,可以看到这样的页面,右侧test_login_success是我们的测试用例,圈绿色的部分是step的内容。
第二种用法:
allure的帮助文档中,给出的step的示例,test_steps_with_placeholders()这个测试用例:
import allure @allure.step('Step with placeholders in the title, positional: "{0}", keyword: "{key}"') def step_with_title_placeholders(arg1, key=None): pass def test_steps_with_placeholders(): step_with_title_placeholders(1, key='something') step_with_title_placeholders(2) step_with_title_placeholders(3, 'anything')
运行结果:
第三种用法:
第三种用法是只写一个@allure.step,没有参数,这种情况会直接把方法名放到步骤显示的位置,例如:
@allure.step def test_login_fail1(self): print("login fail1") assert 0
运行结果:
还有比如:
@allure.step
def passing_step():
pass def test_with_step_in_fixture_from_conftest(fixture_with_conftest_step):
passing_step()
等等方式
6.4.2 attach()添加case描述和截图
allure.attach()不是一个装饰器,需要写到test用例里面,比如下面这样:
def test_login_fail(self): allure.attach("输入正确的用户名:xxxx") print("input username") allure.attach("", "输入错误的密码:xxx") print("input password") allure.attach("", "点击登录") print("click login button") assert 0
第二个attach方法有两个参数,第一个有一个,这样写虽然都不会报错,但是结果会不一样,如图:
第一个,只写了一个参数的,会像图第一个圈红的位置那样,必须点开这个才会看到操作步骤,第二个和第三个,直接就可以看到操作步骤,更直观。
attach还可以增加截图:
def test_login_success(self): allure.attach.file(r'D:\xx\xxx.jpg', '我是附件截图的名字', attachment_type=allure.attachment_type.JPG) print("login success") assert 1
结果:
点击截图的名字,就可以看到截图。
attach.file()第三个参数,还可以是allure.attachment_type.PNG、allure.attachment_type.HTML、allure.attachment_type.TEXT等。
6.4.3 Descriptions
description是显示在描述位置的内容,可以描述一下你这个测试用例想做什么。
第一种用法,长字符串:
@allure.description(""" Multiline test description. That comes from the allure.description decorator. Nothing special about it. """) def test_description_from_decorator(): assert 42 == int(6 * 7)
结果:
第二种用法,html格式:
可以使用html格式
import allure @allure.description_html("""
<h1>Test with some complicated html description</h1>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr align="center">
<td>William</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr align="center">
<td>Vasya</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
""")
def test_html_description():
assert True
结果:
这里如果我们只是想要用一两句话概括这个测试用例的话,直接用一个字符串就足够了。
第三种用法,文档注释:
def test_unicode_in_docstring_description(): """Unicode in description. Этот тест проверяет юникод. 你好伙计. """ assert 42 == int(6 * 7)
像这样的文档注释,会直接放到报告的描述中去,如图:
6.4.4 title用法
title就是显示的case标题
比如这样:
@allure.title("case1:登录成功的用例") def test_login_success(self): allure.attach.file(r'D:\一寸照\报名照片.jpg', '我是附件截图的名字', attachment_type=allure.attachment_type.JPG) print("login success") assert 1
结果如图:
会在圈红部分两个地方显示。
如果有参数的话,还可以这样用:
import allure
import pytest @allure.title("Parameterized test title: adding {param1} with {param2}")
@pytest.mark.parametrize('param1,param2,expected', [
(2, 2, 4),
(1, 2, 5)
]) def test_with_parameterized_title(param1, param2, expected):
assert param1 + param2 == expected
结果就是这样:
6.4.5 link超链接
直接把官网的例子拿过来:
import allure TEST_CASE_LINK = 'https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637' @allure.link('https://www.youtube.com/watch?v=4YYzUTYZRMU')
def test_with_link():
pass @allure.link('https://www.youtube.com/watch?v=Su5p2TqZxKU', name='Click me')
def test_with_named_link():
pass @allure.issue('', 'Pytest-flaky test retries shows like test steps')
def test_with_issue_link():
pass @allure.testcase(TEST_CASE_LINK, 'Test case title')
def test_with_testcase_link():
pass
运行后,会出来一个链接,可以点击。
6.4.6 用例级别
下面这段代码示例里有两个点,一个是feature方法,一个是severity
import allure @allure.feature('这里是一级标签')
class TestLogin:
@allure.title("case1:登录成功的用例")
@allure.description("这里是对test_login_success用例的一些详细说明")
@allure.story("这里是第一个二级标签")
def test_login_success(self):
allure.attach.file(r'D:\一寸照\报名照片.jpg', '我是附件截图的名字',
attachment_type=allure.attachment_type.JPG)
print("login success")
assert 1 @allure.title("case2:登录失败的用例")
@allure.description("这里是对test_login_fail用例的一些详细说明")
@allure.severity(allure.severity_level.CRITICAL)
@allure.story("这里是第一个二级标签")
def test_login_fail(self):
allure.attach("步骤1", "输入正确的用户名")
print("input username")
allure.attach("步骤2", "输入错误的密码")
print("input password")
allure.attach("步骤3", "点击登录")
print("click login button")
assert 0 @allure.title("case3:登录失败的用例")
@allure.description("这里是对test_login_fail1用例的一些详细说明")
@allure.severity(allure.severity_level.BLOCKER)
@allure.story("这里是第一个二级标签")
def test_login_fail1(self):
print("login fail1")
assert 0
点击allure报告中的“图表”,可以看到有优先级:
这个优先级,如果不写,默认就是normal。用法就像标黄色底色那样,级别总共有五个:
- BLOCKER = 'blocker' 中断缺陷(客服端程序无响应,无法执行下一步骤)
- CRITICAL = 'critical' 临界缺陷(功能点缺失)
- NORMAL = 'normal' 普通缺陷(数据计算错误)
- MINOR = 'minor' 次要缺陷(界面错误与ui需求不符)
- TRIVIAL = 'trivial' 轻微缺陷(必须项无提示,或者提示不规范)
用例分级的方法:
@allure.feature('这里是一级标签')
@allure.story("这里是第一个二级标签")
对应的结果为:
6.4.7 添加截图
3.7.6讲到过截图的方法get_screenshot_as_file(路径),6.4.2讲了attach上传图片的方法,两个方法结合起来就可以在测试过程中截图并且传到报告上
def test_login(self):
# 输入手机号
self.login_page.input_tel("")
# 输入密码
self.login_page.input_pwd("sy123")
time.sleep(3)
# 点击登录
self.login_page.click_login()
# 截图的方法
self.login_page.screenshot("login_success")
# 上传到报告
allure.attach.file(r'.\screen\login_success.png', 'login_success',\
attachment_type=allure.attachment_type.PNG) def screenshot(self, file_name):
self.driver.get_screenshot_as_file("./screen/" + file_name + ".png")
移动自动化appium(2)- Allure报告使用详解的更多相关文章
- 自动化集成:Pipeline流水语法详解
前言:该系列文章,围绕持续集成:Jenkins+Docker+K8S相关组件,实现自动化管理源码编译.打包.镜像构建.部署等操作:本篇文章主要描述Pipeline流水线用法. 一.Webhook原理 ...
- 自动化集成:Kubernetes容器引擎详解
前言:该系列文章,围绕持续集成:Jenkins+Docker+K8S相关组件,实现自动化管理源码编译.打包.镜像构建.部署等操作:本篇文章主要描述Kubernetes引擎用法. 一.基础简介 Kube ...
- python接口自动化(七)--状态码详解对照表(详解)
简介 我们为啥要了解状态码,从它的作用,就不言而喻了.如果不了解,我们就会像个无头苍蝇,横冲直撞.遇到问题也不知道从何处入手,就是想找别人帮忙,也不知道是找前端还是后端的工程师. 状态码的作用是:we ...
- SpringBoot自动化配置之四:@Conditional注解详解
前言 之前在分析spring boot 源码时导出可见@ConditionalOnBean 之类的注解,那么它到底是如何使用的以及其工作流程如何,我们这里就围绕以下几点来分析: @Conditiona ...
- Python自动化必备发送邮件报告脚本详解
#!/usr/bin/python3# -*- coding:UTF-8 -*-import smtplib#smtplib库主要用来连接第三方smtp库,用来发邮件from email.mime.t ...
- UI自动化学习笔记- UnitTest单元测试框架详解
一.UnitTest基本使用 1. UnitTest框架 1.1 什么是框架 说明: 框架英文单词frame 为解决一类事情的功能集合 1.2什么是UnitTest框架 概念:UnitTest是pyt ...
- autoIT 自动化上传/下载文件图文详解【python selenium】
情景: 在用selenium进行web页面自动化时,时不时会遇到上传附件的情况,常见的情况就是一个上传按钮,点击后弹出windows窗口,选择文件后上传,如下图1所示 图1 这种情况超出了seleni ...
- postman接口自动化,环境变量的用法详解(附postman常用的方法)
在实现接口自动测试的时候,会经常遇到接口参数依赖的问题,例如调取登录接口的时候,需要先获取登录的key值,而每次请求返回的key值又是不一样的,那么这种情况下,要实现接口的自动化,就要用到postma ...
- Python3自动化运维之Fabric模版详解
一.概要 Fabric是基于Python(2.7,3.4+以上版本)实现的SSH命令行工具,简化了SSH的应用程序部署及系统管理任务,它提供了系统基础的操作组件,可以实现本地或远程shell命令,包括 ...
随机推荐
- Java多态实现的机制
Java提供了编译时多态和运行时多态两种多态机制.前者是通过方法重载实现的,后者是通过方法的覆盖实现的. 在方法覆盖中,子类可以覆盖父类的方法,因此同类的方法会在父类与子类中有着不同的表现形式. 在J ...
- VUE二 生命周期详解
vue官网对vue生命周期的介绍 Vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.销毁等一系列过程,我们称这是Vue的生命周期.通俗说就是Vue实 ...
- Druid未授权(弱口令)的一些利用方式
Druid简介 1.Druid是阿里巴巴数据库事业部出品,为监控而生的数据库连接池. 2.Druid提供的监控功能,监控SQL的执行时间.监控Web URI的请求.Session监控. Druid可能 ...
- Java探针技术-retransformclasses的介绍
retransformclasses void retransformclasses(class... classes) throws unmodifiableclassexception 重转换提供 ...
- 【原创】(求锤得锤的故事)Redis锁从面试连环炮聊到神仙打架。
这是why技术的第38篇原创文章 又到了一周一次的分享时间啦,老规矩,还是先荒腔走板的聊聊生活. 有上面的图是读大学的时候,一次自行车骑行途中队友抓拍的我的照片.拍照的地方,名字叫做牛背山,一个名字很 ...
- Angular介绍
Angulay介绍 1.介绍:是一个用于Html和TypeScript构建客户端应用平台与框架.Angular 本身就是用 TypeScript 写成的.基本构造块是 NgModule,它为组件提供了 ...
- 聊聊order by的工作机制
总结写在前面: 1. 介绍了orderBy的两种算法流程:全字段排序 和 rowid排序. 2. rowid排序 相比 全字段排序,参与排序字段较少,耗内存较少,多一步回表,如果内存够的情况下MySQ ...
- js获取按钮的文字
button按钮有两种情况: 1 <input type="button" id="button" value="button"> ...
- Java学习笔记(2)——有关类
Java类的高级特性: {Java管理文件机制:类包. 同一个包中的类互相访问时,可以不指定包名. 同一个包中的类不必存放在同一位置,如com.lang.class1和com.lang.class2可 ...
- Css盒模型属性详解(margin和padding)
Css盒模型属性详解(margin和padding) 大家好,我是逆战班的一名学员,今天我来给大家分享一下关于盒模型的知识! 关于盒模型的属性详解及用法 盒模型基本属性有两个:padding和marg ...