加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – Test :: Mojo证明会导致重复记录

发布时间:2020-12-16 06:26:52 所属栏目:大数据 来源:网络整理
导读:我有以下测试脚本,它使用Test :: Mojo.当我使用perl从命令行运行它时,它输出正确.但是,当我通过“prove -v”运行它时,Mojo日志记录会被复制,其中一个不会通过“on message”进行管道传输. #!/usr/bin/env perluse strict;use warnings;use Test::More tests
我有以下测试脚本,它使用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';

关于如何获得证明和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';

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

如果您不想要此输出,似乎手动设置MOJO_LOG_LEVEL env变量是最佳方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读