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

Improved error message when checks with variables fail #92

Open
alecthomas opened this issue May 7, 2018 · 4 comments
Open

Improved error message when checks with variables fail #92

alecthomas opened this issue May 7, 2018 · 4 comments

Comments

@alecthomas
Copy link

I realise this would be difficult (or potentially impossible) to solve, but when an expression fails it would be very useful to have the value available.

eg.

The following test code:

a := 1
assert.Check(t, a == 2)

Produces the error:

	parser_test.go:20: assertion failed: expression is false: a == 2

This is not that helpful, as I now need to edit the code to print the actual value.

Ideally it would print something like:

	parser_test.go:20: assertion failed: expression is false: a (1) == 2
@alecthomas alecthomas changed the title Message when check fails Improved error message when checks with variables fail May 7, 2018
@dnephin
Copy link
Member

dnephin commented May 7, 2018

That would be great, but I'm not sure it's possible. I think it will be necessary to keep assert.Equal() for those comparisons. assert.Equal() will print the ast of the arguments if it's a simple expression.

@dnephin
Copy link
Member

dnephin commented May 7, 2018

Another option is something like this:

assert.Check(t, a == 2, "a=%s", a)
// parser_test.go:20: assertion failed: expression is false: a == 2: a=1

If printing the args as part of the "extra messages" is a common pattern it could be done with a convenience function:

assert.Check(t, x > y, assert.Args(x, y))
// parser_test.go:20: assertion failed: expression is false: x > y: x=2, y=100

I don't know if we can do any better than that.

@thaJeztah
Copy link
Contributor

Would assert.Equal() satisfy your use case?

func TestAreYouTheSame(t *testing.T) {
	a := 1
	assert.Equal(t, a, 2)
}

Produces:

=== RUN   TestAreYouTheSame
--- FAIL: TestAreYouTheSame (0.00s)
    bla_test.go:16: assertion failed: 1 (a int) != 2 (int)
FAIL

Process finished with exit code 1

@thaJeztah
Copy link
Contributor

Or, if you need assert.Check;

func TestAreYouTheSame(t *testing.T) {
	a := 1
	assert.Check(t, cmp.Equal(a, 2))
	assert.Equal(t, a, 2)
}
=== RUN   TestAreYouTheSame
--- FAIL: TestAreYouTheSame (0.00s)
    bla_test.go:16: assertion failed: 1 (a int) != 2 (int)
    bla_test.go:17: assertion failed: 1 (a int) != 2 (int)
FAIL

Process finished with exit code 1

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

No branches or pull requests

3 participants