Skip to content

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

Closed

Description

@alexbrainman

$ 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

Activity

  1. qiulaidongfeng commented on Jan 29, 2023

    @qiulaidongfeng
    Member

    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.

  2. alexbrainman commented on Jan 29, 2023

    @alexbrainman
    MemberAuthor

    This is expected behavior.

    Maybe you are correct @qiulaidongfeng .

    Alex

  3. mknyszek commented on Jan 30, 2023

    @mknyszek
    Contributor

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

  4. mknyszek commented on Jan 30, 2023

    @mknyszek
    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.

  5. locked and limited conversation to collaborators on Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions