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

testing: panic in a subtest doesn't execute any deferred funcs outside of the subtest #20394

Closed
blinsay opened this issue May 17, 2017 · 3 comments

Comments

Projects
None yet
4 participants
@blinsay
Copy link

commented May 17, 2017

What did you do?

I've been writing myself some test helpers to set up/tear down some expensive database state and noticed when my tests panicked the tear down wouldn't get run. Eventually, I realized that subtests won't run any defered funcs that were set up before the subtest.

package main

import (
	"fmt"
	"testing"
)

func TestRunPanic(t *testing.T) {
	defer func() {
		fmt.Println("outer deferred")
	}()

	t.Run("subtest", func(t *testing.T) {
		defer func() {
			fmt.Println("inner deferred")
		}()

		panic("boom")
	})
}

I'd expect this example to run both defered funcs, but when running it, this is the output I see.

$ go test -v .
=== RUN   TestRunPanic
=== RUN   TestRunPanic/subtest
inner deferred
panic: boom [recovered]
	panic: boom

This was pretty surprising to me! After looking through the source of t.Run it makes sense, given that the test/panic is being run in a separate goroutine, but the docs for sub-tests and sub-benchmarks don't seem to mention that.

What version of Go are you using (go version)?

go version go1.8.1 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"

@ianlancetaylor ianlancetaylor changed the title panic in a subtest doesn't execute any deferred funcs outside of the subtest testing: panic in a subtest doesn't execute any deferred funcs outside of the subtest May 17, 2017

@ianlancetaylor ianlancetaylor added this to the Go1.10 milestone May 17, 2017

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented May 17, 2017

CC @mpvl

@gopherbot

This comment has been minimized.

Copy link

commented May 30, 2017

CL https://golang.org/cl/44377 mentions this issue.

@mpvl

This comment has been minimized.

Copy link
Member

commented May 30, 2017

Top-level tests are subtests from a hidden main tests and just work exactly the same. But this is not clear from the context. Sent out a CL to clarify.

@gopherbot gopherbot closed this in d10549f May 30, 2017

@golang golang locked and limited conversation to collaborators May 30, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.