软件测试/测试开发丨Allure2报告中添加附件-日志

本文为霍格沃兹测试开发学社学员学习笔记分享

原文链接:https://ceshiren.com/t/topic/24791

Allure2报告中添加附件-日志

Allure2 报告中添加附件(日志)应用场景

  • 应用场景:报告中添加详细的日志信息,有助于分析定位问题。

  • 解决方案:

    • Python:使用 python 自带的 logging 模块生成日志,日志会自动添加到测试报告中。
    • Java:直接通过注解或调用方法添加。

Allure2 报告中添加日志 - Python

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

Allure2 报告中添加日志 - Python

  • 代码输出到用例详情页面。
  • 运行用例:pytest --alluredir ./results --clean-alluredir(注意不要加-vs)。
@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 级别的日志")

Allure2 报告中添加日志 - Python

  • 日志展示在 Test body 标签下,标签下可展示多个子标签代表不同的日志输出渠道:

    • log 子标签:展示日志信息。
    • stdout 子标签:展示 print 信息。
    • stderr 子标签:展示终端输出的信息。

imageimage100%75%50%

import allure

from utils.log_util import logger


@allure.epic("需求1")
@allure.feature("功能模块一")
class TestEpic:
    @allure.story("子功能一")
    @allure.title("用例1")
    def test_case1(self):
        logger.info("这是 TestEpic 第一条用例")
        print("用例1")

    @allure.story("子功能二")
    @allure.title("用例2")
    def test_case2(self):
        logger.debug("这是 TestEpic 第二条用例")
        print("用例2")

    @allure.story("子功能二")
    @allure.title("用例3")
    def test_case3(self):
        logger.warning("这是 TestEpic 第三条用例")
        print("用例3")

    @allure.story("子功能一")
    @allure.title("用例4")
    def test_case4(self):
        logger.error("这是 TestEpic 第四条用例")
        print("用例4")

    @allure.story("子功能三")
    @allure.title("用例5")
    def test_case5(self):
        print("用例5")

@allure.epic("需求1")
@allure.feature("功能模块二")
class TestEpic1:
    @allure.story("子功能四")
    def test_case1(self):
        print("用例1")

    @allure.story("子功能五")
    def test_case2(self):
        print("用例2")

    def test_case3(self):
        print("用例3")


@allure.epic("需求2")
class TestEpic2:
    def test_case1(self):
        print("用例1")

    def test_case2(self):
        print("用例2")

    def test_case3(self):
        print("用例3")

imageimage100%75%50%

Allure2 报告中添加日志展示功能禁用 - Python

  • 禁用日志,可以使用命令行参数控制 --allure-no-capture
pytest --alluredir ./results --clean-alluredir --allure-no-capture

Allure2 添加附件(日志)实现方法 - Java

Allure 支持两种方法:

  • 注解方式添加。

    • String类型添加。
    • byte[]类型添加。
  • 调用方法添加。

    • String类型添加。
    • InputStream类型添加。

注解方式 - Java

  • 日志文件为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;
}

调用方法 - Java

  • 日志文件为String类型。 java Allure.addAttachment("描述信息", "text/plain", 文件读取为String,"txt");
  • 日志文件为InputStream流。 java Allure.addAttachment( "描述信息","text/plain", Files.newInputStream(文件Path), "txt");
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用的内容,"/usr/local/bin/allure/allure-2.23.1: Not a directory" 是一个错误提示,意味着该路径不是一个目录。根据引用的错误1,这可能是因为全局工具配置的路径有问题导致的。请检查您的配置,确保正确指定了allure的路径。 此外,还有可能是由于其他原因导致的错误。比如引用提到的"bash: /usr/bin/autocrorder: /usr/bin/python^M: bad interpreter: No such file or directory"错误提示,表明解释器找不到。这可能是因为指定的解释器路径不正确导致的。您可以检查并确保指定的解释器路径是正确的。 另外,根据引用的错误2,如果您在生成报告时遇到了名字不一致的问题,可以检查一下shell脚本allure-result路径和生成报告的名字是否一致,确保它们是相同的。 最后,根据引用的错误3,如果提示"pytest: not found",这意味着没有找到pytest命令。您可以将py.test的目录添加到环境变量,确保系统可以找到pytest命令。 综上所述,如果您遇到了"/usr/local/bin/allure/allure-2.23.1: Not a directory"错误,需要检查全局工具配置的路径是否正确。此外,还要确保解释器路径正确,生成报告的名字和路径一致,并且系统能够找到pytest命令。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [bash: /usr/bin/autocrorder: /usr/bin/python^M: bad interpreter: No such file or directory](https://download.csdn.net/download/weixin_38653878/12846530)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [allure报告---动态显示模块名和用例标题](https://blog.csdn.net/lixiaomei0623/article/details/120273737)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [python+allure+jenkins 集成接口自动化 问题总结](https://blog.csdn.net/May_JL/article/details/131974974)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值