Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing syntax @testset let #815

Open
MilesCranmer opened this issue Dec 11, 2023 · 3 comments
Open

Unit testing syntax @testset let #815

MilesCranmer opened this issue Dec 11, 2023 · 3 comments

Comments

@MilesCranmer
Copy link
Sponsor Member

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
Copy link
Contributor

dhanak commented May 2, 2024

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
Copy link
Sponsor Member Author

MilesCranmer commented May 2, 2024

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
Copy link
Sponsor Member Author

MilesCranmer commented May 2, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants