smarty / gunit

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

Panic on assertion failure #30

Closed agosto-calvinbehling closed 4 years ago

agosto-calvinbehling commented 4 years ago

Go version go version go1.11.6 linux/amd64

What I did Created a test which fails

What I expected A relevant error message and a more concise stack trace.

What happened panic. index out of range is not relevant to the assertion.

Example code

package function

import (
    "testing"

    "github.com/smartystreets/assertions/should"
    "github.com/smartystreets/gunit"
)

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

type ExampleFixture struct {
    *gunit.Fixture // Required: Embedding this type is what makes the magic happen.
}

func (this *ExampleFixture) TestWithAssertions() {
    this.So(1, should.Equal, 2)
}

Modules

module gunit_runtime_error

require (
    github.com/smartystreets/assertions v1.0.1
    github.com/smartystreets/gunit v1.3.1
)

Output

--- FAIL: TestExampleFixture (0.00s)
    --- FAIL: TestExampleFixture/TestWithAssertions (0.00s)
        test_case.go:67: Test definition:
            /gunit_runtime_error/gunit_test.go:18
        fixture.go:102: 
            PANIC: runtime error: index out of range
            ...
            github.com/smartystreets/gunit/reports.(*failureReport).extractLineOfCode(0xc000083a00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1)
                /gopath/pkg/mod/github.com/smartystreets/gunit@v1.3.1/reports/failure_report.go:63 +0xc1
            github.com/smartystreets/gunit/reports.(*failureReport).scanStack(0xc000083a00, 0xc0000f2000, 0x1f, 0x20)
                /gopath/pkg/mod/github.com/smartystreets/gunit@v1.3.1/reports/failure_report.go:45 +0x1e9
            github.com/smartystreets/gunit/reports.FailureReport(0xc0000942d0, 0x2d, 0xc0000f2000, 0x1f, 0x20, 0x57e880, 0x5e9f10)
                /gopath/pkg/mod/github.com/smartystreets/gunit@v1.3.1/reports/failure_report.go:15 +0xf7
            github.com/smartystreets/gunit.(*Fixture).fail(0xc0000a0a80, 0xc0000942d0, 0x2d)
                /gopath/pkg/mod/github.com/smartystreets/gunit@v1.3.1/fixture.go:93 +0x71
            github.com/smartystreets/gunit.(*Fixture).So(0xc0000a0a80, 0x57e880, 0x5e9f10, 0x5cd0b0, 0xc00008aa50, 0x1, 0x1, 0x57f300)
                /gopath/pkg/mod/github.com/smartystreets/gunit@v1.3.1/fixture.go:49 +0x9d
            gunit_runtime_error.(*ExampleFixture).TestWithAssertions(0xc00009c268)
                /gunit_runtime_error/gunit_test.go:19 +0x99

FAIL
FAIL    gunit_runtime_error 0.002s
mdwhatcott commented 4 years ago

@agosto-calvinbehling - Apparently, runtime.CallerFrames returned errant blank entries in Go 1.11. This isn't a problem from Go 1.12 and higher:

$ go version && go test -v
go version go1.13.9 darwin/amd64
=== RUN   TestExampleFixture
=== PAUSE TestExampleFixture
=== CONT  TestExampleFixture
=== RUN   TestExampleFixture/TestWithAssertions
=== PAUSE TestExampleFixture/TestWithAssertions
=== CONT  TestExampleFixture/TestWithAssertions
    TestExampleFixture/TestWithAssertions: test_case.go:67: Test definition:
        /Users/mike/Desktop/gunit_runtime_error/example_test.go:18
    TestExampleFixture/TestWithAssertions: fixture.go:102: 
        (0): this.So(1, should.Equal, 2) // example_test.go:19
        Expected: '2'
        Actual:   '1'
        (Should be equal)

--- FAIL: TestExampleFixture (0.00s)
    --- FAIL: TestExampleFixture/TestWithAssertions (0.00s)
FAIL
exit status 1
FAIL    gunit_runtime_error 0.293s

I've supplied a fix for this (version v1.1.4), but in the future, please refer to the Go module files of your dependencies to see if your version of go is supported. This project defines Go 1.13 as the minimum supported version of go.

agosto-calvinbehling commented 4 years ago

Huh. Kind of assumed go modules would tell me about that. Good to know. Thanks.

mdwhatcott commented 4 years ago

Yeah, it only warns on compilation failures.