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
Milestone

Comments

@blinsay
Copy link

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 (
	"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
Copy link
Contributor

CC @mpvl

@gopherbot
Copy link

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

@mpvl
Copy link
Contributor

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.

@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.
Projects
None yet
Development

No branches or pull requests

4 participants