Reza Afzalan
Reza Afzalan

Reputation: 5746

How to find variable values where @test fails

When unit testing, it is helpful to know variable values at a failure point

julia> @test a*4==2
Test Failed
  Expression: a * 4 == 2
   Evaluated: 4 == 2
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

e.g. in minimal test above, what is the right way to show value of a if the test fails?

EDIT Actually, I am looking for a general & practical solution for many arbitrary tests and variables.

Upvotes: 4

Views: 188

Answers (2)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22225

Your question makes little sense to me. You want a "general" solution, but you seem to be giving a very "specific" example where your test is a simple expression containing variables, which is generally not going to be the case with unit testing.

There's nothing special about the @test macro itself, it is just a pretty convenience function; the "test" itself in your case is the expression a * 4 == 2.

In general the test will either be a bespoke test function returning a boolean (e.g. testFunction1()) or a test that your function acts according to specification in terms of what it returns for particular inputs (e.g. function1(2,3) == 5). This is the actual test. @test just prettifies the output. It doesn't make sense to talk about variables in the more general unit testing scenario, where there generally aren't any, let alone in terms of this being part of the @test macro functionality which is just a prettifying function!

Therefore my opinion is, if you want to inspect variables in the very specific testing scenario you envisaged, you can come up with a very specific way to do it, e.g. like the one Alexander suggested, or, more generally, by making the collection of any variables involved part of the testing function itself, for potential inspection. But it doesn't make sense to be asking for a "general & practical solution for many arbitrary tests and variables", since generally this makes no sense in the context of unit testing, except in the very simplistic scenario you presented here, which is fairly atypical.

Upvotes: 2

Alexander Morley
Alexander Morley

Reputation: 4181

Just catch the error? If you still want the error to be thrown again you can re-throw it.

a = 2
try @test a*4 == 2
catch ex
    println("a = $a")
    error(ex)
end

Will give:

Test Failed

Expression: a * 4 == 2

Evaluated: 8 == 2

a = 2

LoadError: ErrorException("There was an error during testing") while loading In[44], in expression starting on line 5

in error(::ErrorException) at ./error.jl:22

Upvotes: 2

Related Questions