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

blinsay opened this issue May 17, 2017 · 3 comments


Copy link

@blinsay blinsay 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 (

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

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


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)?


@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
Copy link

@ianlancetaylor ianlancetaylor commented May 17, 2017

CC @mpvl

Copy link

@gopherbot gopherbot commented May 30, 2017

CL mentions this issue.

Copy link

@mpvl mpvl 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.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants