成都市建设部官方网站,wordpress免费有趣插件,保定网页制作,高邮企业网站建设文章目录一、allure的介绍二、allure的运行方式三、allure报告的生成方式一、在线报告、会直接打开默认浏览器展示当前报告方式二、静态资源文件报告#xff08;带index.html、css、js等文件#xff09;#xff0c;需要将报告布置到web服务器上。四、allure中装饰器1、实现给… 文章目录一、allure的介绍二、allure的运行方式三、allure报告的生成方式一、在线报告、会直接打开默认浏览器展示当前报告方式二、静态资源文件报告带index.html、css、js等文件需要将报告布置到web服务器上。四、allure中装饰器1、实现给测试报告添加用例标题a.直接使用allure.title为测试用例自定义标题。b.allure.title支持通过占位符的方式传递参数可以实现测试用例标题参数化,动态生成测试用例标题。c.allure.dynamic.title动态更新测试用例标题。2、allure报告中添加用例步骤方法一:使用装饰器定义一个测试步骤在测试用例中使用。方法二:使用with allure.step()添加测试步骤。3、allure报告中添加用例链接4、allure报告中添加用例分类总结5、allure报告中添加用例描述6、allure报告中添加用例优先级7、allure报告中添加用例支持tags标签8、allure报告中添加pytest.fixture五、失败用例重试功能六、allure报告中添加附件1、添加图片2、添加日志3、添加html4、添加视频七、定制allure报告1、修改页面的logo一、allure的介绍
Allure 是由Java 语言开发的一个轻量级灵活的测试报告工具。 Allure多平台的 Report框架。 Allure 支持多语言包括 python、JaveScript、PHP、Ruby 等。 可以为开发/测试/管理等人员提供详尽的的测试报告包括测试类别、测试步骤、日志、图片、视 频等。 可以为管理层提供高水准的统计报告。 可以集成到Jenkins生成在线的趋势汇总报告。
二、allure的运行方式
收集结果
pytest 测试模块/测试包/测试用例 --alluredir 指定存储测试结果的路径
pytest --alluredir./reports --clean-alluredir生成在线的测试报告: allure serve ./results
安装allure-pytest
三、allure报告的生成
方式一、在线报告、会直接打开默认浏览器展示当前报告
--clean-alluredir:清理已经生成的报告的历史记录 1、pytest 测试模块/测试包/测试用例 --alluredir 指定存储测试结果的路径 2、生成在线的测试报告: allure serve ./results 方式二、静态资源文件报告带index.html、css、js等文件需要将报告布置到web服务器上。
应用场景:如果希望随时打开报告可以生成一个静态资源文件报告将这个报告布署到web服务器上启动web服务即可随时随地打开报告。 解决方案:使用allure generate 生成带有 index.html 的结果报告。这种方式需要 两个步骤: 第一步:生成报告。
--clean如果报告路径重复清理上一次的报告
allure generate ./results --clean-o将测试报告生成指定的目录
allure generate ./results --clean -o ./reports第二步:打开报告。
allure open allure-report打开报告指定IP地址和端口号
-h 127.0.0.1 -p 8888
allure open ./reports -h 127.0.0.1 -p 8888四、allure中装饰器 allure.epic() epic描述 敏捷里面的概念定义史诗往下是feature allure.feature() 模块名称 功能点的描述往下是 story allure.story() 用户故事 用户故事往下是title allure.title(用例的标题)用例的标题 重命名 html 报告名称 allure.step() 操作步骤 测试用例的步骤 allure.testcase() 测试用例的链接地址对应功能测试用例系统里面的case allure.issue() 缺陷 对应缺陷管理系统里面的链接 allure.description() 用例描述 测试用例的描述 allure.severity() 用例等级 blocker, critical, normal, minor, trivial allure.link() 链接 定义一个链接在测试报告展现 allure.attachment() 附件 报告添加附件 1、实现给测试报告添加用例标题
通过使用装饰器allure.title可以为测试用例自定义一个可阅读性的标题。 allure.title的三种使用方式:
a.直接使用allure.title为测试用例自定义标题。
import allureallure.title(测试标题)
def test_with_title():assert True
b.allure.title支持通过占位符的方式传递参数可以实现测试用例标题参数化,动态生成测试用例标题。
allure.title(参数化用例标题:参数1:{p1},参数2:{p2})
pytest.mark.parametrize(p1,p2,p3,[[1,1,2],[2,4,6]])
def test_with_title1(p1,p2,p3):assert p1p2p3 c.allure.dynamic.title动态更新测试用例标题。
allure.title(原始标题)
def test_with_title2():assert Trueallure.dynamic.title(更改后的标题)2、allure报告中添加用例步骤
应用场景:编写自动化测试用例的时候经常会遇到需要编写流程性测试用例的场景,一般流程性的测试用例的测试步骤比较多我们在测试用例中添加详细的步骤会提高测试用例的可阅读性。
方法一:使用装饰器定义一个测试步骤在测试用例中使用。
import allure
import pytestallure.step
def simple_step1(step_param1,step_param2None):定义一个测试步骤print(f步骤1:打开页面参数1:{step_param1},参数2:{step_param2})allure.step
def simple_step2(step_param):定义一个测试步骤print(f步骤1:完成搜索{step_param}功能)pytest.mark.parametrize(param1,[pytest,allure],ids[search pytest,search pytest])
def test_parameterize_with_id(param1):simple_step2(param1)pytest.mark.parametrize(param1,[True, False])
pytest.mark.parametrize(param2, [value 1, value 2])
def test_parametrize_with_two_parameters(param1, param2):simple_step1(param1,param2)pytest.mark.parametrize(param2,[pytest, unittest])
pytest.mark.parametrize(param1,param3, [[1,2]])
def test_parameterize_with_uneven_value_sets(param1, param2, param3):simple_step1(param1,param3)simple_step2(param2)方法二:使用with allure.step()添加测试步骤。
allure.title(搜索用例:{searchkey})
pytest.mark.parametrize(searchkey,[java,allure])
def test_step_in_method(searchkey):with allure.step(测试步骤一:打开页面):print(操作a)print(操作b)with allure.step(f测试步骤二:搜索{searchkey}):print(f搜索操作{searchkey})with allure.step(测试步骤三:断言):assert True3、allure报告中添加用例链接
应用场景:将报告与bug管理系统或测试用例管理系统集成可以添加链接装饰器 allure. link、allure.issue和allure.testcase。
格式1: allure.link (url, name添加一个普通的link链接name起别名
import allureallure.link(http://www.baidu.com,name这是一个链接)
def test_with_link():pass格式2: allure.testcase( url,name)添加一个用例管理系统链接。
TEST_CASE_LINKhttp://www.baidu.comallure.testcase(TEST_CASE_LINK,用例管理系统)
def test_with_testcase():pass格式3: allure.issue(url,name)添加bug管理系统链接。
allure.issue(66666,bug管理系统)
def test_with_issue():pass
4、allure报告中添加用例分类
应用场景:可以为项目以及项目下的不同模块对用例进行分类管理。也可以运行某个类别下的用例。 报告展示:类别会展示在测试报告的Behaviors栏目下。 Allure提供了三个装饰器: allure.epic:敏捷里面的概念定义史诗往下是feature。
import allureallure.epic(需求1)
class TestEpic:def test_case1(self):print(用例1)def test_case2(self):print(用例2)def test_case3(self):print(用例3)allure.epic(需求1)
class TestEpic1:def test_case1(self):print(用例1)def test_case2(self):print(用例2)def test_case3(self):print(用例3)场景:希望在报告中看到测试功能子功能或场景。 子功能上加allure.story、allure.feature
allure.feature:功能点的描述理解成模块往下是story。 allure.story:故事story是 feature的子集。
import allureallure.epic(需求1)
allure.feature(功能模块1)
class TestEpic:allure.story(子功能1)allure.title(用例1)def test_case1(self):print(用例1)allure.story(子功能2)allure.title(用例2)def test_case2(self):print(用例2)allure.story(子功能2)allure.title(用例3)def test_case3(self):print(用例3)allure.story(子功能1)allure.title(用例4)def test_case4(self):print(用例4)allure.story(子功能3)allure.title(用例5)def test_case5(self):print(用例5)allure.epic(需求1)
allure.feature(功能模块2)
class TestEpic1:allure.story(子功能4)def test_case1(self):print(用例1)allure.story(子功能5)def test_case2(self):print(用例2)def test_case3(self):print(用例3)allure.epic(需求2)
allure.feature(功能模块1)
class TestEpic2:def test_case1(self):print(用例1)def test_case2(self):print(用例2)def test_case3(self):print(用例3)运行特定类别的用例
1、只运行epic名为”需求1“的测试用例
pytest demo1/test_allure_feature.py --alluredir./reports --clean-all
uredir --allure-epics需求12、只运行feature名为”功能模块2“的测试用例
pytest demo1/test_allure_feature.py --alluredir./reports --clean-all
uredir --allure-features功能模块23、只运行story名为”子功能1“的测试用例
pytest demo1/test_allure_feature.py --alluredir./reports --clean-all
uredir --allure-stories子功能1
4、只运行story名为”子功能2“和”子功能2“的测试用例
pytest demo1/test_allure_feature.py --alluredir./reports --clean-all
uredir --allure-stories子功能2,子功能3 -vs5、运行story名为”子功能2“和 feature名”功能模块1“的测试用例
pytest demo1/test_allure_feature.py --alluredir./reports --clean-all
uredir --allure-stories子功能2 --allure-features功能模块1 总结
epic:相当于定义一个项目。 feature:相当于一个功能模块相当于testsuite可以管理很多个子分支story story:相当于对应这个功能或者模块下的不同场景分支功能。 epic 与feature、feature 与story类似于父子关系。
5、allure报告中添加用例描述
应用场景:Allure支持往测试报告中对测试用例添加非常详细的描述语用来描述测试用例详情。 Allure添加描述的四种方式: 方式一:使用装饰器 allure.description()传递一个字符串参数来描述测试用例。
import allureallure.description(
多行描述信息br/
通过传递字符串参数添加一段描述语句/br
使用装饰器allure.description
)
def test_description_string():assert True方式二:使用装饰器 allure.description_ html传递一段HTML文本来描述测试用例。
allure.description_html(
img classindex-logo-src src//www.baidu.com/img/flexible/logo/pc/result.png alt到百度首页 title到百度首页
)
def test_description_html():assert True方式三:直接在测试用例方法中通过编写文档注释的方法来添加描述。会按照给定的格式展示不需要添加br/
def test_description_desc():直接写在测试用例中通过编写文档注释的方法来添加描述会按照给定的格式展示不需要添加br/:return:assert True6、allure报告中添加用例优先级
应用场景:用例执行时希望按照严重级别执行测试用例。
解决:可以为每个用例添加一个等级的装饰器用法: allure.severity。
Allure对严重级别的定义分为5个级别: Blocker级别:中断缺陷客户端程序无响应无法执行下一步操作)。 Critical级别:临界缺陷功能点缺失)。 Normal级别:普通缺陷(数值计算错误)。 Minor级别:次要缺陷(界面错误与UI需求不符)。 Trivial级别:轻微缺陷必输项无提示或者提示不规范)。 使用装饰器添加用例方法/类的级别。 类上添加的级别对类中没有添加级别的方法生效。 未加级别标签的用例在运行时是不会被收集上来的 但是在类中没有加标签会随着类上加的标签来收集的 import allure未加级别标签的用例在运行时是不会被收集上来的
def test_with_no_serverity_label():passallure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_serverity():passallure.severity(allure.severity_level.NORMAL)
def test_with_trivial_serverity1():pass对某一级别的用例运行时添加命令行参数–allure-severities:
pytest test_allure_severity.py --alluredir./reports --clean-al
luredir --allure-severitiestrivial
7、allure报告中添加用例支持tags标签
Allure报告支持的一些常见Pytest特性包括xfail、 skipif、fixture等。测试结果会展示特定的标识在用例详情页面。
8、allure报告中添加pytest.fixture
应用场景:fixture和finalizer是分别在测试开始之前和测试结束之后由Pytest调用的实用程序函数。Allure跟踪每个fixture的调用并详细显示调用了哪些方法以及哪些参数从而保持了调用的正确顺序。
import allure
import pytestpytest.fixture()
def func():print(前置操作)yieldprint(后置操作)class TestDemo:def test_a(self,func):assert Truedef test_b(self):pass 五、失败用例重试功能
Allure可以收集用例运行期间重试的用例的结果以及这段时间重试的历史记录。
安装 pip install pytest-rerunfailures
import pytest
import allurepytest.mark.flaky(reruns2,reruns_delay2)
class TestDemo:def test_a(self):assert Truedef test_b(self):assert False六、allure报告中添加附件
1、添加图片
应用场景:在做UI自动化测试时可以将页面截图或者出错的页面进行截图将截图添加到测试报告中展示辅助定位问题。
解决方案:使用allure.attach或者allure.attach.file(添加图片。
语法:allure.attach.file( source,name, attachment_type,extension) 参数解释 source:文件路径相当于传一个文件。 name:附件名字。 attachment_type:附件类型是 allure.attachment_type其中的一种(支持PNG、JPG、BMP、GIF等)。 extension:附件的扩展名。
案例1
import pytest
import allureclass TestWithAttach:def test_pic(self):allure.attach.file(source./1.jpeg,name图片,attachment_typeallure.attachment_type.PNG,extensionjpeg)案例2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time : 2023/2/20 18:46
# Author : 杜兰特
# File : test_allure_pic.pyimport pytest
import allureclass TestWithAttach:def test_pic(self):allure.attach.file(source./1.jpeg,name图片,attachment_typeallure.attachment_type.PNG,extensionjpeg)def test_pic1(self):with open(./1.jpeg,moderb) as f:filef.read()print(file)allure.attach(file,name页面截图,attachment_typeallure.attachment_type.PNG)2、添加日志
应用场景:报告中添加详细的日志信息有助于分析定位问题。 解决方案:使用python自带的logging模块生成日志日志会自动添加到测试报告中。
import allurefrom utils.log_util import loggerallure.epic(需求1)
allure.feature(功能模块一)
class TestEpic:allure.story(子功能一)allure.title(用例1)def test_case1(self):logger.info(这是 TestEpic 第一条用例)print(用例1)allure.story(子功能二)allure.title(用例2)def test_case2(self):logger.debug(这是 TestEpic 第二条用例)print(用例2)allure.story(子功能二)allure.title(用例3)def test_case3(self):logger.warning(这是 TestEpic 第三条用例)print(用例3)allure.story(子功能一)allure.title(用例4)def test_case4(self):logger.error(这是 TestEpic 第四条用例)print(用例4)allure.story(子功能三)allure.title(用例5)def test_case5(self):print(用例5)allure.epic(需求1)
allure.feature(功能模块二)
class TestEpic1:allure.story(子功能四)def test_case1(self):print(用例1)allure.story(子功能五)def test_case2(self):print(用例2)def test_case3(self):print(用例3)allure.epic(需求2)
class TestEpic2:def test_case1(self):print(用例1)def test_case2(self):print(用例2)def test_case3(self):print(用例3)3、添加html
应用场景:可以定制测试报告页面效果可以将HTML类型的附件显示在报告页面上。 解决方案:使用allure.attach(添加html代码。
语法: allure.attach (body ,name,attachment_type,extension)参数解释: body:要写入附件的内容(HTML代码块)。 name:附件名字。 attachment_type:附件类型是allure.attachment_type其中的一种。 extension:附件的扩展名。
import allure
from utils.log_util import loggerclass TestAllureHTML:def test_html(self):logger.info(测试日志)allure.attach(bodyimg classindex-logo-src src//www.baidu.com/img/flexible/logo/pc/result.png alt到百度首页 title到百度首页,name添加html,attachment_typeallure.attachment_type.HTML,extensionhtml)4、添加视频
应用场景:在做UI自动化测试时可以将页面截图或者出错的页面进行截图将截图添加到测试报告中展示辅助定位问题。 解决方案:使用allure.attach. file()添加视频。
语法: allure.attach (body ,name,attachment_type,extension)参数解释: body:要写入附件的内容(HTML代码块)。 name:附件名字。 attachment_type:附件类型是allure.attachment_type其中的一种。 extension:附件的扩展名。
import allureclass TestWithAttach:def test_video(self):allure.attach.file(xxx.mp4,name视频资源,attachment_typeallure.attachment_type.MP4,extensionmp4)七、定制allure报告
应用场景:针对不同的项目可能需要对测试报告展示的效果进行定制比如修改页面的logo、修改项目的标题或者添加一些定制的功能等等。
1、修改页面的logo
1.修改allure.yml文件添加logo插件custom-logo-plugin(在allure安装路径下可以通过where allure或者which allure查看allure安装路径)。 2.编辑styles.css文件配置logo图片。