既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
应用场景:将报告与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
七.Allure2报告中添加用例分类
1.Allure
分类
(1)应用场景:可以为项目,以及项目下的不同模块对用例进行分类管理。也可以运行某个类别下的用例。
(2)报告展示:类别会展示在测试报告的Behaviors
栏目下。
(3)Allure
提供了三个装饰器:
@allure.epic
:敏捷里面的概念,定义史诗,往下是feature
。@allure.feature
:功能点的描述,理解成模块往下是story
。@allure.story
:故事story
是feature
的子集。
2.Allure
分类 - epic
- 场景:希望在测试报告中看到用例所在的项目,需要用到
epic
,相当于定义一个项目的需求,由于粒度比较大,在epic
下面还要定义略小粒度的用户故事。 - 装饰器:
@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
- 场景: 希望在报告中看到测试功能,子功能或场景。
- 装饰器:
@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
类似于父子关系。
八.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 代码块 ''')
九.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
十.Allure2报告中添加用例支持tags标签
1.Allure2
添加用例标签-xfail
、skipif
import pytest
# 用法:使用装饰器 @pytest.xfail()、@pytest.skipif()
# 当用例通过时标注为 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
应用场景: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("测试用例")
十一.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
十二.Allure2 报告中添加附件-图片
1.应用场景:在做 UI
自动化测试时,可以将页面截图,或者出错的页面进行截图,将截图添加到测试报告中展示,辅助定位问题。
2.解决方案
- Python:使用
allure.attach
或者allure.attach.file()
添加图片。 - Java:直接通过注解或调用方法添加。
3.python
方法一
语法:allure.attach.file(source, name, attachment_type, extension),
参数解释:
①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
方法二
语法:allure.attach(body, name=None, attachment_type=None, extension=None)
参数解释:
①body:要写入附件的内容
②name:附件名字。
③attachment_type:附件类型,是 allure.attachment_type 其中的一种(支持 PNG、JPG、BMP、GIF 等)。
④extension:附件的扩展名。
5.裂图的原因以及解决办法
- 图片上传过程中出现了网络中断或者传输过程中出现了错误。 解决方案:重新上传图片。
Allure
报告中的图片大小超过了Allure
的限制。 解决方案:调整图片大小。- 图片本身存在问题。 解决方案:检查图片格式和文件本身。
十三.Allure2报告中添加附件-日志
1.应用场景:报告中添加详细的日志信息,有助于分析定位问题。 2.解决方案:使用 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
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");
十四.Allure2报告中添加附件-html
1.应用场景:可以定制测试报告页面效果,可以将 HTML
类型的附件显示在报告页面上。
2.解决方案:使用 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)
十五.Allure2 报告中添加附件-视频
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
附件是HTML类型’,
allure.attachment_type.HTML)
def test_html_part(self):
allure.attach('''html代码块''',
'附件是HTML类型',
allure.attachment_type.HTML)
### **十五.Allure2 报告中添加附件-视频**
[外链图片转存中...(img-WXu0Jw9d-1714993788056)]
[外链图片转存中...(img-NyMVNVP7-1714993788056)]
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618608311)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**