Golang单元测试结果输出方式

本文介绍了三种单元测试结果的输出方式:Testing终端输出,适用于终端打印;Logrus,支持文件输出,提供TextFormatter和JSONFormatter两种格式;Tebeka重定向,支持文件输出,内容格式为XML,适用于Windows和Linux系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单元测试结果输出方式

目前支持Testing终端输出、Logrus、Tebeka重定向。

Testing终端输出
  1. 此种方式只支持终端打印的结果输出

  2. 输入样例如下:

    time="2018-12-26T10:16:25+08:00" level=info msg="GetQueueTotal out values normal"
    --- PASS: TestGetQueueTotal (0.07s)
            apis_test.go:13: test gets queue total function passed
    === RUN   TestGetQueueServicesSate
           0
    
    time="2018-12-26T10:16:25+08:00" level=info msg="GetQueueServicesSate out values normal"
    --- PASS: TestGetQueueServicesSate (0.03s)
            apis_test.go:26: 0 testing mq service state function normal
    === RUN   TestCreateDir
    --- PASS: TestCreateDir (0.00s)
            utils_test.go:19: test success to create file directory function
    === RUN   TestGetTime
    --- PASS: TestGetTime (0.00s)
            utils_test.go:43: 2018-12-26.log successful test acquisition of current system time function
    PASS
    ok      mq_exporter/component   0.888s`
    
Logrus输出

​ 支持文件输出,输出的格式为TextFormatter和JSONFormatter

  • JSONFormatter的输出样例
  {"level":"info","msg":"exporter start..","time":"2018-12-26T10:56:18+08:00"}
  {"level":"info","msg":"GetQueueTotal out values normal","time":"2018-12-26T10:56:18+08:00"}
  {"level":"info","msg":"GetQueueTotal out values normal","time":"2018-12-26T10:56:19+08:00"}
  {"level":"info","msg":"GetQueueServicesSate out values normal","time":"2018-12-26T10:56:19+08:00"}
  {"level":"info","msg":"GetQueueTotal out values normal","time":"2018-12-26T10:57:03+08:00"}
  {"level":"info","msg":"GetQueueTotal out values normal","time":"2018-12-26T10:57:04+08:00"}
  {"level":"info","msg":"GetQueueServicesSate out values normal","time":"2018-12-26T10:57:04+08:00"}
  {"level":"info","msg":"GetQueueTotal out values normal","time":"2018-12-26T10:57:09+08:00"}
  {"level":"info","msg":"GetQueueTotal out values normal","time":"2018-12-26T10:57:09+08:00"}
  {"level":"info","msg":"GetQueueServicesSate out values normal","time":"2018-12-26T10:57:09+08:00"}
  • TextFormatter的输出样例:

    time="2015-03-26T01:27:38-04:00" level=debug msg="Started observing beach" animal=walrus number=8
    time="2015-03-26T01:27:38-04:00" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
    time="2015-03-26T01:27:38-04:00" level=warning msg="The group's number increased tremendously!" number=122 omg=true
    time="2015-03-26T01:27:38-04:00" level=debug msg="Temperature changes" temperature=-4
    time="2015-03-26T01:27:38-04:00" level=panic msg="It's over 9000!" animal=orca size=9009
    time="2015-03-26T01:27:38-04:00" level=fatal msg="The ice breaks!" err=&{0x2082280c0 map[animal:orca size:9009] 2015-03-26 01:27:38.441574009 -0400 EDT panic It's over 9000!} number=100 omg=true
    
Tebeka重定向
  1. 支持文件输出,内容格式为xml,目前支持的操作系统为windows,linux,暂不支持Aix
  2. 输出样例如下:
   <?xml version="1.0" encoding="UTF-8"?>
     <testsuite name="mq_exporter/component" tests="4" errors="0" failures="1" skip="0">
       <testcase classname="mq_exporter/component" name="TestGetQueueTotal" time="0.09">
       </testcase>
       <testcase classname="mq_exporter/component" name="TestGetQueueServicesSate" time="0.06">
         <failure type="go.error" message="error">
           <![CDATA[Microsoft Windows [?? 10.0.17134.472]
   (c) 2018 Microsoft Corporation????????
   
   E:\Workplace_Go\src\mq_exporter\component>
   time="2018-12-24T16:45:12+08:00" level=info msg="GetQueueServicesSate out values normal"
   time="2018-12-24T16:45:12+08:00" level=error msg="strconv.Atoi: parsing \"MicrosoftWindows[\\xb0??0.0.17134.472]\\r(c)2018MicrosoftCorporation\\xa1\\xa3\\xb1\\xa3\\xc1\\xf4\\xcb\\xf9\\xd3\\xd0?\\xc0\\xfb\\xa1\\xa3\\r\\rE:\\\\Workplace_Go\\\\src\\\\mq_exporter\\\\component>\": invalid syntax" function=GetQueueServicesSate
   	apis_test.go:23: testing failed to obtain mq service state function]]>
         </failure>    </testcase>
       <testcase classname="mq_exporter/component" name="TestCreateDir" time="0.00">
   
       </testcase>
       <testcase classname="mq_exporter/component" name="TestGetTime" time="0.00">
   
       </testcase>
     </testsuite>
Golang中,单元测试是开发人员在编写代码时编写的一种测试方法,它关注代码的局部而不是整体。单元测试的目的是验证代码的各个单元(函数、方法、类等)是否按照预期工作。单元测试通常比其他测试运行得更快,并且可以帮助开发人员在代码编写过程中及时发现和修复问题。\[1\] 在Golang中,我们可以使用testing包来编写和运行单元测试。在测试函数中,我们可以使用testing.T类型的参数来进行断言和错误报告。如果需要跳过某个测试,可以使用testing.T的Skip方法。例如,可以使用testing.Short()函数来判断是否在短模下运行测试,并使用t.Skip方法跳过测试。\[2\] 要运行Golang单元测试,可以使用go test命令。默认情况下,go test会运行当前目录下的所有测试文件。可以使用-v选项来输出详细的日志信息。测试运行成功时,会显示PASS,测试失败时,会显示FAIL。如果只想测试单个文件,可以在go test命令后面加上被测试的原文件。如果只想测试单个方法,可以使用-run选项指定要运行的测试方法的名称。\[3\] 希望这些信息对你有帮助! #### 引用[.reference_title] - *1* *2* [Golang单元测试](https://blog.youkuaiyun.com/LinAndCurry/article/details/122324323)[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^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Golang-单元测试](https://blog.youkuaiyun.com/ALEX_CYL/article/details/121793330)[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^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值