Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate suite method signatures and continue execution for valid tests #1665

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ 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 {
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
}

if !suiteSetupDone {
if stats != nil {
stats.Start = time.Now()
Expand Down
56 changes: 56 additions & 0 deletions suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.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")
}
Loading