簡體   English   中英

如何使用 Test::more 忽略 perl 中的 TAP 結果?

[英]How can I ignore a TAP result in perl with Test::more?

我想利用這個庫來漂亮地打印我的測試Test::Differences; 但我不想將其計入我的計划中,因為這僅用於在我的用例中進行調試。

所以我創建了這個問題的一個最小示例來重現這個問題,我知道這樣做是不“正確的”,但是很好地說明了這個問題。

use strict;
use warnings;
use utf8::all;
use open ':std', ':encoding(UTF-8)';

use Test::Differences;
use Test::Deep::NoTest qw(cmp_details deep_diag);
use Test::More tests => 2;
use JSON;

my $context = 1;
my $extracted_ref = {
    a => '1',
    b => '2',
    c => '3',
    d => '4',
};
my $reference_ref = {
    a => '1',
    b => '3',
    c => '3',
    d => '4',
};

my ($is_compare_ok, $stack) = cmp_details($extracted_ref,$reference_ref);
my $json_reference = JSON->new->canonical->encode($reference_ref);
my $json_extracted = JSON->new->canonical->encode($extracted_ref);

ok(1);
if ($is_compare_ok){
    ok(1);
    return 1;                               
}else{                                  
    ok(0);
    eq_or_diff($reference_ref, $extracted_ref, "Error in '$0'", {context=>$context}); # <- I don't want to count this
    return 0;
}

我希望在腳本ok執行測試,這兩個在此過程中完成。 但是 function eq_or_diff添加了一個新測試,因此腳本以執行 3 個測試結束,因此done_testing()期望2但得到3

這是一個最小的例子,但通常我有一個主腳本:

main.t

use Test::More tests => 2;
...
ok(some_function_in_other_file_or_library(), "name_of_the_test");
...

lib.pm

...
sub some_function_in_other_file_or_library{
...
   eq_or_diff(...)
   return $bool;
}
...

我提到這一點是因為我嘗試使用substest並且我無法使其工作,而且我主要不必現在在 lib 中發生什么,因為否則我認為我可以使用done_testing($planned_tests+1)

lib.pm

use Test::More tests qw/subtest/;
subtest 'An example subtest' => sub {
    plan tests => 1;
 
    eq_or_diff(...)
};

摘要:我怎樣才能做出類似於:

do_not_add_to_tap(eq_or_diff(...))
# or 
increased during runtime the planned tests
plan() = plan()+1...

Test::Differences和 Friends 真正為您做兩件事,然后通過將其與測試框架緊密耦合來使其復雜化。 這不是什么大問題,因為它實際上只是包裝了其他模塊中的東西,您可以直接使用。

首先,它使用Data::Dumper到 output 通過設置各種參數(例如SortKeys )進行可預測的序列化。 其次,它使用Text::Diff來比較這些轉儲的行。

所以,你自己做吧。 這是第一部分,它只返回表示轉儲的單個字符串,適用於字符串比較以查看它們是否相等:

sub dump_it {
    my( $arg ) = @_;
    local $Data::Dumper::Deparse   = 1;
    local $Data::Dumper::Indent    = 1;
    local $Data::Dumper::Purity    = 0;
    local $Data::Dumper::Terse     = 1;
    local $Data::Dumper::Deepcopy  = 1;
    local $Data::Dumper::Quotekeys = 0;
    local $Data::Dumper::Useperl   = 1;
    local $Data::Dumper::Sortkeys  = 1;
    Data::Dumper::Dumper($arg);
    }

第二部分與Text::Diff做類似的事情。 它設置了各種參數,您可以根據需要調味。 我這樣做是為了將兩個字符串傳遞給它,我將它們變成數組引用中的行(因為這都在內存中):

sub my_diff {
    state $rc = require Text::Diff;
    my( $got, $expected, $context ) = @_;

    $context //= 3;  # lines around the diff to show

    my $diff = Text::Diff::diff
        [ split /^/m, dump_it( $got ) ],
        [ split /^/m, dump_it( $expected ) ],
      { CONTEXT     => $context,
        STYLE       => 'Table',
        FILENAME_A  => 'got',
        FILENAME_B  => 'expected',
        OFFSET_A    => 1,
        OFFSET_B    => 1,
        INDEX_LABEL => "Ln",
      };
    chomp $diff;
    $diff .= "\n";

    $diff;
    }

所以現在你的程序的內容是這樣的:

my $is_same = dump_it( $reference_ref ) eq dump_it( $extracted_ref );

pass();
if ($is_same){
    pass();
}else{
    fail();
    note( my_diff( $reference_ref, $extracted_ref, 1 ) );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM