Skip to content

Commit

Permalink
[checkpoint] implement suggestion from stretchr#1363
Browse files Browse the repository at this point in the history
  • Loading branch information
mach6 committed Mar 28, 2023
1 parent c5fc9d6 commit 893f9cf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions suite/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ type SetupSubTest interface {
type TearDownSubTest interface {
TearDownSubTest()
}

// BeforeSubTest has a function to be executed right before the subtest
// starts and receives the suite, test and subtest names as input
type BeforeSubTest interface {
BeforeSubTest(suiteName, testName, subTestName string)
}

// AfterSubTest has a function to be executed right after the subtest
// finishes and receives the suite, test and subtest names as input
type AfterSubTest interface {
AfterSubTest(suiteName, testName, subTestName string)
}
16 changes: 16 additions & 0 deletions suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"regexp"
"runtime/debug"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -95,13 +96,28 @@ func failOnPanic(t *testing.T, r interface{}) {
// Provides compatibility with go test pkg -run TestSuite/TestName/SubTestName.
func (suite *Suite) Run(name string, subtest func()) bool {
oldT := suite.T()
var (
suiteName string
parentName string
)
n := strings.Split(suite.s.T().Name(), "/")
if len(n) > 1 {
suiteName, parentName = n[0], strings.Join(n[1:len(n)], "/")
}

if setupSubTest, ok := suite.s.(SetupSubTest); ok {
setupSubTest.SetupSubTest()
}

if beforeSubTest, ok := suite.s.(BeforeSubTest); ok {
beforeSubTest.BeforeSubTest(suiteName, parentName, name)
}

defer func() {
suite.SetT(oldT)
if afterSubTest, ok := suite.s.(AfterSubTest); ok {
afterSubTest.AfterSubTest(suiteName, parentName, name)
}
if tearDownSubTest, ok := suite.s.(TearDownSubTest); ok {
tearDownSubTest.TearDownSubTest()
}
Expand Down
13 changes: 13 additions & 0 deletions suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ type SuiteTester struct {
NonTestMethodRunCount int
SetupSubTestRunCount int
TearDownSubTestRunCount int
BeforeSubTestRunCount int
AfterSubTestRunCount int

SuiteNameBefore []string
TestNameBefore []string
Expand Down Expand Up @@ -261,10 +263,18 @@ func (suite *SuiteTester) TearDownSubTest() {
suite.TearDownSubTestRunCount++
}

func (suite *SuiteTester) AfterSubTest(suiteName, testName, subtestName string) {
suite.AfterSubTestRunCount++
}

func (suite *SuiteTester) SetupSubTest() {
suite.SetupSubTestRunCount++
}

func (suite *SuiteTester) BeforeSubTest(suiteName, testName, subtestName string) {
suite.BeforeSubTestRunCount++
}

type SuiteSkipTester struct {
// Include our basic suite logic.
Suite
Expand Down Expand Up @@ -349,6 +359,9 @@ func TestRunSuite(t *testing.T) {
assert.Equal(t, suiteTester.TearDownSubTestRunCount, 2)
assert.Equal(t, suiteTester.SetupSubTestRunCount, 2)

assert.Equal(t, suiteTester.AfterSubTestRunCount, 2)
assert.Equal(t, suiteTester.BeforeSubTestRunCount, 2)

// Methods that don't match the test method identifier shouldn't
// have been run at all.
assert.Equal(t, suiteTester.NonTestMethodRunCount, 0)
Expand Down

0 comments on commit 893f9cf

Please sign in to comment.