最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

go - How do I use context logger for unit test assertion failure - Stack Overflow

programmeradmin3浏览0评论

Is there a way to use my own context logger like zap to print out the assertion failure?

func (s *TempSuite) Test() {
    s.Run("Test", func() {
        s.Assertions.True(false, "Failure here")
    })
}

Instead of this:

                Error:          Should be true
                Test:           TestTempSuite/Test/Test
                Messages:       Failure here
--- FAIL: TestTempSuite (0.00s)
    --- FAIL: TestTempSuite/Test (0.00s)
        --- FAIL: TestTempSuite/Test/Test (0.00s)

I'd like something like

{"timestamp":"xxxxxx", "message":"Error:          Should be true
                         \nTest:           TestTempSuite/Test/Test
                         \nMessages:       Failure here
                         \n--- FAIL: TestTempSuite (0.00s)
                         \n--- FAIL: TestTempSuite/Test (0.00s)
                         \n--- FAIL: TestTempSuite/Test/Test (0.00s)",  "testsuite":"xxxxx", "myTag": "xxxxxxx"}

Is there perhaps a way to pipe the output to my own logger?

Is there a way to use my own context logger like zap to print out the assertion failure?

func (s *TempSuite) Test() {
    s.Run("Test", func() {
        s.Assertions.True(false, "Failure here")
    })
}

Instead of this:

                Error:          Should be true
                Test:           TestTempSuite/Test/Test
                Messages:       Failure here
--- FAIL: TestTempSuite (0.00s)
    --- FAIL: TestTempSuite/Test (0.00s)
        --- FAIL: TestTempSuite/Test/Test (0.00s)

I'd like something like

{"timestamp":"xxxxxx", "message":"Error:          Should be true
                         \nTest:           TestTempSuite/Test/Test
                         \nMessages:       Failure here
                         \n--- FAIL: TestTempSuite (0.00s)
                         \n--- FAIL: TestTempSuite/Test (0.00s)
                         \n--- FAIL: TestTempSuite/Test/Test (0.00s)",  "testsuite":"xxxxx", "myTag": "xxxxxxx"}

Is there perhaps a way to pipe the output to my own logger?

Share Improve this question edited Mar 29 at 13:33 hydradon asked Mar 28 at 19:55 hydradonhydradon 1,4542 gold badges24 silver badges57 bronze badges 2
  • If you just want to have JSON output - use go 1.24 and -json flag – Anatoly Commented Mar 30 at 18:16
  • @Anatoly that still doesnt let me add additional context/field to the log, I'd get this {"Time":"2025-03-30T15:31:40.456822-04:00","Action":"run","Package":"/test/temp","Test":"TestTempSuite/Test/TestA_2"}. What i'm looking for is how to add extra fields like "testsuite":"xxxxx", "myTag": "xxxxxxx" (like zap.Field) – hydradon Commented Mar 30 at 19:32
Add a comment  | 

1 Answer 1

Reset to default 0

It should be possible to override the Fail method of testify Suite

type CustomSuite struct {
    suite.Suite
    logger *zap.Logger
}

func (s *CustomSuite) Fail(message string, callerSkip ...int) {
    // Get stack information
    stack := strings.Join(getCallerStack(callerSkip...), "\n")
    
    // Use Zap to log the error
    s.logger.Error("Test assertion failed",
        zap.String("testsuite", "xxxxx"),
        zap.String("myTag", "xxxxxxx"),
        zap.String("message", message),
        zap.String("stack", stack),
    )
    
    // Call the original Fail method
    s.T().Fail(message, callerSkip...)
}

func getCallerStack(skip ...int) []string {
    // Implement the logic to get the call stack
}
发布评论

评论列表(0)

  1. 暂无评论