如何使用pytest的allure.attach在测试报告中为用例添加附件?

2026-05-24 18:181阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计706个文字,预计阅读时间需要3分钟。

如何使用pytest的allure.attach在测试报告中为用例添加附件?

前言:allure测试报告支持展示不同类型的附件,包括对测试用例、测试步骤及fixture函数的补充。使用allure.attach()或allure.attach.file()可以附加测试用例执行结果。

使用allure.attach()或allure.attach.file()的作用是:为allure测试报告添加测试用例执行结果的补充。

前言

allure测试报告同样支持显示不同类型的附件,对测试用例、测试步骤以及fixture函数的结果加以补充。

allure.attach()或者allure.attach.file的作用是为allure测试报告的测试用例执行结果添加附件。

allure.attach的用法一:(最常用)

语法:

allure.attach(body, name, attachment_type, extension)

参数解释:

  • body:要写入附件的内容;
  • name:附件名字;
  • attachment_type:附件类型,是allure.attachment_type其中的一种;
  • extension:附件的扩展名;

allure.attach的用法二:

语法:

如何使用pytest的allure.attach在测试报告中为用例添加附件?

allure.attach.file(source, name, attachment_type, extension)

参数解释:

  • source:文件路径,相当于传一个文件;
  • name:附件名字;
  • attachment_type:附件类型,是allure.attachment_type其中的一种;
  • extension:附件的扩展名;

allure.attachment_type的所有值列举:

TEXT = ("text/plain", "txt")
CSV = ("text/csv", "csv")
TSV = ("text/tab-separated-values", "tsv")
URI_LIST = ("text/uri-list", "uri")

HTML = ("text/html", "html")
XML = ("application/xml", "xml")
JSON = ("application/json", "json")
YAML = ("application/yaml", "yaml")
PCAP = ("application/vnd.tcpdump.pcap", "pcap")

PNG = ("image/png", "png")
JPG = ("image/jpg", "jpg")
SVG = ("image/svg-xml", "svg")
GIF = ("image/gif", "gif")
BMP = ("image/bmp", "bmp")
TIFF = ("image/tiff", "tiff")

MP4 = ("video/mp4", "mp4")
OGG = ("video/ogg", "ogg")
WEBM = ("video/webm", "webm")

PDF = ("application/pdf", "pdf")

allure.attach使用举例:

1、测试用例中添加文本附件:

# file_name: test_allure_attachments.py


import pytest
import allure


@pytest.fixture()
def attach_for_text():
allure.attach(body="这是一段文本,setUp", name="test文本01", attachment_type=allure.attachment_type.TEXT)
yield
allure.attach(body="这是一段文本,teardown", name="test文本02", attachment_type=allure.attachment_type.TEXT)


def test_attachment_text(attach_for_text):
pass


if __name__ == '__main__':
pytest.main(['-s', 'test_allure_attachments.py'])

执行命令:

> pytest test_allure_attachments.py --alluredir=./report/result_data

> allure serve ./report/result_data

查看测试报告展示效果:

从测试报告中可以看到,通过使用allure.attach指定attachment_type=allure.attachment_type.TEXT,往测试用例中添加了一段文本。

2、测试用例中添加图片以及HTML附件:

# file_name: test_allure_attachments.py


import pytest
import allure


def test_mutiple_attachments():
allure.attach.file("./pytest_study/image/pikaqiu.jpg", attachment_type=allure.attachment_type.JPG)

allure.attach("<html><body><font color='red'>这是一段html</font></body></html>",
attachment_type=allure.attachment_type.HTML)


if __name__ == '__main__':
pytest.main(['-s', 'test_allure_attachments.py'])

执行命令:

> pytest test_allure_attachments.py --alluredir=./report/result_data

> allure serve ./report/result_data

查看测试报告展示效果:

从上面的报告中可以看到:

通过allure.attach.file()指定attachment_type=allure.attachment_type.JPG的方式往测试报告中添加了一张图片;

通过allure.attach()指定attachment_type=allure.attachment_type.HTML的方式往测试报告中添加了一段HTML内容;

去期待陌生,去拥抱惊喜。

本文共计706个文字,预计阅读时间需要3分钟。

如何使用pytest的allure.attach在测试报告中为用例添加附件?

前言:allure测试报告支持展示不同类型的附件,包括对测试用例、测试步骤及fixture函数的补充。使用allure.attach()或allure.attach.file()可以附加测试用例执行结果。

使用allure.attach()或allure.attach.file()的作用是:为allure测试报告添加测试用例执行结果的补充。

前言

allure测试报告同样支持显示不同类型的附件,对测试用例、测试步骤以及fixture函数的结果加以补充。

allure.attach()或者allure.attach.file的作用是为allure测试报告的测试用例执行结果添加附件。

allure.attach的用法一:(最常用)

语法:

allure.attach(body, name, attachment_type, extension)

参数解释:

  • body:要写入附件的内容;
  • name:附件名字;
  • attachment_type:附件类型,是allure.attachment_type其中的一种;
  • extension:附件的扩展名;

allure.attach的用法二:

语法:

如何使用pytest的allure.attach在测试报告中为用例添加附件?

allure.attach.file(source, name, attachment_type, extension)

参数解释:

  • source:文件路径,相当于传一个文件;
  • name:附件名字;
  • attachment_type:附件类型,是allure.attachment_type其中的一种;
  • extension:附件的扩展名;

allure.attachment_type的所有值列举:

TEXT = ("text/plain", "txt")
CSV = ("text/csv", "csv")
TSV = ("text/tab-separated-values", "tsv")
URI_LIST = ("text/uri-list", "uri")

HTML = ("text/html", "html")
XML = ("application/xml", "xml")
JSON = ("application/json", "json")
YAML = ("application/yaml", "yaml")
PCAP = ("application/vnd.tcpdump.pcap", "pcap")

PNG = ("image/png", "png")
JPG = ("image/jpg", "jpg")
SVG = ("image/svg-xml", "svg")
GIF = ("image/gif", "gif")
BMP = ("image/bmp", "bmp")
TIFF = ("image/tiff", "tiff")

MP4 = ("video/mp4", "mp4")
OGG = ("video/ogg", "ogg")
WEBM = ("video/webm", "webm")

PDF = ("application/pdf", "pdf")

allure.attach使用举例:

1、测试用例中添加文本附件:

# file_name: test_allure_attachments.py


import pytest
import allure


@pytest.fixture()
def attach_for_text():
allure.attach(body="这是一段文本,setUp", name="test文本01", attachment_type=allure.attachment_type.TEXT)
yield
allure.attach(body="这是一段文本,teardown", name="test文本02", attachment_type=allure.attachment_type.TEXT)


def test_attachment_text(attach_for_text):
pass


if __name__ == '__main__':
pytest.main(['-s', 'test_allure_attachments.py'])

执行命令:

> pytest test_allure_attachments.py --alluredir=./report/result_data

> allure serve ./report/result_data

查看测试报告展示效果:

从测试报告中可以看到,通过使用allure.attach指定attachment_type=allure.attachment_type.TEXT,往测试用例中添加了一段文本。

2、测试用例中添加图片以及HTML附件:

# file_name: test_allure_attachments.py


import pytest
import allure


def test_mutiple_attachments():
allure.attach.file("./pytest_study/image/pikaqiu.jpg", attachment_type=allure.attachment_type.JPG)

allure.attach("<html><body><font color='red'>这是一段html</font></body></html>",
attachment_type=allure.attachment_type.HTML)


if __name__ == '__main__':
pytest.main(['-s', 'test_allure_attachments.py'])

执行命令:

> pytest test_allure_attachments.py --alluredir=./report/result_data

> allure serve ./report/result_data

查看测试报告展示效果:

从上面的报告中可以看到:

通过allure.attach.file()指定attachment_type=allure.attachment_type.JPG的方式往测试报告中添加了一张图片;

通过allure.attach()指定attachment_type=allure.attachment_type.HTML的方式往测试报告中添加了一段HTML内容;

去期待陌生,去拥抱惊喜。