stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
23.15k stars 1.59k forks source link

Panic comparing two ints #960

Open wonboyn opened 4 years ago

wonboyn commented 4 years ago

Current behaviour:

With go 1.14.2 darwin/amd64 and testify 1.6.0 if I assert.Equal two ints, it causes testify to panic:

` panic: interface conversion: interface {} is int, not string [recovered] panic: interface conversion: interface {} is int, not string

goroutine 31 [running]: testing.tRunner.func1.1(0x1476ea0, 0xc000593b30) /usr/local/Cellar/go/1.14.2_1/libexec/src/testing/testing.go:940 +0x2f5 testing.tRunner.func1(0xc000234c60) /usr/local/Cellar/go/1.14.2_1/libexec/src/testing/testing.go:943 +0x3f9 panic(0x1476ea0, 0xc000593b30) /usr/local/Cellar/go/1.14.2_1/libexec/src/runtime/panic.go:969 +0x166 github.com/stretchr/testify/assert.messageFromMsgAndArgs(0xc0007d5e70, 0x2, 0x2, 0x2, 0x3) /Users/tim/go/pkg/mod/github.com/stretchr/testify@v1.6.0/assert/assertions.go:191 +0x184 github.com/stretchr/testify/assert.Fail(0x169eb20, 0xc000234c60, 0xc0007c1b00, 0x47b, 0xc0007d5e70, 0x2, 0x2, 0x6) /Users/tim/go/pkg/mod/github.com/stretchr/testify@v1.6.0/assert/assertions.go:257 +0x1a2 github.com/stretchr/testify/assert.Equal(0x169eb20, 0xc000234c60, 0x14d8320, 0xc000234c60, 0x1456960, 0x1992400, 0xc0007d5e70, 0x2, 0x2, 0xc00048ed60) /Users/tim/go/pkg/mod/github.com/stretchr/testify@v1.6.0/assert/assertions.go:346 +0x3fc github.com/stretchr/testify/assert.(*Assertions).Equal(0xc0007d5e60, 0x14d8320, 0xc000234c60, 0x1456960, 0x1992400, 0xc0007d5e70, 0x2, 0x2, 0xc000700001) /Users/tim/go/pkg/mod/github.com/stretchr/testify@v1.6.0/assert/assertion_forward.go:132 +0xc8 `

Here's the assertion:

assert := assert.New(t)
var tests = []struct {
    name     string
    region   string
    expected int
    flag     string
}{
    {"Valid region.", "EMEA", 0, "false"},
    {"Invalid region", "Fredville", 0, "true"},
}

// Iterate through the test data
for _, test := range tests {

    // Run each test
    result := GetCustomerByRegion(test.region)
    resultCount := len(result)
    if test.flag == "true" {
        assert.Equal(t, resultCount, test.expected, test.name)
    } else {
        assert.NotEqual(t, resultCount, test.expected, test.name)
    }
}

Expected behaviour: testify should determine if the two ints are equal, or fail gracefully.

boyan-soubachov commented 4 years ago

Do you mind showing the GetCustomerByRegion function and what exact variable its returning?

wonboyn commented 4 years ago

Hi there. Unfortunately, I'm not able to share that code. The function is returning a string

boyan-soubachov commented 4 years ago

Are you able to reproduce the same error but with non-proprietary code? If you gave a complete example of just any code that causes the same error, it would be much easier to diagnose and debug.

wonboyn commented 4 years ago

Sure - I'll try & do that & get back to you.

One other bit of info I just found is that the error does not happen in I debug the test call, but does happen when I run go test -v

nbaztec commented 4 years ago

I think the issue is in the code:

assert := assert.New(t)
...
assert.Equal(t, resultCount, test.expected, test.name)

assert := assert.New(t) creates a new assertion object that wraps the *testing.T instance. The object includes the original *testing.T object and will forward the instance to the underlying assert call.

The correct way to use it would be to omit the *testing.T object:

assert := assert.New(t)
assert.Equal(resultCount, test.expected, test.name)

or use the global assert package:

assert.Equal(t, resultCount, test.expected, test.name)