Skip to content

Conversation

@adombeck
Copy link
Contributor

It's a known issue that using t.FailNow() in deferred functions can result in the error not being printed.

t.FailNow is also called by the require package, so using that in deferred functions, including t.Cleanup functions, can result in the error not being printed.

I can consistently reproduce that issue when the test is run in parallel and also has subtests which are run in parallel. Minimal reproducer:

package main

import (
	"testing"
	"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
	t.Parallel()
	t.Cleanup(func() { require.Fail(t, "This error is never printed") })
	t.Run("subtest", func(t *testing.T) { t.Parallel() })
}

Using non-fatal errors instead fixes the issue.

It's a known issue that using `t.FailNow()` in deferred functions
can result in the error not being printed:

    golang/go#29207

`t.FailNow` is also called by the `require` package, so using that in
deferred functions, including `t.Cleanup` functions, can result in the
error not being printed.

I can consistently reproduce that issue when the test is run in parallel
and also has subtests which are run in parallel. Minimal reproducer:

    package main

    import (
    	"testing"
    	"github.com/stretchr/testify/require"
    )

    func Test(t *testing.T) {
    	t.Parallel()
    	t.Cleanup(func() { require.Fail(t, "This error is never printed") })
    	t.Run("subtest", func(t *testing.T) { t.Parallel() })
    }

Using non-fatal errors instead fixes the issue.
@adombeck adombeck force-pushed the fix-no-error-messages-in-test-cleanup branch from 3aabb97 to a9e242f Compare November 25, 2025 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants