allure报告相关笔记

发布时间:2026/10/31 19:54:40
allure报告相关笔记
一、报告类别

@allure.epic:最高级别的分类,表示一个大的功能、模块或业务场景。功能模块
@allure.feature:表示一个功能模块或业务子场景,位于史诗下。主流程
@allure.story:表示一个具体的测试场景,位于特性下。具体主流程或分支流程-如增、删、改
@allure.title:表示故事中的一个具体测试场景的标题。

from allure_commons.types import AttachmentType是用于Allure 测试报告的附件类型定义模块,它提供了一些预定义的附件类型,帮助你将文件、文本、图片等信息附加到 Allure 测试报告中。


二、报告标签搜索
  • 在报告输入框输入 tag: 相应标签 就可以搜索对应的标签可以,在团队协作中可以打上名称通过@pytest.mark.name打上名称标签让人一看就知道是谁的问题

主要功能

  1. 明确附件类型:

    • 当你将文件、日志、截图或其他数据附加到 Allure 报告时,可以通过指定附件类型,让报告正确识别和渲染这些附件。
    • 比如,AttachmentType.TEXT表示普通文本,AttachmentType.PNG表示图片。
  2. 增强报告的可读性:

    • 不仅展示测试用例的运行结果,还可以附加更多上下文信息,例如请求的 JSON 数据、响应日志、截图等,帮助调试和分析失败原因。

二、AttachmentType

fromallure_commons.typesimportAttachmentType

常用的附件类型

以下是AttachmentType中的一些预定义类型及其作用:

附件类型描述
AttachmentType.TEXT普通文本附件,比如日志或控制台输出。
AttachmentType.JSONJSON 格式的数据附件,用于附加请求或响应数据。
AttachmentType.HTMLHTML 格式附件,比如网页内容或报告片段。
AttachmentType.PNGPNG 格式的图片附件,用于附加截图或图表。
AttachmentType.JPGJPG 格式的图片附件。
AttachmentType.CSVCSV 格式的附件,用于附加表格或数据文件。
AttachmentType.XMLXML 格式的数据附件。
AttachmentType.MARKDOWNMarkdown 格式的文本附件,用于附加格式化文本或文档。
AttachmentType.PDFPDF 文件附件,用于附加文档或截图。

示例用法

在测试中使用AttachmentType将信息附加到 Allure 报告:

1. 附加文本日志
importallure allure.attach("这是测试日志内容",name="Test Log",attachment_type=AttachmentType.TEXT)
2. 附加 JSON 数据
response_data={"status":200,"message":"Success"}allure.attach(str(response_data),name="Response JSON",attachment_type=AttachmentType.JSON)
3. 附加截图
withopen("screenshot.png","rb")asimage:allure.attach(image.read(),name="Screenshot",attachment_type=AttachmentType.PNG)
4. 附加 HTML
html_content="<html><body><h1>Hello, World!</h1></body></html>"allure.attach(html_content,name="Sample HTML",attachment_type=AttachmentType.HTML)

在接口测试中的典型用法

结合 Schemathesis 和 Allure,附加接口请求和响应的数据:

@schema.parametrize()deftest_api(case):# 调用接口response=case.call()# 附加请求信息allure.attach(f"Request URL:{case.path}\nMethod:{case.method}\nHeaders:{case.headers}",name="Request Info",attachment_type=AttachmentType.TEXT)# 附加响应信息allure.attach(response.text,name="Response Body",attachment_type=AttachmentType.JSON)# 验证响应case.validate_response(response)

总结

AttachmentType是 Allure 提供的一种类型标识工具,用于明确附件的格式和用途。通过它,你可以将各种数据(如日志、截图、请求/响应信息)附加到测试报告中,让测试结果更加直观和便于分析。