Breaking up long testset descriptions

Very simple question - maybe even a feature request to open on JuliaLang repo.

Basically, I would like to enforce a style guide where my code is at most 80 characters long, including tests. However, the @testset macro does not allow breaking up description strings across multiple lines. I have in mind something like the following:

using Test

@testset "One very verbose and some would say" *
         "needlessly verbose testset description" begin
    @test true
    # ...
end

Trying this in the REPL with Julia 1.6.0 on an Intel-based Linux machine (Ubuntu 20.04, Focal Fossa) gets this error:

ERROR: LoadError: Unexpected argument "One very verbose and some would say" * "needlessly verbose testset description" to @testset
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] parse_testset_args(args::Tuple{Expr})
   @ Test /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Test/src/Test.jl:1280
 [3] testset_beginend(args::Tuple{Expr, Expr}, tests::Expr, source::LineNumberNode)
   @ Test /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Test/src/Test.jl:1123
 [4] var"@testset"(__source__::LineNumberNode, __module__::Module, args::Vararg{Any, N} where N)
   @ Test /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Test/src/Test.jl:1115
in expression starting at REPL[3]:1

If you have sufficiently nested testsets, or would rather have testsets flattened and have longer naming to compensate (e.g. if you wanted to use something like ReTest.jl), then this is a bit annoying from a style perspective.

Is there any native Julia solution? I am guessing that another macro can be used for the above, but it seems like a sensible enough request for the testset macro to support multi-line strings by default. It is just a single string after all - you’re not even changing the argument signature of the macro.

Thanks in advance for any advice and/or assistance.


julia> @testset "One very verbose and some would say \
                needlessly verbose testset description" begin
           @test true
           # ...
       end

julia> a = "One very verbose and some would say" *
                       "needlessly verbose testset description"

julia> @testset "$a" begin
           @test true
           # ...
       end

Thanks for the quick reply! The second approach works, the first fails with an error about an invalid escape sequence though:

julia> @testset "One very verbose and some would say \
                       needlessly verbose testset description" begin
ERROR: syntax: invalid escape sequence
Stacktrace:
 [1] top-level scope
   @ none:1

update your Julia to 1.7 (if you can)

2 Likes

That’s a nice reason to upgrade. Thanks a lot @jling!

1 Like

Triple-quoted multi-line literals work too, as well as expression interpolations like this:

@testset "$(
    "A long " *
    "description " *
    "spanning " *
    "many " *
    "lines")" begin
       @test true
end
1 Like