Test :: Most - test with a report error using stacktrace

I am fixing a large script test (> 1000 lines) that uses some utility methods (also> 1000 lines) to run retests with different settings for the source data. This helps consolidate the code. However, when the test fails, it reports the line number inside the utility method, which makes it difficult to track which test failed.

Is it possible to configure Test::Mostthat during the test run not a single line number is executed, but instead

#!/usr/bin/env perl
use strict;
use warnings;
use autodie;

use Test::Most tests => 3;

ok(1, 'first test');

note "The following includes a failed test, but a stack trace would be more helpful";

helper_sub_with_test();       # Line 13

ok(1, 'third test');

sub helper_sub_with_test {
    ok(0, "second test");     # Line 17
}

Outputs:

$ perl scratch.pl 
1..3
ok 1 - first test
# The following includes a failed test, but a stack trace would be more helpful
not ok 2 - second test
#   Failed test 'second test'
#   at scratch.pl line 17.
ok 3 - third test
# Looks like you failed 1 test of 3.

As you can see, it would be useful if the failed test reported both lines 17 and 13, when there are several calls to the utility method.

+4
source share
2 answers

, Test:: More ​​beastie, ? 13 , .

13 17, :

local $Test::Builder::Level = $Test::Builder::Level + 1;

:

#!/usr/bin/env perl
use strict;
use warnings;
use autodie;

use Test::Most tests => 3;

ok(1, 'first test');

note "The following includes a failed test, but a stack trace would be more helpful";

helper_sub_with_test();       # Line 13

ok(1, 'third test');

sub helper_sub_with_test {
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    ok(0, sprintf "second test (look at line %d)", __LINE__);     # Line 18
}
+4

, , - Test:: Builder:: ok. Test:: Most.

Aspect .

use Carp qw(longmess);
use Test::Most;
use Aspect;

after {
    # For some reason, the return value is not being captured by Aspect
    my $last_test = ($_->self->summary)[-1];
    print longmess if !$last_test;
} call "Test::Builder::ok";

sub foo { ok(0) }

foo();
pass;

done_testing;
+3

Source: https://habr.com/ru/post/1569783/


All Articles