Providing @testset option with macro depending on Julia version

I want to use Test.@testset with option failfast=true in my unit testing. But this feature requires at least Julia 1.9 and I’m testing on both newer and older versions of Julia.

I would like to define a macro or function that checks the Julia version and only sets failfast if the version is above 1.9.

My best guess so far:

using Test

macro failfast()
  @static if Base.VERSION ≥ v"1.9"
    :( failfast=true )
  end
end

@testset verbose=false @failfast "MyTest" begin
  @test true
end

How do I prevent that the macro expression is executed immediately?
I also tried the use of code generation with @eval, but without success.

I don’t think this is the correct approach as the macro-expansion order is probably always wrong (depends a bit on how @testset uses the option). You’ll need to put a macro infront like

macro mytestset(body)
    quote
        if Base.VERSION ≥ v"1.9"
            @testset verbose=false failfast=true begin $body end
        else
            @testset verbose=false begin $body end
        end
    end
end

You can also extend this to take keyword arguments and forward them to @testset and then it almost looks like the regular macro :slight_smile:
(Coded on mobile so syntax might be off. I am not sure if you need the begin end around $body but shouldn’t hurt)

1 Like