Test with_handler() in 0.6.0



I’m using Julia 0.6.0 and would like to know the equivalent of this code in 0.4.x (taken from https://docs.julialang.org/en/release-0.4/stdlib/test/#handlers)

julia> using Base.Test

julia> custom_handler(r::Test.Success) = println("Success on $(r.expr)")
custom_handler (generic function with 1 method)

julia> custom_handler(r::Test.Failure) = error("Error on custom handler: $(r.expr)")
custom_handler (generic function with 2 methods)

julia> custom_handler(r::Test.Error) = rethrow(r)
custom_handler (generic function with 3 methods)

julia> Test.with_handler(custom_handler) do
         @test 1 == 1
         @test 1 != 1
Success on :((1==1))
ERROR: Error on custom handler: :((1!=1))
 in error at error.jl:21
 in custom_handler at none:1
 in do_test at test.jl:39
 in anonymous at no file:3
 in task_local_storage at task.jl:28
 in with_handler at test.jl:24

I want to print custom messages on PASS/FAIL of each test.


I do not know how to make this work under 0.6

But having a look at test.jl, you can see which functions you can modify to get your custom messages.

import Base.Test.Pass
function Base.show(io::IO, t::Pass)
    print_with_color(:green, io, "Test Passed. You can get some ice cream now \n"; bold = true)
    if !(t.orig_expr === nothing)
        print(io, "  Expression: ", t.orig_expr)
    if t.test_type == :test_throws
        # The correct type of exception was thrown
        print(io, "\n      Thrown: ", typeof(t.value))
    elseif t.test_type == :test && isa(t.data,Expr) && t.data.head == :comparison
        # The test was an expression, so display the term-by-term
        # evaluated version as well
        print(io, "\n   Evaluated: ", t.data)

@test 2 == 2 #this should print 'Test Passed. You can get some ice cream now '

Test Passed. You can get some ice cream now 


Thanks @bernhard :slight_smile:
This is helpful