Julia: custom output for tests that use `isa`

Created on 23 Apr 2018  路  3Comments  路  Source: JuliaLang/julia

I often want to test my things are resturning the type I want.
The sensible way to do so would be:
@test result isa FooType.

But that does not give a good error message on failure.
So instead I write:

@test result |> typeof <: FooType,
which will, on a failure output the type that it actually was

Sometimes I could just be using @inferred for this.

This is simialr to
https://github.com/JuliaLang/julia/issues/25486
https://github.com/JuliaLang/julia/issues/25487

display and printing testsystem

Most helpful comment

All 3 comments

You can make your own:

macro cooltest(ex)
    if ex.head === :call && ex.args[1] === :isa
        a, b = ex.args[2:3]
        return :(@test typeof($a) <: $b)
    else
        return :(@test $ex)
    end
end

which produces output like

julia> @cooltest 1 isa String
Test Failed at REPL[2]:4
  Expression: typeof(1) <: String
   Evaluated: Int64 <: String
ERROR: There was an error during testing

Personally I don't think we should do any syntactic modifications in @test itself to make sure that the test that's run faithfully represents what was written.

30721 adds nice output for isa:

julia> using Test

julia> result = Int
Int64

julia> @test result isa AbstractString
Test Failed at REPL[3]:1
  Expression: result isa AbstractString
   Evaluated: Int64 isa AbstractString
ERROR: There was an error during testing
Was this page helpful?
0 / 5 - 0 ratings