Skip to content

Commit

Permalink
Add sub-tests to Suite (#1246)
Browse files Browse the repository at this point in the history
Co-authored-by: Vadym Tishchenko <[email protected]>
  • Loading branch information
qerdcv and Vadym Tishchenko committed Nov 2, 2022
1 parent b747d7c commit 1333b5d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
13 changes: 13 additions & 0 deletions suite/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "testing"
type TestingSuite interface {
T() *testing.T
SetT(*testing.T)
SetS(suite TestingSuite)
}

// SetupAllSuite has a SetupSuite method, which will run before the
Expand Down Expand Up @@ -51,3 +52,15 @@ type AfterTest interface {
type WithStats interface {
HandleStats(suiteName string, stats *SuiteInformation)
}

// SetupSubTest has a SetupSubTest method, which will run before each
// subtest in the suite.
type SetupSubTest interface {
SetupSubTest()
}

// TearDownSubTest has a TearDownSubTest method, which will run after
// each subtest in the suite have been run.
type TearDownSubTest interface {
TearDownSubTest()
}
24 changes: 23 additions & 1 deletion suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ var matchMethod = flag.String("testify.m", "", "regular expression to select tes
// retrieving the current *testing.T context.
type Suite struct {
*assert.Assertions

mu sync.RWMutex
require *require.Assertions
t *testing.T

// Parent suite to have access to the implemented methods of parent struct
s TestingSuite
}

// T retrieves the current *testing.T context.
Expand All @@ -43,6 +47,12 @@ func (suite *Suite) SetT(t *testing.T) {
suite.require = require.New(t)
}

// SetS needs to set the current test suite as parent
// to get access to the parent methods
func (suite *Suite) SetS(s TestingSuite) {
suite.s = s
}

// Require returns a require context for suite.
func (suite *Suite) Require() *require.Assertions {
suite.mu.Lock()
Expand Down Expand Up @@ -85,7 +95,18 @@ 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()
defer suite.SetT(oldT)

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

defer func() {
suite.SetT(oldT)
if tearDownSubTest, ok := suite.s.(TearDownSubTest); ok {
tearDownSubTest.TearDownSubTest()
}
}()

return oldT.Run(name, func(t *testing.T) {
suite.SetT(t)
subtest()
Expand All @@ -98,6 +119,7 @@ func Run(t *testing.T, suite TestingSuite) {
defer recoverAndFailOnPanic(t)

suite.SetT(t)
suite.SetS(suite)

var suiteSetupDone bool

Expand Down
29 changes: 21 additions & 8 deletions suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,16 @@ type SuiteTester struct {
Suite

// Keep counts of how many times each method is run.
SetupSuiteRunCount int
TearDownSuiteRunCount int
SetupTestRunCount int
TearDownTestRunCount int
TestOneRunCount int
TestTwoRunCount int
TestSubtestRunCount int
NonTestMethodRunCount int
SetupSuiteRunCount int
TearDownSuiteRunCount int
SetupTestRunCount int
TearDownTestRunCount int
TestOneRunCount int
TestTwoRunCount int
TestSubtestRunCount int
NonTestMethodRunCount int
SetupSubTestRunCount int
TearDownSubTestRunCount int

SuiteNameBefore []string
TestNameBefore []string
Expand Down Expand Up @@ -255,6 +257,14 @@ func (suite *SuiteTester) TestSubtest() {
}
}

func (suite *SuiteTester) TearDownSubTest() {
suite.TearDownSubTestRunCount++
}

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

type SuiteSkipTester struct {
// Include our basic suite logic.
Suite
Expand Down Expand Up @@ -336,6 +346,9 @@ func TestRunSuite(t *testing.T) {
assert.Equal(t, suiteTester.TestTwoRunCount, 1)
assert.Equal(t, suiteTester.TestSubtestRunCount, 1)

assert.Equal(t, suiteTester.TearDownSubTestRunCount, 2)
assert.Equal(t, suiteTester.SetupSubTestRunCount, 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

1 comment on commit 1333b5d

@Jhhkkvgghkkjchjn
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.