29.2 F
Columbus
Sunday, January 24, 2021

go test -v streaming output

This post was originally published on this site

The testing package is one of my favourite packages in the Go standard library, not just because of its low noise approach to unit testing, but, over the lifetime of Go, it has received a steady stream of quality of life improvements driven by real world usage.

The most recent example of this is, in Go 1.14, go test -v will stream t.Log output as it happens, rather than hoarding it til the end of the test run. Here’s an example;

package main

import (
	"fmt"
	"testing"
	"time"
)

func TestLogStreaming(t *testing.T) {
	for i := 0; i < 5; i++ {
		time.Sleep(300 * time.Millisecond)
		fmt.Println("fmt.Println:", i)
		t.Log("t.Log:", i)
	}
}

Note: Calling fmt.Println inside a test is generally considered a no no as it bypasses the testing package’s output buffering irrespective of the -v flag. However, for this example, it‘s necessary to demonstrate the streaming t.Log change.

% go1.13 test -v tlog_test.go
=== RUN   TestLogStreaming
fmt.Println: 0
fmt.Println: 1
fmt.Println: 2
fmt.Println: 3
fmt.Println: 4
--- PASS: TestLogStreaming (1.52s)
    tlog_test.go:13: t.Log: 0
    tlog_test.go:13: t.Log: 1
    tlog_test.go:13: t.Log: 2
    tlog_test.go:13: t.Log: 3
    tlog_test.go:13: t.Log: 4
PASS
ok      command-line-arguments  1.971s

Under Go 1.13 and earlier the fmt.Println lines output immediately. t.Log lines are buffered and are printed after the test completes.

% go1.14 test -v tlog_test.go
=== RUN   TestLogStreaming
fmt.Println: 0
    TestLogStreaming: tlog_test.go:13: t.Log: 0
fmt.Println: 1
    TestLogStreaming: tlog_test.go:13: t.Log: 1
fmt.Println: 2
    TestLogStreaming: tlog_test.go:13: t.Log: 2
fmt.Println: 3
    TestLogStreaming: tlog_test.go:13: t.Log: 3
fmt.Println: 4
    TestLogStreaming: tlog_test.go:13: t.Log: 4
--- PASS: TestLogStreaming (1.51s)
PASS
ok      command-line-arguments  1.809s

Under Go 1.14 the fmt.Println and t.Log lines are interleaved, rather than waiting for the test to complete, demonstrating that test output is streamed when go test -v is used.

This is a great quality of life improvement for integration style tests that often retry for long periods when the test is failing. Streaming t.Log output will help Gophers debug those test failures without having to wait until the entire test times out to receive their output.

Related Articles

Live Stream: Using Maven in IntelliJ IDEA

Register now and join us for the live stream of ‘Using Maven in IntelliJ IDEA’. In this live stream, Java Champion and Java User Group...

Webinar Recording: What’s New in TeamCity 2020.2

The recording of our January 19 webinar, “What’s New in TeamCity 2020.2”, is now available. In this webinar, Marco Behler, the Developer Advocate in...

Webinar – OOPs, I did it again

Join us Thursday, February 11, 2021, 16:00 – 17:00 CET (10:00 AM – 11:00 AM EST or check other timezones) for our free live webinar, OOPs, I did...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

21,412FansLike
0FollowersFollow
0SubscribersSubscribe
- Advertisement -

Latest Articles

Live Stream: Using Maven in IntelliJ IDEA

Register now and join us for the live stream of ‘Using Maven in IntelliJ IDEA’. In this live stream, Java Champion and Java User Group...

Webinar Recording: What’s New in TeamCity 2020.2

The recording of our January 19 webinar, “What’s New in TeamCity 2020.2”, is now available. In this webinar, Marco Behler, the Developer Advocate in...

Webinar – OOPs, I did it again

Join us Thursday, February 11, 2021, 16:00 – 17:00 CET (10:00 AM – 11:00 AM EST or check other timezones) for our free live webinar, OOPs, I did...

CUE: Configuration superpowers for everyone

On this episode we learn how to Configure, Unify, and Execute things. What’s CUE all about? Well, it’s an open source language with a...