From d8f2343cd54e86f09bb917384ddd9950b1bed16d Mon Sep 17 00:00:00 2001 From: vyas-git Date: Tue, 22 Oct 2024 21:33:03 +0530 Subject: [PATCH 1/2] Validate suite method signatures and continue execution for valid tests --- suite/suite.go | 7 ++++++ suite/suite_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/suite/suite.go b/suite/suite.go index 18443a91c..798a3559b 100644 --- a/suite/suite.go +++ b/suite/suite.go @@ -148,6 +148,13 @@ func Run(t *testing.T, suite TestingSuite) { continue } + // Checking method signature, should not contain arguments and return values + if method.Type.NumIn() > 1 || method.Type.NumOut() > 0 { + t.Errorf("testify: suite method '%s' has %d input arguments and %d return values. It should have none.", + method.Name, method.Type.NumIn()-1, method.Type.NumOut()) + continue + } + if !suiteSetupDone { if stats != nil { stats.Start = time.Now() diff --git a/suite/suite_test.go b/suite/suite_test.go index b97eb4311..97920f57e 100644 --- a/suite/suite_test.go +++ b/suite/suite_test.go @@ -746,3 +746,59 @@ func TestUnInitializedSuites(t *testing.T) { }) }) } + +// This suite tests both valid and invalid method signatures. +type SuiteSignatureValidationTester struct { + Suite + + executedTestCount int + setUp bool + toreDown bool +} + +// SetupSuite is run once before any tests. +func (s *SuiteSignatureValidationTester) SetupSuite() { + s.setUp = true +} + +func (s *SuiteSignatureValidationTester) TestValidSignature() { + s.executedTestCount++ +} + +func (s *SuiteSignatureValidationTester) TestInvalidSignatureReturnValue() interface{} { + s.executedTestCount++ + return nil +} + +func (s *SuiteSignatureValidationTester) TestInvalidSignatureArg(somearg string) { + s.executedTestCount++ +} + +func (s *SuiteSignatureValidationTester) TestInvalidSignatureBoth(somearg string) interface{} { + s.executedTestCount++ + return nil +} + +func (s *SuiteSignatureValidationTester) TearDownSuite() { + s.toreDown = true +} + +func TestSuiteSignatureValidation(t *testing.T) { + suiteTester := new(SuiteSignatureValidationTester) + + // Running tests with internal testing mechanism + ok := testing.RunTests(allTestsFilter, []testing.InternalTest{ + { + Name: "signature validation", + F: func(t *testing.T) { + Run(t, suiteTester) + }, + }, + }) + + // Ensure that the test suite did not pass due to invalid signatures + require.False(t, ok) + assert.Equal(t, 1, suiteTester.executedTestCount, "Only the valid test method should be executed") + assert.True(t, suiteTester.setUp, "SetupSuite should have been executed") + assert.True(t, suiteTester.toreDown, "TearDownSuite should have been executed") +} From d07691358ac710802b3bf46888bfc9895d64d19a Mon Sep 17 00:00:00 2001 From: vyas-git Date: Fri, 1 Nov 2024 22:54:01 +0100 Subject: [PATCH 2/2] Log warning for invalid method signatures and skip execution --- suite/suite.go | 3 +-- suite/suite_test.go | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/suite/suite.go b/suite/suite.go index 798a3559b..626ff5ae4 100644 --- a/suite/suite.go +++ b/suite/suite.go @@ -150,8 +150,7 @@ func Run(t *testing.T, suite TestingSuite) { // Checking method signature, should not contain arguments and return values if method.Type.NumIn() > 1 || method.Type.NumOut() > 0 { - t.Errorf("testify: suite method '%s' has %d input arguments and %d return values. It should have none.", - method.Name, method.Type.NumIn()-1, method.Type.NumOut()) + fmt.Printf("Warning: In suite %s, method '%s' has an invalid signature and will be skipped. It has %d input arguments and %d return values; it should have none.\n", suiteName, method.Name, method.Type.NumIn()-1, method.Type.NumOut()) continue } diff --git a/suite/suite_test.go b/suite/suite_test.go index 97920f57e..b7a236e71 100644 --- a/suite/suite_test.go +++ b/suite/suite_test.go @@ -797,7 +797,7 @@ func TestSuiteSignatureValidation(t *testing.T) { }) // Ensure that the test suite did not pass due to invalid signatures - require.False(t, ok) + require.True(t, ok) assert.Equal(t, 1, suiteTester.executedTestCount, "Only the valid test method should be executed") assert.True(t, suiteTester.setUp, "SetupSuite should have been executed") assert.True(t, suiteTester.toreDown, "TearDownSuite should have been executed")