Skip to content

Allure2 报告中添加附件 — 日志


简介

在使用 Allure 生成报告时可以将详细的日志信息添加到报告中。


添加日志


Python 版本

  • 步骤一: 日志配置,在测试报告中使用 logger 对象生成对应级别的日志。
# 在 util 包中创建一个日志模块: 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)

  • 步骤二: 添加日志描述。
#需先从util中导入log_util.py模块
@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 级别的日志")

  • 步骤三: 执行用例。
  • 运行用例:pytest [文件名.py] --alluredir ./results --clean-alluredir(注意不要加-vs)。

  • 步骤四:查看报告。
    • 日志展示在 Test body 标签下,标签下可展示多个子标签代表不同的日志输出渠道:
      • log 子标签:展示日志信息。
      • stdout 子标签:展示 print 信息。
      • stderr 子标签:展示终端输出的信息。


  • Allure 报告禁止展示日志

    禁用日志,可以使用命令行参数控制 --allure-no-capture
pytest [文件名.py] --alluredir ./results --clean-alluredir --allure-no-capture

Java 版本

方式一:使用 @Attachmen 注解

  • 日志文件为 String 类型。

    @DisplayName("注解方法 - 文本添加验证")
    @Test
    public void testAllureWithTxtAttachment() {
        //3.添加到注解中
        attachTxtFile(文件读取为String);
    }
    @Attachment(value = "描述信息", type = "text/plain")
    public static String attachTxtFile(String txtContent) {
        return txtContent;
    }
    

  • 日志文件为byte[]类型。

    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;
    }
    

  • 注解方式且日志文件为 byte[]类型。

    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;
    }
    

方式二:调用Allure.addAttachment方法

  • 日志文件为 String 类型。

    Allure.addAttachment("描述信息", "text/plain", 文件读取为String,"txt");
    
  • 日志文件为 InputStream 流。

    Allure.addAttachment( "描述信息","text/plain",
        Files.newInputStream(文件Path), "txt");
    

总结

  • Allure 报告中添加日志附件简介
  • Allure 报告中添加日志实现方法 - Python
  • Allure 报告中添加日志实现方法 - Java