1. 程式人生 > 其它 >allure報告(三)之新增附件(allure.attach)

allure報告(三)之新增附件(allure.attach)

前言

allure測試報告支援顯示不同型別的附件,對測試用例、測試步驟以及fixture的結果加以補充。

allure.attach的用法一:

語法:

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

引數解釋:

  • body:要寫入附件的內容;
  • name:附件名字;
  • attachment_type:附件型別,是allure.attachment_type其中的一種;
  • extension:附件的副檔名;

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內容;

去期待陌生,去擁抱驚喜。