Skip to content

testing: t.FailNow() in a deferred function masks test panic #29207

Open
@matthewmcnew

Description

@matthewmcnew

When a test panics, any deferred functions will still process allowing the test to properly clean up. If a deferred function executes t.FailNow() the test output will not include any information on the panic or the panic's goroutine trace.

An example is shown here: https://play.golang.org/p/gWPDVluKaST
Despite that test clearly panicking with a t.FailNow() in the deferred function the test output only shows:

--- FAIL: TestPanic (0.00s)
FAIL

The desired behavior would be along the lines of this example: https://play.golang.org/p/qrLtmYnkqAh. (Where the deferred function calls runtime.Goexit() instead) This produces the following output:

--- FAIL: TestPanic (0.00s)
panic: Test Panic
	panic: test executed panic(nil) or runtime.Goexit

goroutine 5 [running]:
testing.tRunner.func1(0x4560b0, 0xf)
	/usr/local/go/src/testing/testing.go:792 +0x460
runtime.Goexit()
	/usr/local/go/src/runtime/panic.go:397 +0x140
main.TestPanic.func1()
	/tmp/sandbox284554843/main.go:18 +0x20
panic(0x121940, 0x165a58)
	/usr/local/go/src/runtime/panic.go:513 +0x240
main.TestPanic(0x4560b0, 0xbab699fc)
	/tmp/sandbox284554843/main.go:20 +0x60
testing.tRunner(0x4560b0, 0x1558f8)
	/usr/local/go/src/testing/testing.go:827 +0x140
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:878 +0x3c0

I believe this is the result of this line:

if !t.finished && err == nil {

!t.finished will return false because t.Now() marks the test finished and err will be nil because the runtime.Goexit() in t.FatalNow() is what is being recovered (not the original panic).

This prevents the expected panic on this line:

panic(err)

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions