golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.9k stars 17.65k forks source link

testing: failures in Cleanup functions do not trigger -failfast if the test includes a parallel subtest #61034

Open bcmills opened 1 year ago

bcmills commented 1 year ago

When run with -failfast, the following test should run only Test1, which fails. Instead, it reports the failure from Test1 and then proceeds to run Test2.

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

package main

import "testing"

func Test1(t *testing.T) {
    t.Cleanup(func() {
        t.Errorf("fail in Cleanup")
    })

    t.Run("sub", func(t *testing.T) {
        t.Parallel()
    })
}

func Test2(t *testing.T) {
    t.Fatalf("This test should not run with -failfast.")
}

Without the call to t.Parallel(), it skips Test2 as expected.

This is because the numFailed variable used for -failfast is incremented before cleanup, only for tests that have parallel subtests.

I do not see a compelling reason for the difference in codepaths between tests with parallel subtests and those without. But I also don't see why we need to keep track of the number of failed tests explicitly — it seems like we could instead just set a boolean when any test fails.

gopherbot commented 1 year ago

Change https://go.dev/cl/506755 mentions this issue: testing: simplify concurrency and cleanup

bcmills commented 11 months ago

Reopening because the fix was reverted (#64402).

ansel1 commented 10 months ago

The CL originally submitted for this (which was later reverted) also fixes #49929