"Return exact same expression."
macro flawed_identity(ex)
@assert ex.head != :(=) "oops we don't support head of :="
ex
end
and I write some tests:
@testset "identity" begin
ex = :(2+1)
new = @flawed_identity 2 + 1
@test eval(new) == ex
@flawed_identity ex2 = 2 + 1
@test eval(new) == ex
end
When I run the tests, I get an AssertionError, and I don’t get a Test Summary. How should I write Unit Tests for macros such that all tests run even if an error pops up?
julia> using Test
julia> "Return exact same expression."
macro flawed_identity(ex)
@assert ex.head != :(=) "oops we don't support head of :="
ex
end
@flawed_identity
julia> macro test_macro_throws(err_type, ex)
return quote
@test_throws(
$(esc(err_type)),
try
@eval $(esc(ex))
catch err
@show err
throw(err.error)
end
)
end
end
@test_macro_throws (macro with 2 methods)
julia> @testset "identity" begin
new = @flawed_identity 2 + 1
@test new == 3
@test_macro_throws AssertionError @flawed_identity ex2 = 2 + 1
end
err = LoadError("REPL[84]", 4, AssertionError("oops we don't support head of :="))
Test Summary: | Pass Total
identity | 2 2
Test.DefaultTestSet("identity", Any[], 2, false, false)
there might be a better way. We haven’t tried anything else recently.
Bit late to the party, but it can sometimes also be very useful to move as much of the logic as possible into a function. The function can be tested quite easily. See also InteractiveUtils.gen_call_with_extracted_types_and_kwargs and it’s usage in, for example, Revise.jl