Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testutil suite run silently aborts on panics in OptBefore hooks #244

Closed
softwarebygabe opened this issue Oct 7, 2021 · 1 comment
Closed

Comments

@softwarebygabe
Copy link

What happened:
A panic in an OptBefore handler in a suite happened at runtime and the test run was aborted but reported as successful.

What you expected to happen:
The test run to be aborted but be reported as failed in the before hook.

How to reproduce it (as minimally and precisely as possible):
consider this illustrative example:

swallow.go

package swallow

func Add(a, b int) int {
	return a + b
}

With no panic in the before hook
swallow_test.go

func TestMain(m *testing.M) {
	suite := testutil.New(m,
		testutil.OptLog(logger.All()),
		testutil.OptBefore(func(c context.Context) error {
			return nil
		}))
	suite.Run()
}
func Test_Add_ShouldPass(t *testing.T) {
	result := swallow.Add(2, 2)
	if result != 4 {
		t.Fail()
	}
}

func Test_Add_ShouldFail(t *testing.T) {
	result := swallow.Add(2, 2)
	if result == 4 {
		t.Fail()
	}
}
 ~/workspace/go-test  go test -v ./pkg/swallow                                                                                   03:55:56 PM 
=== RUN   Test_Add_ShouldPass
--- PASS: Test_Add_ShouldPass (0.00s)
=== RUN   Test_Add_ShouldFail
--- FAIL: Test_Add_ShouldFail (0.00s)
FAIL
FAIL    github.com/softwarebygabe/go-test/pkg/swallow   0.126s
FAIL

Now with a panic
swallow_test.go

package swallow_test

import (
	"context"
	"testing"

	"github.com/blend/go-sdk/logger"
	"github.com/blend/go-sdk/testutil"
	"github.com/softwarebygabe/go-test/pkg/swallow"
)

func TestMain(m *testing.M) {
	suite := testutil.New(m,
		testutil.OptLog(logger.All()),
		testutil.OptBefore(func(c context.Context) error {
			panic("ahh")
			// return nil
		}))
	suite.Run()
}
func Test_Add_ShouldPass(t *testing.T) {
	result := swallow.Add(2, 2)
	if result != 4 {
		t.Fail()
	}
}

func Test_Add_ShouldFail(t *testing.T) {
	result := swallow.Add(2, 2)
	if result == 4 {
		t.Fail()
	}
}
 ~/workspace/go-test  go test -v ./pkg/swallow                                                                                   03:56:03 PM 
ok      github.com/softwarebygabe/go-test/pkg/swallow   0.127s
 ~/workspace/go-test  go test ./pkg/swallow                                                                                      03:58:12 PM 
ok      github.com/softwarebygabe/go-test/pkg/swallow   0.124s
 ~/workspace/go-test   

The only indication that the tests were not run is the lack of any tests being listed with the -v flag.

Anything else we need to know?:

Environment:

  • go-sdk version: v1.20210826.18
  • go version (e.g go version): go version go1.15.6 darwin/amd64
  • OS (e.g: cat /etc/os-release): macOS 11.6
  • Others:
@softwarebygabe
Copy link
Author

Also hey 👋 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant