Skip to content

suite: skip AfterTest when SetupTest or BeforeTest skips (#1781)#1873

Open
barry3406 wants to merge 1 commit intostretchr:masterfrom
barry3406:fix/skip-before-test-aftertest
Open

suite: skip AfterTest when SetupTest or BeforeTest skips (#1781)#1873
barry3406 wants to merge 1 commit intostretchr:masterfrom
barry3406:fix/skip-before-test-aftertest

Conversation

@barry3406
Copy link
Copy Markdown

Summary

Skip the AfterTest hook when a SetupTest or BeforeTest call caused the test to be skipped, so AfterTest always has a matching BeforeTest.

Changes

  • suite/suite.go: track whether BeforeTest finished via a local beforeTestFinished flag; only invoke AfterTest in the deferred cleanup when that flag is set.
  • suite/suite_test.go: add tests covering skip-in-BeforeTest, skip-in-SetupTest, and skip-in-test-body (regression guard).

TearDownTest is intentionally left unchanged: it still runs in all cases so users can clean up partially initialized state set up before the skip.

Motivation

Per #1781, calling t.Skip() from BeforeTest correctly prevents the test body from running, but AfterTest is still invoked — leaving AfterTest without the paired BeforeTest it expects. Scenario 3 in the reporter's example causes a panic("after") today even though the test should be skipped cleanly. SetupTest has the same inconsistency (scenario 2). Gating AfterTest on BeforeTest having finished resolves both without changing the test-body-skip path.

Related issues

Closes #1781

If SetupTest or BeforeTest calls t.Skip(), the deferred cleanup still
invoked AfterTest even though the paired BeforeTest never completed.
Track whether BeforeTest finished and only run AfterTest when it did,
so AfterTest always has a matching BeforeTest.

TearDownTest still runs in all cases to preserve the existing contract
that teardown can clean up partially initialized state.

Fixes stretchr#1781
@dolmen dolmen added the pkg-suite Change related to package testify/suite label Apr 22, 2026
Comment thread suite/suite.go
// AfterTest has a matching BeforeTest.
if beforeTestFinished {
afterTestSuite.AfterTest(suiteName, method.Name)
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the if beforeTestFinished around the if suite.(AfterTest) block: I prefer to keep the cast really close to the method call.

Comment thread suite/suite_test.go

// skipInBeforeTestSuite is used to verify that AfterTest is not called when
// BeforeTest skips the test (#1781).
type skipInBeforeTestSuite struct {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap SuiteTester that already handles counting.
You'll have to override only the method where Skip() happens.

With this the case will be much shorter and so clearer.

type skipInBeforeTestSuite struct {
	SuiteTester
}

func (s *skipInBeforeTestSuite) BeforeTest(suiteName, testName string) {
	s.SuiteTester.BeforeTest(suiteName, testName)
	if testName == "TestA" {
		s.T().Skip()
	}
}

Also add a TestB and check that its Before/After are correctly called

Comment thread suite/suite_test.go
// skipInSetupTestSuite is used to verify that AfterTest is not called when
// SetupTest skips the test (#1781).
type skipInSetupTestSuite struct {
Suite
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap SuiteTester also here.

Comment thread suite/suite_test.go
// skipInTestBodySuite is used to verify that AfterTest is still called when
// the test body itself calls Skip (regression guard for existing behavior).
type skipInTestBodySuite struct {
Suite
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap SuiteTester also here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg-suite Change related to package testify/suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

suite: Skipping a test in BeforeTest prevents test execution but not AfterTest

2 participants