简体   繁体   English

在 golang 中使用其他外部库打印测试日志

[英]Testing logs printed using other external library in golang

My application uses other external library (which uses zap library) to print audit-logs and now I want to test the printed audit-logs in my golang testing file.我的应用程序使用其他外部库(使用 zap 库)来打印审计日志,现在我想在我的 golang 测试文件中测试打印的审计日志。 Can someone help me with it.有人可以帮我吗?

You could take a look at the zaptest package:你可以看看zaptest package:

Package zaptest provides a variety of helpers for testing log output. Package zaptest提供了多种测试日志的helpers output。

As example, in your test:例如,在您的测试中:

func Test_zap(t *testing.T) {

    t.Run("Handle log message", func(t *testing.T) {
        // Given
        observedZapCore, observedLogs := observer.New(zap.InfoLevel)
        observedLogger := zap.New(observedZapCore)

        // When
        myFunction(observedLogger)

        // Then
        require.Equal(t, 1, observedLogs.Len())
        firstLog := observedLogs.All()[0]
        assert.Equal(t, "log myFunction", firstLog.Message)

    })
}

where myfunction is我的功能在哪里

func myFunction(logger *zap.Logger) {
    logger.Info("log myFunction")
}

Check also this interesting article about that另请查看这篇有趣的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Jasmine中的外部库进行单元测试 - Unit testing with external library in Jasmine Golang 单元测试使用 PanicsWithValue - Golang unit testing using PanicsWithValue 是否可以使用Jest或任何其他测试库/框架加载URL并获取浏览器呈现的DOM对象? - Is it possible to load url and get the browser rendered DOM object using Jest or any other testing library/framework? 使用Google Analytics(分析)等外部程式库进行Angular JS测试 - Angular JS testing with external library like Google Analytics Silverlight单元测试框架在外部类库中运行测试 - Silverlight Unit Testing Framework running tests in external class library 单元测试时,模拟外部库的方法有什么意义? - What is the point of mocking the method of an external library when unit testing? 在golang中使用子进程测试时如何生成单元测试覆盖率? - How to generate unit test coverage when using subprocess testing in golang? 工匠命令测试输出“+----+----------+------------+--------------- -------+”未打印 - artisan command testing Output "+----+----------+------------+----------------------+" was not printed 使用功能进行Golang测试 - Golang testing with functions 导出Golang软件包进行测试? - Exporting Golang packages for testing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM