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

Better @assert default failure message, like that given by @test #21725

Open
oxinabox opened this issue May 6, 2017 · 6 comments
Open

Better @assert default failure message, like that given by @test #21725

oxinabox opened this issue May 6, 2017 · 6 comments
Labels
error handling Handling of exceptions by Julia or the user

Comments

@oxinabox
Copy link
Contributor

oxinabox commented May 6, 2017

when you fail a @test, you get a great message telling you what the expect value was and what the actual value was.
With @assert, you do not.

For example

julia> using Base.Test

julia> a=(2,4)
(2,4)

julia> @test a[1]==3
Test Failed
  Expression: a[1] == 3
   Evaluated: 2 == 3
ERROR: There was an error during testing
 in record(::Base.Test.FallbackTestSet, ::Base.Test.Fail) at ./test.jl:397
 in do_test(::Base.Test.Returned, ::Expr) at ./test.jl:281

julia> @assert a[1]==3
ERROR: AssertionError: a[1] == 3

I often fine myself writing things like:

julia> @assert(a[1]==3, "a[1]==$(a[1]) != 3")
ERROR: AssertionError: a[1]==2 != 3

but @test shows that this can be done automatically.
I think that makes a better default, than just printing out what the expression was, without showing the values.

@oxinabox oxinabox changed the title Better assert default failure message, like that given by @test Better @assert default failure message, like that given by @test May 6, 2017
@TotalVerb
Copy link
Contributor

Just curious: why not simply use Test.@test?

@oxinabox
Copy link
Contributor Author

oxinabox commented May 6, 2017

They are not semantically the same operation.
That feels rather dirty, but it would work, yes.

But eventually @asserts will be (to some lesser or greater extent),
be removed when running in some form of optimized mode.
#7732
#10614

Also @test throws the error on the wrong line -- it alway throws an error from ./test.jl:397
when it fails to find a outer testset to eat the Error.

@kshyatt kshyatt added the error handling Handling of exceptions by Julia or the user label May 6, 2017
@StefanKarpinski
Copy link
Member

StefanKarpinski commented May 8, 2017

I agree with this general direction – if we're going to keep asserts. There's the question of whether we should or not, however, since people tend to abuse asserts for error checking instead of strictly using them for verifying the internal logic of their algorithms when testing.

@samoconnor
Copy link
Contributor

Related #15495

@al6x
Copy link

al6x commented Jan 30, 2020

+1 for printing the actual value, currently you see message like

AssertionError: base ≈ 12

and you need to add println(base) and run it again before you can fix the code.

@Wikunia
Copy link
Contributor

Wikunia commented Apr 17, 2020

Maybe there can be a different macro for this behavior otherwise @StefanKarpinski or should one always do using Test; @test for this usecases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error handling Handling of exceptions by Julia or the user
Projects
None yet
Development

No branches or pull requests

7 participants