smarty / gunit

xUnit-style test fixture adapter for go test
Other
120 stars 11 forks source link

Not compatible with Testify #19

Closed ghostsquad closed 6 years ago

ghostsquad commented 6 years ago

I'm not sure if this is a problem with Testify or this framework, so I'm starting here.

Expected Behavior

Test passes

Actual Behavior

Runtime Panic

Minimum Repro steps

example_test.go


package foo

import ( "testing" "github.com/smartystreets/gunit" "github.com/stretchr/testify/assert" )

func Test(t *testing.T) { f := new(ExampleFixture) f.t = t gunit.Run(f, t) }

type ExampleFixture struct { gunit.Fixture t testing.T }

func (this *ExampleFixture) TestFoo() { assert.Equal(this.t, 123, 123, "they should be equal") }


Output:

$ go test ./maps/foo/example_test.go --- FAIL: Test (0.00s) --- FAIL: Test/TestFoo (0.00s) test_case.go:67: Test definition:

    fixture.go:97:
        PANIC: runtime error: invalid memory address or nil pointer dereference
        goroutine 21 [running]:
        github.com/smartystreets/gunit.(*Fixture).recoverPanic(0xc420134e80, 0x131ac80, 0x154b470)
            /Users/wes/go/src/github.com/smartystreets/gunit/fixture.go:103 +0xf6
        github.com/smartystreets/gunit.(*Fixture).finalize(0xc420134e80)
            /Users/wes/go/src/github.com/smartystreets/gunit/fixture.go:93 +0x22f
        panic(0x131ac80, 0x154b470)
            /usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229
        testing.(*T).Helper(0x0)
            <autogenerated>:1 +0x5
        github.com/stretchr/testify/assert.Equal(0x13b6bc0, 0x0, 0x12fb5e0, 0x13b4268, 0x12fb5e0, 0x13b4270, 0xc420060b68, 0x1, 0x1, 0xc420060ba8)
            /Users/wes/go/src/github.com/stretchr/testify/assert/assertions.go:328 +0x480
        command-line-arguments.(*ExampleFixture).TestFoo(0xc4200b3260)
            /Users/wes/projects/space-z/maps/foo/example_test.go:21 +0xa4
        reflect.Value.call(0x1364e00, 0xc4200b3260, 0x3613, 0x1379ce4, 0x4, 0x0, 0x0, 0x0, 0xf, 0x114d20e, ...)
            /usr/local/Cellar/go/1.10/libexec/src/reflect/value.go:447 +0x969
        reflect.Value.Call(0x1364e00, 0xc4200b3260, 0x3613, 0x0, 0x0, 0x0, 0x3613, 0xc420134e80, 0xc420060f40)
            /usr/local/Cellar/go/1.10/libexec/src/reflect/value.go:308 +0xa4
        github.com/smartystreets/gunit.(*testCase).runTest(0xc42014a9a0)
            /Users/wes/go/src/github.com/smartystreets/gunit/test_case.go:86 +0x7c
        github.com/smartystreets/gunit.(*testCase).runWithSetupAndTeardown(0xc42014a9a0)
            /Users/wes/go/src/github.com/smartystreets/gunit/test_case.go:76 +0x5f
        github.com/smartystreets/gunit.(*testCase).run(0xc42014a9a0, 0xc4201561e0)
            /Users/wes/go/src/github.com/smartystreets/gunit/test_case.go:64 +0x79
        github.com/smartystreets/gunit.(*testCase).(github.com/smartystreets/gunit.run)-fm(0xc4201561e0)
            /Users/wes/go/src/github.com/smartystreets/gunit/test_case.go:48 +0x34
        testing.tRunner(0xc4201561e0, 0xc4200b3210)
            /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:777 +0xd0
        created by testing.(*T).Run
            /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:824 +0x2e0

FAIL FAIL command-line-arguments 0.010s

mdwhatcott commented 6 years ago

The reason you are getting a panic is because gunit is designed to instantiate fresh instances of ExampleFixture and it takes care of embedding the *testing.T in the *gunit.Fixture behind the scenes. Setting it manually before calling gunit.Run is a no-op.

gunit was designed to work specifically with our own assertions library. We don't have any plans to add support for testify or any other testing libraries/frameworks.

I'll rewrite your test example using the approved method:

package foo

import (
    "testing"
    "github.com/smartystreets/assertions/should"
    "github.com/smartystreets/gunit"
)

func Test(t *testing.T) {
    gunit.Run(new(ExampleFixture), t)
}

type ExampleFixture struct {
    *gunit.Fixture
}

func (this *ExampleFixture) TestFoo() {
    // option 1, use the Assert function, which allows a custom message in case of failure:
    this.Assert(123 == 123, "they should be equal")

    // option 2, use our assertions library and print your own message is case of failure:
    if !this.So(123, should.Equal, 123) {
        this.Println("They should be equal!")
    }
}

Would that satisfy your needs?

ghostsquad commented 6 years ago

Well I started using the smartystreets/assertions library, but I wanted to play with other assertion libraries. So, technically no this doesn't solve the problem. But you answered the question of why they aren't compatible. Thanks!