5

我有以下使用 Test::Mojo 的测试脚本。当我使用 perl 从命令行运行它时,它会正确输出。但是,当我通过“prove -v”运行它时,Mojo 日志记录被复制,其中一个没有通过“on message”管道传输。

#!/usr/bin/env perl

use strict;
use warnings;

use Test::More tests => 1;

use Mojolicious::Lite;
use Test::Mojo;

app->log->on(
    message => sub {
        my ( $log, $level, @lines ) = @_;
        note "MojoLog $level: @lines";
    }
);

get '/debug/mojo/req_url' => sub {
    my $c = shift;

    $c->render( text => $c->req->url );
};

subtest 'Mojo - $c->req->url' => sub {
    plan tests => 3;

    my $t = Test::Mojo->new;

    $t->get_ok('/debug/mojo/req_url')    #
        ->status_is(200)                 #
        ->content_is('/debug/mojo/req_url');
};

直接运行时的输出:

$ perl dup_logging.t
1..1
# Subtest: Mojo - $c->req->url
    1..3
    # MojoLog debug: GET "/debug/mojo/req_url"
    # MojoLog debug: Routing to a callback
    # MojoLog debug: 200 OK (0.000797s, 1254.705/s)
    ok 1 - GET /debug/mojo/req_url
    ok 2 - 200 OK
    ok 3 - exact match for content
ok 1 - Mojo - $c->req->url

并且通过证明运行时的输出:

$ prove -v dup_logging.t
dup_logging.t ..
1..1
# Subtest: Mojo - $c->req->url
    1..3
[Thu Mar  8 12:16:35 2018] [debug] GET "/debug/mojo/req_url"
    # MojoLog debug: GET "/debug/mojo/req_url"
[Thu Mar  8 12:16:35 2018] [debug] Routing to a callback
    # MojoLog debug: Routing to a callback
[Thu Mar  8 12:16:35 2018] [debug] 200 OK (0.000842s, 1187.648/s)
    # MojoLog debug: 200 OK (0.000842s, 1187.648/s)
    ok 1 - GET /debug/mojo/req_url
    ok 2 - 200 OK
    ok 3 - exact match for content
ok 1 - Mojo - $c->req->url
ok
All tests successful.
Files=1, Tests=1,  1 wallclock secs ( 0.03 usr  0.01 sys +  0.34 cusr  0.03 csys =  0.41 CPU)
Result: PASS

以下是我的版本信息:

$ perl -MMojolicious -E 'say Mojolicious->VERSION'
7.14
$ prove --version
TAP::Harness v3.36 and Perl v5.16.3

我发现避免这个问题的一种方法是在脚本顶部设置 MOJO_LOG_LEVEL 环境变量。

$ENV{MOJO_LOG_LEVEL} = 'fatal';

关于如何让 Prove 和 Test::Mojo 在日志记录方面发挥出色的任何其他建议?

4

1 回答 1

6

证明测试运行程序使用TAP::Harness基础设施。运行时prove -v,这将设置HARNESS_IS_VERBOSE环境变量。

然后,Mojo::Test 获取这个环境变量:

# Silent or loud tests
$ENV{MOJO_LOG_LEVEL} ||= $ENV{HARNESS_IS_VERBOSE} ? 'debug' : 'fatal';

因此,您在运行时会收到 Mojo 的调试日志消息prove -v

如果您不想要此输出,手动设置 MOJO_LOG_LEVEL 环境变量似乎是最好的方法。

于 2018-03-08T20:59:48.560 回答