go 1.9: New Helper() method in testing package does not work with testing.TB interface

606 views
Skip to first unread message

lukas....@googlemail.com

unread,
Aug 25, 2017, 4:35:21 PM8/25/17
to golang-nuts
I'm using helper functions in my test, for example this one:

// assert fails the test if the condition is false.
func
assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
 
if !condition {
 _
, file, line, _ := runtime.Caller(1)
 fmt
.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
 tb
.FailNow()
 
}
}

I hoped that I could improve these helpers with the new (*B).Helper() and (*T).Helper() methods:

// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
tb.Helper()
if !condition {
tb.Fatalf("\033[31m "+msg+"\033[39m\n\n", v...)
}
}

I expected Go to print the file and line number of the failing test, followed by a red message. Instead, the file and line number is of form "<autogenerated>:1:"

It works if I replace tb testing.TB with t testing.T. Is there a reason the new Helper() method is part of the TB interface but not working as expected? Or could it be a bug?

Caleb Spare

unread,
Aug 25, 2017, 4:39:59 PM8/25/17
to lukas....@googlemail.com, golang-nuts
Oh, shoot. It should be part of the interface. That was part of the proposal:


but I forgot to add it.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Caleb Spare

unread,
Aug 25, 2017, 4:44:00 PM8/25/17
to lukas....@googlemail.com, golang-nuts
I filed https://github.com/golang/go/issues/21627. Thanks for the heads up.

lukas....@googlemail.com

unread,
Aug 25, 2017, 4:46:51 PM8/25/17
to golang-nuts
It is part of the interface...

Caleb Spare

unread,
Aug 25, 2017, 4:49:26 PM8/25/17
to lukas....@googlemail.com, golang-nuts
Yeah, sorry, I misread the report.

I can't reproduce the issue. Can you give a complete runnable example.

lukas....@googlemail.com

unread,
Aug 25, 2017, 6:06:30 PM8/25/17
to golang-nuts
package bug

// Pow returns x raised to the power of y i.e. x^y.
func Pow(x, y int) int {
return x ^ y
}

package bug_test

import (
"fmt"
"testing"

)

func TestPow(t *testing.T) {
tests := []struct {
x, y, z int
}{
{0, 0, 0},
{1, 0, 1},
{0, 1, 11},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
z := bug.Pow(tt.x, tt.y)
assert(t, tt.z == z, fmt.Sprintf("exp: %d, got: %d", tt.z, z))
})
}
}

// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
tb.Helper()
if !condition {
tb.Fatalf("\033[31m "+msg+"\033[39m\n\n", v...)
}
}

This will error on test case 3.

I expected this output:

--- FAIL: TestPow (0.00s)

    --- FAIL: TestPow/#02 (0.00s)

    bug_test.go:22 exp: 11, got: 1

    

FAIL

coverage: 100.0% of statements

exit status 1

FAIL github.com/lukasmalkmus/bug 0.008s


I got this:

--- FAIL: TestPow (0.00s)

    --- FAIL: TestPow/#02 (0.00s)

        <autogenerated>:1:  exp: 11, got: 1

    

FAIL

coverage: 100.0% of statements

FAIL    github.com/lukasmalkmus/bug     0.015s




Am Freitag, 25. August 2017 22:35:21 UTC+2 schrieb lukas....@googlemail.com:

lukas....@googlemail.com

unread,
Aug 25, 2017, 6:08:06 PM8/25/17
to golang-nuts
I'm sorry, I think I made a huge mistake. The malicious output only occurs when running go test with the -race flag.


Am Freitag, 25. August 2017 22:35:21 UTC+2 schrieb lukas....@googlemail.com:

Caleb Spare

unread,
Aug 25, 2017, 6:25:22 PM8/25/17
to lukas....@googlemail.com, golang-nuts
That's not a mistake. It's still a bug! Just doesn't seem like a very severe one. I filed https://github.com/golang/go/issues/21631.

Reply all
Reply to author
Forward
0 new messages