Replace testify assertions with custom testing helpers#159
Replace testify assertions with custom testing helpers#159hanzei merged 2 commits intostretchr:masterfrom
testify assertions with custom testing helpers#159Conversation
|
@ccoVeille wdyt ? |
std_assert_test.go
Outdated
| // Allow formatting helpers similar to fmt in tests using fmt.Sprintf | ||
| var _ = fmt.Sprintf | ||
| var _ = runtime.NumCPU |
There was a problem hiding this comment.
I don't see a need for this.
I feel like you are forcing import. So symbols are available.
Why that ?
I'm not saying you are wrong. I'm saying I'm suspicious
| var assert nonfatal | ||
| var require fatal |
There was a problem hiding this comment.
This idea is awesome !
I was like: WTF why removing testify imports everywhere. It won't work!
You got me, and I love the solution you found.
std_assert_test.go
Outdated
| } | ||
| return true | ||
| } | ||
| func (a fatal) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) bool { |
There was a problem hiding this comment.
There is an issue with all fatal methods.
Here I'm talking about a compatibility issue with testify that is pointless as you remove testify dependency.
But I have to mention it.
testify/require asserters don't return bool
So here (and everywhere) it should be this
| func (a fatal) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) bool { | |
| func (a fatal) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) { |
std_assert_test.go
Outdated
| msg = fmt.Sprintf(msg, msgAndArgs...) | ||
| } | ||
| t.Helper() | ||
| t.Errorf(msg) |
There was a problem hiding this comment.
| t.Errorf(msg) | |
| t.Error(msg) |
std_assert_test.go
Outdated
| msg = fmt.Sprintf(msg, msgAndArgs...) | ||
| } | ||
| t.Helper() | ||
| t.Fatalf(msg) |
There was a problem hiding this comment.
| t.Fatalf(msg) | |
| t.Fatal(msg) |
30daf59 to
a4359ce
Compare
thanks for the review, i made changes based on your remarks |
| func (nonfatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) bool { | ||
| if len(msgAndArgs) > 0 { | ||
| msg = fmt.Sprintf(msg, msgAndArgs...) | ||
| } | ||
| t.Helper() | ||
| t.Error(msg) | ||
| return false | ||
| } | ||
|
|
||
| func (fatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) { | ||
| if len(msgAndArgs) > 0 { | ||
| msg = fmt.Sprintf(msg, msgAndArgs...) | ||
| } | ||
| t.Helper() | ||
| t.Fatal(msg) | ||
| } |
There was a problem hiding this comment.
Nitpicking here.
Usually t.Helper() call in on the first line of a method.
| func (nonfatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) bool { | |
| if len(msgAndArgs) > 0 { | |
| msg = fmt.Sprintf(msg, msgAndArgs...) | |
| } | |
| t.Helper() | |
| t.Error(msg) | |
| return false | |
| } | |
| func (fatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) { | |
| if len(msgAndArgs) > 0 { | |
| msg = fmt.Sprintf(msg, msgAndArgs...) | |
| } | |
| t.Helper() | |
| t.Fatal(msg) | |
| } | |
| func (nonfatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) bool { | |
| t.Helper() | |
| if len(msgAndArgs) > 0 { | |
| msg = fmt.Sprintf(msg, msgAndArgs...) | |
| } | |
| t.Error(msg) | |
| return false | |
| } | |
| func (fatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) { | |
| t.Helper() | |
| if len(msgAndArgs) > 0 { | |
| msg = fmt.Sprintf(msg, msgAndArgs...) | |
| } | |
| t.Fatal(msg) | |
| } |
There was a problem hiding this comment.
does it changes something or is it more an idiomatic way of doing it ?
There was a problem hiding this comment.
It's clearly idiomatic, but for good reasons.
here you have calls to fmt.Sprintf which cannot call t.Error or t.Fail, so it would be OK for now.
But anyone who could come after you to update these lines, may add something that calls t.Error before the call to t.Helper a few line later. Then the line calling t.Errir would be the one reported.
Also, I thought about a possible panic, that might affect the way a test helper could react when something panic.
But I'm unsure how it would affect a test, assuming the panic is caught.
Anyway, fmt.Sprintf handles panics: an example could be a fmt.Stringer that would panic would be represented with PANIC: when passed as %s/%v to fmt.Sprintf
Do, yes. It's idiomatic and safer.
a4359ce to
374dc22
Compare
|
@hanzei wdyt ? |
|
Let me fix the CI issues first, and then I'll take a look. |
hanzei
left a comment
There was a problem hiding this comment.
I'm not too happy that we are losing some of the nicely styled output of testify with these changes, but I see the need to move away from testify.
LGTM 👍
|
@emilien-puget @ccoVeille Are we good to merge the PR? |
| @@ -0,0 +1,238 @@ | |||
| package objx_test | |||
There was a problem hiding this comment.
Add a comment that mentions why this file exists: this is a reimplementation of a subset of Testify's API to break a cycle dependency with Testify.
There was a problem hiding this comment.
i added some words about that
8b61542 to
f0057b2
Compare
|
Thank you very much @emilien-puget 🎉 |
|
@hanzei could we have a release/tag so we can update it on testify side ? |
|
For record, I suggested @alexandear to use your code in @chavacava codeberg project https://codeberg.org/chavacava/garif/pulls/15#issuecomment-10961225 He liked your idea |
stretchr/objx#159 Co-Authored-by: Emilien Puget <emilien-puget@users.noreply.github.com>
stretchr/objx#159 Co-Authored-by: Emilien Puget <emilien-puget@users.noreply.github.com>
stretchr/objx#159 Co-Authored-by: Emilien Puget <emilien-puget@users.noreply.github.com>
Summary
Replace
testifyassertions with custom testing helpersChecklist
task testtask lint