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

Add assertionf assertions like Errorf and Equalf #356

Merged
merged 2 commits into from
May 29, 2017
Merged

Conversation

ernesto-jimenez
Copy link
Member

@ernesto-jimenez ernesto-jimenez commented Sep 24, 2016

Related to #339

In Go 1.7, the govet will complain with the following code:

assert.Error(t, err, "test %d", i)

The complain is:

no formatting directive in Error call

The cause being the fact that Error's signature ends with
msgAndArgs ...interface{} instead of msg string, args ...interface{}.

In this PR we add format assertions like Equalf and Errorf.

In a v2 of testify we would remove msgAndArgs from all the assertions and
have people use the format assertions if they want to customise the error.

Copy link

@prashantv prashantv left a comment

Choose a reason for hiding this comment

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

Overall looks good, just some minor comments.

For examples, I think they should encourage always leaving some context. For the formatting methods, the context should use a formatting string, but the non-formatting methods should just a have a static string.

@@ -246,7 +248,7 @@ func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool {

// Implements asserts that an object is implemented by the specified interface.
//
// assert.Implements(t, (*MyInterface)(nil), new(MyObject), "MyObject")

Choose a reason for hiding this comment

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

Why are the messages removed in the examples? I'd expect the methods without the f suffix to still accept arguments which are appended similar to how fmt.Println does.

Copy link
Member Author

Choose a reason for hiding this comment

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

We cannot do it for this version, since it would be a breaking change, but I'm thinking that testify's assertions provide plenty of context around the failure.

I a v2 of testify, we would probably have assertions not take any message at all, and offer the "format" assertions like Errorf to those who want to provide some context.

This has some extra benefits, since people right now make mistakes like this:

assert.Error(t, err, "expected error message")

Because they believe that would check that err.Error() == "expected error message" and the code compiles and runs.

Choose a reason for hiding this comment

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

Sorry, I don't mean change the interface, just the examples should suggest formatting.

@@ -555,7 +555,7 @@ func TestNoError(t *testing.T) {
}()

if err == nil { // err is not nil here!
t.Errorf("Error should be nil due to empty interface", err)
t.Errorf("Error should be nil due to empty interface: %s", err)

Choose a reason for hiding this comment

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

nit: should use %v for types that aren't bytes or strings
same below at line 595

Copy link
Member Author

Choose a reason for hiding this comment

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

This is part of our internal tests, so this should be fine.

Choose a reason for hiding this comment

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

Yep, this will work but the %s format specified is not intended to be used for arbitrary types:

From https://golang.org/pkg/fmt/:

%s  the uninterpreted bytes of the string or slice

Whereas we really want %v:

%v  the value in a default format

// Containsf asserts that the specified string, list(array, slice...) or map contains the
// specified substring or element.
//
// assert.Containsf(t, "Hello World", "World", "extra error message")

Choose a reason for hiding this comment

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

The examples for Containsf should contain a formatting string + arguments. Otherwise Contains is fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is part of the code-generated comments.

, "extra error message" is added to the end of usage examples on "formatted assertions".

Would need to figure out what we can add instead that would be a relevant non-confusing message+variable that works for any assertion.

@ains
Copy link

ains commented Nov 28, 2016

Any update on getting this merged in @ernesto-jimenez?

@mohit
Copy link

mohit commented Apr 21, 2017

Any updates. Seems like this is still required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants