Mil*_*ler 5 perl mojolicious-lite
我有以下测试脚本,它使用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');
};
Run Code Online (Sandbox Code Playgroud)
直接运行时的输出:
$ 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
Run Code Online (Sandbox Code Playgroud)
运行时的输出证明:
$ 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
Run Code Online (Sandbox Code Playgroud)
以下是我的版本信息:
$ perl -MMojolicious -E 'say Mojolicious->VERSION'
7.14
$ prove --version
TAP::Harness v3.36 and Perl v5.16.3
Run Code Online (Sandbox Code Playgroud)
我发现避免此问题的一种方法是在脚本顶部设置MOJO_LOG_LEVEL环境变量.
$ENV{MOJO_LOG_LEVEL} = 'fatal';
Run Code Online (Sandbox Code Playgroud)
关于如何获得证明和Test :: Mojo关于日志记录的任何其他建议?
证明testrunner使用TAP::Harness基础设施.运行时prove -v,这将设置HARNESS_IS_VERBOSE环境变量.
然后,Mojo :: Test选择了这个环境变量:
# Silent or loud tests
$ENV{MOJO_LOG_LEVEL} ||= $ENV{HARNESS_IS_VERBOSE} ? 'debug' : 'fatal';
Run Code Online (Sandbox Code Playgroud)
因此,您在运行时会收到Mojo的调试日志消息prove -v.
如果您不想要此输出,似乎手动设置MOJO_LOG_LEVEL env变量是最佳方法.