要向Allure测试报告添加照片,可以使用Allure附件机制。以下是将照片从文件夹中添加到Allure报告的Python代码示例:
import allure
# 定义附件名和附件类型
@allure.attachment("Screenshot", allure.attachment_type.PNG)
def attach_screenshot():
# 打开照片文件
with open('path/to/image.png', 'rb') as f:
# 读取照片文件并返回
return f.read()
def test_example_with_screenshot():
# 测试代码
# ...
# 添加照片附件
allure.attach(attach_screenshot(), name="Screenshot", attachment_type=allure.attachment_type.PNG)
以上代码中,attach_screenshot()
函数定义了一个名为 "Screenshot" 的附件,它的类型是 PNG。该函数返回包含读取的照片数据的字节流。在测试函数中,我们可以使用 allure.attach()
方法将该附件添加到Allure报告中。
请注意,这里我们没有直接将文件路径传递给Allure附件,而是在 attach_screenshot()
函数中打开了文件并返回文件内容。这是因为在不同的执行环境中,文件路径可能不同,甚至可能不存在。因此,我们应该在测试函数内部打开文件,而将它的读取和附加操作封装在函数中。
当测试运行时,Allure报告中将显示名为 "Screenshot" 的附件,并且可以单击它以查看照片。
上一篇:Allure报告嵌入问题