-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
suite.Run: validate Test* methods signature #1512
base: master
Are you sure you want to change the base?
Conversation
@@ -184,6 +184,11 @@ func Run(t *testing.T, suite TestingSuite) { | |||
failOnPanic(t, r) | |||
}() | |||
|
|||
if method.Type.NumIn() > 1 || method.Type.NumOut() > 0 { | |||
msg := fmt.Sprintf("testify: suite method '%s' shouldn't have any arguments or returning values\n", method.Name) | |||
panic(msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about using panic here tho, seems appropriate to me, but i will appreciate if you take a look @dolmen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it better to use t.Error(msg)? Then you can directly see the TestMethod violating the contract. I find panic always quite hard to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wvell the motivation to add panic here instead of t.Error
is the source of this condition. I.e. It is not the tested code returning unexpected result, but rather the programmer who messed up the signature of the Test* method itself. In that case panic is more fitting IMHO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah fair point, I just hate reading panics.
Thanks for putting in the work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I'm a strong advocate of panic
to catch programmer's mistake, I think a panic
is not appropriate in this case as the stack trace will not be useful to the user.
Instead Run
is being called with *testing.T
, so let's just use t.Fatal
(or t.Fatalf
) to report the issue.
However, the main problem with this implementation is that this check must be done much much earlier, line 151, just after the successful call to methodFilter
.
@@ -184,6 +184,11 @@ func Run(t *testing.T, suite TestingSuite) { | |||
failOnPanic(t, r) | |||
}() | |||
|
|||
if method.Type.NumIn() > 1 || method.Type.NumOut() > 0 { | |||
msg := fmt.Sprintf("testify: suite method '%s' shouldn't have any arguments or returning values\n", method.Name) | |||
panic(msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I'm a strong advocate of panic
to catch programmer's mistake, I think a panic
is not appropriate in this case as the stack trace will not be useful to the user.
Instead Run
is being called with *testing.T
, so let's just use t.Fatal
(or t.Fatalf
) to report the issue.
However, the main problem with this implementation is that this check must be done much much earlier, line 151, just after the successful call to methodFilter
.
Summary
Added panic in suite.Run if Test* method has args or returning values
P.S. This is my first ever opensource contribution, so I'm very open to any code logic/styling criticism!
Changes
testify: suite method <MethodName> shouldn't have any arguments or returning values
Motivation
As @wvell mentioned in issue #1508 currently,
suite.Run
returns an ambiguous error when executing with instance of Suite that has incorrectTest*
methods signatures.Related issues
Closes #1508