本文为霍格沃兹测验开发学社学员笔记分享
原文链接:ceshiren.com/t/topic/246…
Allure2测验陈述
1、运用 Allure2 运转办法-Python
1)–alluredir 参数生成测验陈述。
#在测验履行期间收集成果
pytest [测验用例/模块/包] --alluredir=./result/ (—alluredir这个选项 用于指定存储测验成果的途径)
#生成在线的测验陈述
allure serve ./result
2、运用 Allure2 运转办法-Java
1)运用 allure:report 参数生成测验陈述
# 在测验履行期间收集成果
# mvn指令行运用 maven插件装置
mvn clean test allure:report
# 生成在线的测验陈述
# mvn 直接找target/allure-results目录
mvn allure:serve
2)运转mvn指令对应没有在target下面生成allure-results目录,处理办法
①在src/test/resources途径下装备allure装备文件allure.properties,指名allure陈述生成途径。
allure.results.directory=target/allure-resultsa
3)运转mvn指令一向卡在下载中,处理办法
①在项目下创立.allure文件夹。
②下载allure解压到.allure文件夹下。
3、生成测验陈述
1)生成测验陈述需求运用指令行东西 allure
2)指令格局:allure [option] [command] [command options]
# 过程一:在测验履行期间收集成果
# —alluredir这个选项 用于指定存储测验成果的途径
pytest [测验文件] -s –q --alluredir=./result/
# 假如要铲除已经生成的陈述的历史记载,能够增加参数--clean-alluredir
pytest [测验文件] -s –q --alluredir=./result/ --clean-alluredir
# 过程二:检查测验陈述,留意这儿的serve书写
allure serve ./result/
3)Allure 陈述生成的两种办法
①办法一:在线陈述,会直接翻开默许浏览器展现当时陈述。
# 办法一:测验完结后检查实际陈述,在线检查陈述,会直接翻开默许浏览器展现当时陈述。
allure serve ./result/
②办法二:静态资源文件陈述(带 index.html、css、js 等文件),需求将陈述布署到 web 服务器上。
a.运用场景:假如期望随时翻开陈述,能够生成一个静态资源文件陈述,将这个陈述布署到 web 服务器上,发动 web 服务,即可随时随地翻开陈述。
b.处理方案:运用allure generate 生成带有 index.html 的成果陈述。这种办法需求两个过程:第一步:生成陈述。第二步:翻开陈述。
# 生成陈述
allure generate ./result
# 翻开陈述
allure open ./report/
4)常用参数
①allure generate 能够指定输出途径,也能够整理上次的陈述记载。
②-o / –output 输出陈述的途径。
③-c / –clean 假如陈述途径重复。
④allure open 翻开陈述。
⑤-h / –host 主机 IP 地址,此主机将用于发动报表的 web 服务器。
⑥-p / –port 主机端口,此端口将用于发动报表的 web 服务器,默许值:0。
# 生成陈述,指定输出途径,整理陈述。
allure generate ./result -o ./report --clean
# 翻开陈述,指定IP地址和端口。
allure open -h 127.0.0.1 -p 8883 ./report/
4、Allure2 陈述中增加用例标题
(1)经过运用装修器 @allure.title 能够为测验用例自界说一个可阅览性的标题。allure.title 的三种运用办法:
1)办法一:直接运用装修器 @allure.title 为测验用例自界说标题。
import allure
import pytest
@allure.title("自界说测验用例标题")
def test_with_title():
assert True
2)办法二:@allure.title 支撑经过占位符的办法传递参数,能够完结测验用例标题参数化,动态生成测验用例标题。
import allure
import pytest
@allure.title("参数化用例标题:参数一:{param1} ,参数二: {param2}")
@pytest.mark.parametrize("param1, param2, expected", [
(1, 1, 2),
(0.1, 0.3, 0.4)
])
def test_with_parametrize_title(param1, param2, expected):
assert param1 + param2 == expected
3)办法三:allure.dynamic.title 动态更新测验用例标题。
@allure.title("原始标题")
def test_with_dynamic_title():
assert True
allure.dynamic.title("更改后的新标题")
5、allure2陈述中增加用例过程
1)办法一:运用装修器界说一个测验过程,在测验用例中运用。
# 办法一:运用装修器界说一个测验过程,在测验用例中运用
import allure
import pytest
# 界说测验过程:simple_step1
@allure.step
def simple_step1(step_param1, step_param2 = None):
'''界说一个测验过程'''
print(f"过程1:翻开页面,参数1: {step_param1}, 参数2:{step_param2}")
# 界说测验过程:simple_step2
@allure.step
def simple_step2(step_param):
'''界说一个测验过程'''
print(f"过程2:完结查找 {step_param} 功用")
@pytest.mark.parametrize('param1', ["pytest", "allure"], ids=['search pytest', 'search allure'])
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) # 调用过程二
2)办法二:运用 with allure.step() 增加测验过程。
# 办法二:运用 `with allure.step()` 增加测验过程
@allure.title("查找用例")
def test_step_in_method():
with allure.step("测验过程一:翻开页面"):
print("操作 a")
print("操作 b")
with allure.step("测验过程二:查找"):
print("查找操作 ")
with allure.step("测验过程三:断言"):
assert True
6、allure2陈述中增加用例链接
1)运用场景:将陈述与 bug 办理体系或测验用例办理体系集成,能够增加链接装修器 @allure.link、@allure.issue 和@allure.testcase。
①格局 1:@allure.link(url, name) 增加一个一般的 link 链接。
②格局 2:@allure.testcase(url, name) 增加一个用例办理体系链接。
③格局 3:@allure.issue(url, name),增加 bug 办理体系
# 格局1:增加一个一般的link 链接
@allure.link('https://ceshiren.com/t/topic/15860')
def test_with_link():
pass
# 格局1:增加一个一般的link 链接,增加链接称号
@allure.link('https://ceshiren.com/t/topic/15860', name='这是用例链接地址')
def test_with_named_link():
pass
# 格局2:增加用例办理体系链接
TEST_CASE_LINK = 'https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'
@allure.testcase(TEST_CASE_LINK, '用例办理体系')
def test_with_testcase_link():
pass
# 格局3:增加bug办理体系链接
# 这个装修器在展现的时候会带 bug 图标的链接。能够在运转时经过参数 `--allure-link-pattern` 指定一个模板链接,以便将其与提供的问题链接类型链接模板一同运用。履行指令需求指定模板链接:
`--allure-link-pattern=issue:https://abc.com/t/topic/{}`
@allure.issue("15860", 'bug办理体系')
def test_with_issue():
pass
7、allure2陈述中增加用例分类
(1)Allure 分类
1)运用场景:能够为项目,以及项目下的不同模块对用例进行分类办理。也能够运转某个类别下的用例。
2)陈述展现:类别会展现在测验陈述的 Behaviors 栏目下。
3)Allure 提供了三个装修器:
①@allure.epic:灵敏里边的概念,界说史诗,往下是 feature。
②@allure.feature:功用点的描绘,理解成模块往下是 story。
③@allure.story:故事 story 是 feature 的子集。
(2)Allure 分类 – epic
1)场景:期望在测验陈述中看到用例所在的项目,需求用到 epic,相当于界说一个项目的需求,因为粒度比较大,在 epic 下面还要界说略小粒度的用户故事。
2)装修器:@allure.epic
import allure
@allure.epic("需求1")
class TestEpic:
def test_case1(self):
print("用例1")
def test_case2(self):
print("用例2")
def test_case3(self):
print("用例3")
(3)Allure 分类 – feature/story
1)场景: 期望在陈述中看到测验功用,子功用或场景。
2)装修器: @allure.Feature、@allure.story
3)过程:
①功用上加 @allure.feature('功用称号')
②子功用上加 @allure.story('子功用称号')
import allure
@allure.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")
4)Allure 运转 feature/story,allure 相关的指令检查 : pytest –help|grep allure
#经过指定指令行参数,运转 epic/feature/story 相关的用例:
pytest 文件名
--allure-epics=EPICS_SET --allure-features=FEATURES_SET --allure-stories=STORIES_SET
# 只运转 epic 名为 "需求1" 的测验用例
pytest --alluredir ./results --clean-alluredir --allure-epics=需求1
# 只运转 feature 名为 "功用模块2" 的测验用例
pytest --alluredir ./results --clean-alluredir --allure-features=功用模块2
# 只运转 story 名为 "子功用1" 的测验用例
pytest --alluredir ./results --clean-alluredir --allure-stories=子功用1
# 运转 story 名为 "子功用1和子功用2" 的测验用例
pytest --alluredir ./results --clean-alluredir --allure-stories=子功用1,子功用2
# 运转 feature + story 的用例(取并集)
pytest --alluredir ./results --clean-alluredir --allure-features=功用模块1 --allure-stories=子功用1,子功用2
Allure epic/feature/story 的联系
5)总结
①epic:灵敏里边的概念,用来界说史诗,相当于界说一个项目。
②feature:相当于一个功用模块,相当于 testsuite,能够办理许多个子分支 story。
③story:相当于对应这个功用或许模块下的不同场景,分支功用。
④epic 与 feature、feature 与 story 类似于父子联系。
8、Allure2 陈述中增加用例描绘
1)运用场景:Allure 支撑往测验陈述中对测验用例增加十分具体的描绘语,用来描绘测验用例详情。
2)Allure 增加描绘的四种办法:
①办法一:运用装修器 @allure.description() 传递一个字符串参数来描绘测验用例。
@allure.description("""
多行描绘语:<br/>
这是经过传递字符串参数的办法增加的一段描绘语,<br/>
运用的是装修器 @allure.description
""")
def test_description_provide_string():
assert True
②办法二:运用装修器 @allure.description_html 传递一段 HTML 文本来描绘测验用例。
@allure.description_html("""html代码块""")
def test_description_privide_html():
assert True
③办法三:直接在测验用例办法中经过编写文档注释的办法来增加描绘。
def test_description_docstring():
"""
直接在测验用例办法中
经过编写文档注释的办法
来增加描绘。
:return:
"""
assert True
④办法四:用例代码内部动态增加描绘信息。
import allure
@allure.description("""这个描绘将被替换""")
def test_dynamic_description():
assert 42 == int(6 * 7)
allure.dynamic.description('这是终究的描绘信息')
# allure.dynamic.description_html(''' html 代码块 ''')
9、Allure2陈述中增加用例优先级
1)运用场景:用例履行时,期望按照严重等级履行测验用例。
2)处理:能够为每个用例增加一个等级的装修器,用法:@allure.severity。
3)Allure 对严重等级的界说分为 5 个等级:
①Blocker等级:中止缺点(客户端程序无呼应,无法履行下一步操作)。
②Critical等级:临界缺点( 功用点缺失)。
③Normal等级:一般缺点(数值计算过错)。
④Minor等级:非必须缺点(界面过错与UI需求不符)。
⑤Trivial等级:细微缺点(必输项无提示,或许提示不规范)。
4)运用装修器增加用例办法/类的等级。类上增加的等级,对类中没有增加等级的办法生效。
#运转时增加指令行参数 --allure-severities:
pytest --alluredir ./results --clean-alluredir --allure-severities normal,blocker
import allure
def test_with_no_severity_label():
pass
@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
pass
@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
pass
@allure.severity(allure.severity_level.NORMAL)
class TestClassWithNormalSeverity(object):
def test_inside_the_normal(self):
pass
@allure.severity(allure.severity_level.CRITICAL)
def test_critical_severity(self):
pass
@allure.severity(allure.severity_level.BLOCKER)
def test_blocker_severity(self):
pass
10、allure2陈述中增加用例支撑tags标签
(1)Allure2 增加用例标签-xfail、skipif
1)用法:运用装修器 @pytest.xfail()、@pytest.skipif()
import pytest
# 当用例经过期标注为 xfail
@pytest.mark.xfail(condition=lambda: True, reason='这是一个预期失败的用例')
def test_xfail_expected_failure():
"""this test is a xfail that will be marked as expected failure"""
assert False
# 当用例经过期标注为 xpass
@pytest.mark.xfail
def test_xfail_unexpected_pass():
"""this test is a xfail that will be marked as unexpected success"""
assert True
# 跳过用例
@pytest.mark.skipif('2 + 2 != 5', reason='当条件触发时这个用例被跳过 @pytest.mark.skipif')
def test_skip_by_triggered_condition():
pass
(2)Allure2 增加用例标签-fixture
1)运用场景:fixture 和 finalizer 是分别在测验开始之前和测验结束之后由 Pytest 调用的实用程序函数。Allure 跟踪每个 fixture 的调用,并具体显现调用了哪些办法以及哪些参数,从而坚持了调用的正确顺序。
import pytest
@pytest.fixture()
def func(request):
print("这是一个fixture办法")
# 界说一个完结器,teardown动作放在完结器中
def over():
print("session等级完结器")
request.addfinalizer(over)
class TestClass(object):
def test_with_scoped_finalizers(self,func):
print("测验用例")
11、allure2陈述中支撑记载失败重试功用
1)Allure 能够收集用例运转期间,重试的用例的成果,以及这段时间重试的历史记载。
2)重试功用能够运用 pytest 相关的插件,例如 pytest-rerunfailures。重试的成果信息,会展现在详情页面的”Retries” 选项卡中。
import pytest
@pytest.mark.flaky(reruns=2, reruns_delay=2) # reruns重试次数,reruns_delay每次重试间隔时间(秒)
def test_rerun2():
assert False
12、Allure2 陈述中增加附件-图片
(1)运用场景:
在做 UI 主动化测验时,能够将页面截图,或许犯错的页面进行截图,将截图增加到测验陈述中展现,辅助定位问题。
(2)处理方案:
①Python:运用 allure.attach 或许 allure.attach.file() 增加图片。
②Java:直接经过注解或调用办法增加。
(3)python办法一:
1)语法:allure.attach.file(source, name, attachment_type, extension),
2)参数解说:
①source:文件途径,相当于传一个文件。
②name:附件姓名。
③attachment_type:附件类型,是 allure.attachment_type 其间的一种(支撑 PNG、JPG、BMP、GIF 等)。
④extension:附件的扩展名。
import allure
class TestWithAttach:
def test_pic(self):
allure.attach.file("pic.png", name="图片", attachment_type=allure.attachment_type.PNG, extension="png")
(4)python办法二
1)语法:allure.attach(body, name=None, attachment_type=None, extension=None)
2)参数解说:
①body:要写入附件的内容
②name:附件姓名。
③attachment_type:附件类型,是 allure.attachment_type 其间的一种(支撑 PNG、JPG、BMP、GIF 等)。
④extension:附件的扩展名。
(5)裂图的原因以及处理办法
1)图片上传过程中呈现了网络中止或许传输过程中呈现了过错。
处理方案:重新上传图片。
2)Allure 陈述中的图片大小超过了 Allure 的限制。
处理方案:调整图片大小。
3)图片自身存在问题。
处理方案:检查图片格局和文件自身。
13、Allure2陈述中增加附件-日志
(1)运用场景:
陈述中增加具体的日志信息,有助于分析定位问题。
(2)处理方案:
1)Python:运用 python 自带的 logging 模块生成日志,日志会主动增加到测验陈述中。
①日志装备,在测验陈述中运用 logger 目标生成对应等级的日志。
# 创立一个日志模块: log_util.py
import logging
import os
from logging.handlers import RotatingFileHandler
# 绑定绑定句柄到logger目标
logger = logging.getLogger(__name__)
# 获取当时东西文件所在的途径
root_path = os.path.dirname(os.path.abspath(__file__))
# 拼接当时要输出日志的途径
log_dir_path = os.sep.join([root_path, f'/logs'])
if not os.path.isdir(log_dir_path):
os.mkdir(log_dir_path)
# 创立日志记载器,指明日志保存途径,每个日志的大小,保存日志的上限
file_log_handler = RotatingFileHandler(os.sep.join([log_dir_path, 'log.log']), maxBytes=1024 * 1024, backupCount=10 , encoding="utf-8")
# 设置日志的格局
date_string = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(
'[%(asctime)s] [%(levelname)s] [%(filename)s]/[line: %(lineno)d]/[%(funcName)s] %(message)s ', date_string)
# 日志输出到控制台的句柄
stream_handler = logging.StreamHandler()
# 将日志记载器指定日志的格局
file_log_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)
# 为大局的日志东西目标增加日志记载器
# 绑定绑定句柄到logger目标
logger.addHandler(stream_handler)
logger.addHandler(file_log_handler)
# 设置日志输出等级
logger.setLevel(level=logging.INFO)
②代码输出到用例详情页面。运转用例指令:pytest --alluredir ./results --clean-alluredir(留意不要加-vs)。
import allure
from pytest_test.log_util import logger
@allure.feature("功用模块2")
class TestWithLogger:
@allure.story("子功用1")
@allure.title("用例1")
def test_case1(self):
logger.info("用例1的 info 等级的日志")
logger.debug("用例1的 debug 等级的日志")
logger.warning("用例1的 warning 等级的日志")
logger.error("用例1的 error 等级的日志")
logger.fatal("用例1的 fatal 等级的日志")
③日志展现在 Test body 标签下,标签下可展现多个子标签代表不同的日志输出渠道:
log 子标签:展现日志信息。
stdout 子标签:展现 print 信息。
stderr 子标签:展现终端输出的信息。
④禁用日志,能够运用指令行参数控制 --allure-no-capture
pytest --alluredir ./results --clean-alluredir --allure-no-capture
-- 日志文件为byte[]类型增加。
public void exampleTest() {
byte[] contents = Files.readAllBytes(Paths.get("a.txt"));
attachTextFile(byte[]的文件, "描绘信息");
}
@Attachment(value = "{attachmentName}", type = "text/plain")
public byte[] attachTextFile(byte[] contents, String attachmentName) {
return contents;
}
②调用办法增加。
--String类型增加。 日志文件为String类型
Allure.addAttachment("描绘信息", "text/plain", 文件读取为String,"txt");
--InputStream类型增加。日志文件为InputStream流
Allure.addAttachment("描绘信息", "text/plain", Files.newInputStream(文件Path), "txt");
14、allure2陈述中增加附件-html
(1)运用场景
能够定制测验陈述页面效果,能够将 HTML 类型的附件显现在陈述页面上。
(2)处理方案:
1)Python:运用 allure.attach() 增加 html 代码。
①语法:allure.attach(body, name, attachment_type, extension),
②参数解说:
body:要写入附件的内容(HTML 代码块)。
name:附件姓名。
attachment_type:附件类型,是 allure.attachment_type 其间的一种。
extension:附件的扩展名。
import allure
class TestWithAttach:
def test_html(self):
allure.attach('<head></head><body> a page </body>',
'附件是HTML类型',
allure.attachment_type.HTML)
def test_html_part(self):
allure.attach('''html代码块''',
'附件是HTML类型',
allure.attachment_type.HTML)
15、Allure2 陈述中增加附件-视频
(1)运用场景:
在做 UI 主动化测验时,能够将页面截图,或许犯错的页面进行截图,将截图增加到测验陈述中展现,辅助定位问题。
(2)处理方案:
1)Python:运用 allure.attach.file() 增加视频。
①语法:allure.attach.file(source, name, attachment_type, extension)
②参数解说:
source:文件途径,相当于传一个文件。
name:附件姓名。
attachment_type:附件类型,是 allure.attachment_type 其间的一种。
extension:附件的扩展名。
import allure
class TestWithAttach:
def test_video(self):
allure.attach.file("xxx.mp4", name="视频", attachment_type=allure.attachment_type.MP4, extension="mp4")
16、Allure2 陈述定制
运用场景:针对不同的项目或许需求对测验陈述展现的效果进行定制,比如修正页面的 logo、修正项目的标题或许增加一些定制的功用等等。
(1)页面 Logo
1)修正allure.yml 文件,在后面增加 logo 插件:custom-logo-plugin(在 allure 装置途径下:/allure-2.21.0/config/allure.yml,能够经过 where allure 或许which allure检查 allure 装置途径)
2)修改 styles.css 文件,装备 logo 图片(logo图片能够提前准备好放在/custom-logo-plugin/static中)
/* 翻开 styles.css 文件,
目录在装置allure时,解压的途径:/xxx/allure-2.21.0/plugins/custom-logo-plugin/static/styles.css,
将内容修正图片logo和相应的大小:*/
.side-nav__brand {
background: url("logo.jpeg") no-repeat left center !important;
margin-left: 10px;
height: 40px;
background-size: contain !important;
}
(2)页面标题
1)修改 styles.css 文件,增加修正标题对应的代码。
/* 去掉图片后边 allure 文本 */
.side-nav__brand-text {
display: none;
}
/* 设置logo 后面的字体样式与字体大小 */
.side-nav__brand:after {
content: "测验陈述";
margin-left: 18px;
height: 20px;
font-family: Arial;
font-size: 13px;
}