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

assert: document that filename and line number are included #233

Merged
merged 1 commit into from Apr 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions assert/assert.go
Expand Up @@ -3,8 +3,19 @@ values in tests. When an assertion fails a helpful error message is printed.

Example usage

All the assertions in this package use testing.T.Helper to mark themselves as
test helpers. This allows the testing package to print the filename and line
number of the file function that failed.

assert.NilError(t, err)
// filename_test.go:212: assertion failed: error is not nil: file not found

If any assertion is called from a helper function, make sure to call t.Helper
from the helper function so that the filename and line number remain correct.

The examples below show assert used with some common types and the failure
messages it produces.
messages it produces. The filename and line number portion of the failure
message is omitted from these examples for brevity.

// booleans

Expand Down Expand Up @@ -171,7 +182,7 @@ func NilError(t TestingT, err error, msgAndArgs ...interface{}) {
// values.
//
// assert.Equal(t, actual, expected)
// // assertion failed: 1 (actual int) != 21 (expected int32)
// // main_test.go:41: assertion failed: 1 (actual int) != 21 (expected int32)
//
// If either x or y are a multi-line string the failure message will include a
// unified diff of the two values. If the values only differ by whitespace
Expand Down