Test with title string

Hi,

I am doing some testing for a package and I would like to have a string attached to the test result. Right now, I have to use @testset with a begin keyword.

Is there anything like the following?

@test "Testing dummy equality" 1==0

Thank you for your help,

Best

No, as far as I can tell the Base.Test module does not provide any way to label individual tests except by putting them into a @testset.

You could, of course, write your own functions that include labels, e.g.

==ᴸ(a,b; label="") = a == b
@test 1 ==ᴸ 0 label="Testing dummy equality"

(Keyword arguments on the @test line are passed to the function call in the first expression.)

3 Likes

Thank you but this does not print the string like @testset. I guess I am looking for a “short” @testset

If the goal is to print the label and a pass/fail, then maybe this’ll do the trick:

function print_test(label, cond)
    printstyled("$label: ", color=:normal)
    if cond
        printstyled("PASS", color=:green)
    else
        printstyled("FAIL", color=:red)
    end
    print("\n")
end

and an example usage would look like:

print_test("Testing dummy equality", 1==0)

However, since this doesn’t use @test, there won’t be any useful error messages if the test fails and it won’t be counted within a testset block.
A possible workaround is to return true/false from the print_test() function, then it can be called by @test print_test(...). That’ll make the test count, but any error messages will still be useless probably.

1 Like

I just use lots of short testsets.
Often nested

1 Like

You could define:

using Test
macro testL(label, args...)
    :(@testset $label begin @test $(args...); end)
end

and then you can do

@testL "Test dummy equality" 1==0

for example.

3 Likes

Wonderfull! Thank you