I wrote some test framework using Perl's Test::more, specifically using the ok($test_result, $test_description).
As some tests "cluster", I wrote a subroutine that performs multiple ok() tests, and the main program calls such subroutines.
Now the problem for a failed test is this:
Test::More outputs the line number within the subroutine (direct caller), but I'd like to have the "caller's caller" (indirect caller) line to be output instead.
Is that possible?
Imagine the code to be similar as this:
#!/usr/bin/perl
use 5.18.2;
use warnings;
use strict;
use Test::More;
sub foo(@)
{
# do something magical
return undef;
}
sub foo_OK(@)
{
ok(foo(@_), 'foo_OK: ' . join(' ', @_)); # actually less trivial
}
sub complex_test(@)
{
foo_OK(qw(something special), @_);
foo_OK(qw(something else), @_);
#...
}
sub main()
{
complex_test(qw(abra kadabra));
complex_test(qw(more magic));
#...
}
main();
done_testing();
So I'd like to see the line of main, not the line of test_OK if ok() fails.
As shown the output would be:
not ok 1 - foo_OK: something special abra kadabra
# Failed test 'foo_OK: something special abra kadabra'
# at /tmp/test.pl line 15.
not ok 2 - foo_OK: something else abra kadabra
# Failed test 'foo_OK: something else abra kadabra'
# at /tmp/test.pl line 15.
not ok 3 - foo_OK: something special more magic
# Failed test 'foo_OK: something special more magic'
# at /tmp/test.pl line 15.
not ok 4 - foo_OK: something else more magic
# Failed test 'foo_OK: something else more magic'
# at /tmp/test.pl line 15.
1..4
# Looks like you failed 4 tests of 4.
Based on https://stackoverflow.com/a/78083924/6607497 I used
Test::Builderto create sub-tests via thechild()method, so a failing test would show the line number, but also causing the parent test to fail (that would also print the line number). That way I get the information I wanted, even with more output that ideally wanted.A modified example would look like this (line numbers added to understand the output better):
And an example output would be: