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: invalid testing.go:1343: test executed panic(nil) or runtime.Goexit: subtest may have called FailNow on a parent test error #58129

Closed
alexbrainman opened this issue Jan 29, 2023 · 4 comments

Comments

@alexbrainman
Copy link
Member

$ go version

I use play.golang.org website.

Does this issue reproduce with the latest release?

Yes.

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

I use play.golang.org website.

What did you do?

I run this test

https://go.dev/play/p/BtIQcyQJYxR

What did you expect to see?

I expected to see this

=== RUN   TestRunFatal
=== RUN   TestRunFatal/Alex
    prog_test.go:11: Alex
--- FAIL: TestRunFatal (0.00s)
    --- FAIL: TestRunFatal/Alex (0.00s)
FAIL

Program exited.

This output is from slightly modified version of my test

https://go.dev/play/p/QeGIM_xxA4E

What did you see instead?

I see this

=== RUN   TestRunFatal
=== RUN   TestRunFatal/Alex
=== CONT  TestRunFatal
    prog_test.go:11: Alex
=== CONT  TestRunFatal/Alex
    testing.go:1343: test executed panic(nil) or runtime.Goexit: subtest may have called FailNow on a parent test
--- FAIL: TestRunFatal (0.00s)
    --- FAIL: TestRunFatal/Alex (0.00s)
FAIL

Program exited.

Perhaps I don't understand how tests work. But I was surprised and confused by the

    testing.go:1343: test executed panic(nil) or runtime.Goexit: subtest may have called FailNow on a parent test

error message. I also tried reading testing.FailNow documentation at

https://pkg.go.dev/testing#T.FailNow

The documentation talks about goroutines, but I don't have any goroutines in my test.

Thank you for investigating this.

Alex

@qiulaidongfeng
Copy link
Contributor

Document in https://pkg.go.dev/testing#T.Run
Run runs f as a subtest of t called name. It runs f in a separate goroutine and blocks until f returns or calls t.Parallel to become a parallel test.

Document in https://pkg.go.dev/testing#T.Fatal
Fatal is equivalent to Log followed by FailNow.

https://go.dev/play/p/BtIQcyQJYxR A new goroutine is opened in the code t.Run() of. In this goroutine, TestRunFatal's t.Fatal() calls TestRunFatal's t.FailNow(). This is expected behavior.

@alexbrainman
Copy link
Member Author

This is expected behavior.

Maybe you are correct @qiulaidongfeng .

Alex

@mknyszek
Copy link
Contributor

mknyszek commented Jan 30, 2023

That's right, this behavior is expected. The test captures t from the parent test and fails it from the subtest, which isn't allowed (because it's not on that test's goroutine).

@mknyszek
Copy link
Contributor

On the topic of error messages and documentation, I think this could probably be a bit clearer, or have an example. I wonder if any of the go.dev tutorials point this out.

@golang golang locked and limited conversation to collaborators Jan 30, 2024
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