2

PerlTest::More似乎没有提供内置的方式来表示“退出这个测试脚本,并继续下一个”。

你可以exit()die()但它不会给你非常有用的 TAP 输出或prove输出,就像

t/foo.pl       (Wstat: 256 Tests: 5 Failed: 0)
  Non-zero exit status: 1
  Parse errors: Bad plan.  You planned 38 tests but ran 5.

这不是很有帮助。

如果您BAIL_OUT("reason")获得更多有用的输出,但所有测试都会中止,而不仅仅是当前的测试脚本。

是否有一种明智的方法来打印诊断消息,当线束退出时,该消息将在prove'stderr 和摘要输出中看到,然后跳转到下一个测试脚本?

4

2 回答 2

2

很多时候,我只是将我的测试包装在里面subtest 'name' => sub { ... }并在里面使用return,如下所示:

use Test::More;

subtest 'part 1' => sub {

    plan tests => 10;

    lives_ok {
        ...
    } "this went smooth"

    or return; # no use of further testing

    # more tests

    fail "I'm done here, bail out!" or return

    # more tests

};

done_testing;

这是可行的,因为通过测试返回“true”,失败测试返回“false”,因此第二部分or得到评估。

这不会破坏 TAP,您可以“退出”子测试或整个测试(如果已包装)并继续运行其余测试。

我的用例是,如果我不能实例化要进一步测试的测试对象,那么该子测试中的其余测试将毫无用处,这些测试都没有意义,而且我只会得到杂乱无章的测试报告。

您可能会这样做:

    lives_ok {
        ...
    } "Yay"

    or fail "No use to continue here, bye!" or return;

更明确地说你确实退出了那里。

于 2020-06-05T19:10:03.223 回答
2

到目前为止,我对 Perl 标准库的了解比我想的要多得多:

# Install a filter to prevent BAIL_OUT() from aborting the whole
# run.
Test2::API::test2_add_callback_context_init(sub {
    my $context = shift;
    $context->hub->filter(sub {
        my ($hub, $event) = @_;

        #
        # If you want to see details on the event contents use:
        #
        #print("EVENT FILTER: " . Dumper($event) . "\n");

        # Turn BAIL_OUT() into die(), or maybe $ctx->throw(). We can't easily
        # get the context.
        if ($event->isa('Test2::Event::Bail')) {

            # Ideally we'd produce a Test2::Event with a Test2::EventFacet::Control
            # with terminate true, and a Test2::EventFacet::Trace . For for
            # now we'll just log the BAIL_OUT() event and exit.
            #
            # If we ->throw it instead, we get a big stack for little benefit.
            # And END { } blocks will still get run when we exit(...) here so
            # appropriate cleanup still happens.
            #
            $event->trace->alert($event->reason);
            exit(1);
        }
        return $event;
    });
});

这将导致一个测试脚本调用BAIL_OUT("FOOBAR");保释,例如

FOOBAR at t/foo.pl line 4.
# Looks like your test exited with 1 just after 5.

然后provestderr 将显示:

t/foo.pl ......... 1/38 
# Looks like your test exited with 1 just after 5.

总结会显示

t/foo.pl       (Wstat: 256 Tests: 5 Failed: 0)
  Non-zero exit status: 1
  Parse errors: Bad plan.  You planned 38 tests but ran 5.

这远非理想,因为标准错误和摘要并没有告诉我们出了什么问题。

但它阻止了BAIL_OUT()其余的运行。

一个缺点是这无助于为die(). 所以我还在寻找更好的选择。

于 2020-06-05T06:50:26.653 回答