miga89
miga89

Reputation: 438

Exception handling in a @testset in Julia

what is the prefered way to investigate or print out further detail (print input variable of a function, iteration number, etc.) of a failed @test inside a @testset? I tried to wrap a try-catch-block around it. However, it doesn't seem to fire.

Here is a made-up example:

using Base.Test
rng = MersenneTwister(3231);

# define function that works different than expected
function compare(a,b)
    if a == 3 && b == 3
        return false
    else
        return a == b
    end
end

# test function in a test set
@testset "Test Compare Function" begin
    for iii = 1:10
        number = rand(rng,1:10)
        try
            @test compare(number,number) == true
        catch
            @show(number)
        end
    end
end

Thank you very much!

Upvotes: 1

Views: 173

Answers (1)

Chris Rackauckas
Chris Rackauckas

Reputation: 19132

You need to make sure it tests after the printing.

@testset "Test Compare Function" begin
    for iii = 1:10
        number = rand(rng,1:10)
        @test begin
             res = compare(number,number) == true
             if !res
                 @show number
                 flush(STDOUT)
             end
             res
        end
    end
end

Upvotes: 1

Related Questions