Skip to content

proposal: testing: add -vf (verbose when fail) flag to go test command #59201

@vladopajic

Description

@vladopajic

Problem

For large golang codebase running test can produce overwhelming test results. Developers may not care about output of all tests unless test has failed.

Solution

With new option for go test command, flag -vf (verbose when fail), developer would be able to run tests, but with verbose output of tests only if it has failed. This would reduce total test result output only to bare minimum.

Open questions

  1. For test functions which want to produce additional output it would be helpful to expose io.Writer in testing.T struct which could be used to collect all outputs of tests in order to conditionally print entire test outputs.
// Consider having this test case in which
// output will be always written to stdout regardless 
// if test has failed or not.
func TestFoo(t *testing.T) {
    fmt.Printf("running test")
    
    ...
}

// In order to control outputs of tests, they would have to use 
// exposed `t.Writer` which would collect outputs of tests. 
// `go test` tool would then be able to decided weather data collected by
// this `t.Writer` would need to be outputted to stdout.
func TestFoo(t *testing.T) {
    fmt.Fprintf(t.Writer, "running test")

    ...
}

// Since most codebases already use loggers, it would be quite trivial to
// create logger with `t.Writer` and pass it where necessary.
func TestFoo(t *testing.T) {
    log := logger.New().WithWriter(t.Writer)
    log.Debug("running test")

    ...
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions