JuliaLang / Compat.jl

Compatibility across Julia versions
Other
141 stars 117 forks source link

Unit testing syntax `@testset let` #815

Open MilesCranmer opened 7 months ago

MilesCranmer commented 7 months ago

The new unit testing syntax on Julia 1.9 is extremely useful. For example:

julia> @testset let logi = log(im)
           @test imag(logi) == π/2
           @test !iszero(real(logi))
       end
Test Failed at none:3
  Expression: !(iszero(real(logi)))
     Context: logi = 0.0 + 1.5707963267948966im

ERROR: There was an error during testing

The downside is that it's only Julia 1.9, so I can't actually use it in my packages, as I want to maintain compatibility with Julia 1.6.

So, it would be great if this syntax could be added to Compat.jl!

Best, Miles

dhanak commented 2 months ago

Apart from the arguably nicer syntax, what is the benefit of the above compared to

julia> @testset begin
           local logi = log(im)
           @test imag(logi) == π/2
           @test !iszero(real(logi))
       end

which is compatible with older Julia versions?

MilesCranmer commented 2 months ago

benefit of the above compared to

Better ergonomics. It's a huge pain to need to write out local for all relevant unittest variables, especially given some of my test suites are 1000s of lines of code. By being less ergonomic it's also less safe, because many people just won't bother to do it – they would need to figure out exactly what variables need to be local. let ... end just does this for me automatically. If we can do @testset let then I can just ctrl-F replace all instances of @testset begin and be done with it.

Also the nice part about Compat.jl is I can write backwards-compatible code without needing to miss out on the current Julia API goodness.

MilesCranmer commented 2 months ago

@aviatesk would you be up for pushing your PR https://github.com/JuliaLang/julia/pull/50151 to here as well? Or maybe comment on if there’s a sensible workaround. Like a @lettestset macro