Gre*_*bet 10 perl unit-testing tap
当Test :: More将arrayrefs和hashrefs相互比较时,相应的诊断消息确实提供了信息,并显示了结构不同的第一个索引,无论嵌套深度如何.但是,当它将arrayref或hashref与简单标量进行比较时,它会在诊断消息中生成一个字符串化的标量(带有内存地址和引用类型),这很难解释.
有没有办法以自定义方式(例如使用Data :: Dumper)将Test :: More配置为漂亮打印数组或hashrefs?
这是一个包含两个测试用例的示例.第一部分让您深入了解程序中存在的内容,但却出乎意料.第二个通知用户字符串和arrayref之间的类型不匹配,但不打印arrayref中的任何项目.
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 2;
is_deeply(
{
a => [5],
},
{
a => [5, 6, 8],
},
'compare two hashrefs structurally (very informative output)',
);
is_deeply(
[5, 6, 8],
"",
'compare two "incompatible" values structurally (uninformative output)',
);
Run Code Online (Sandbox Code Playgroud)
而TAP输出:
1..2
not ok 1 - compare two hashrefs structurally (very informative output)
# Failed test 'compare two hashrefs structurally (very informative output)'
# at test-more-failure.pl line 6.
# Structures begin differing at:
# $got->{a}[1] = Does not exist
# $expected->{a}[1] = '6'
not ok 2 - compare two "incompatible" values structurally (uninformative output)
# Failed test 'compare two "incompatible" values structurally (uninformative output)'
# at test-more-failure.pl line 16.
# Structures begin differing at:
# $got = ARRAY(0x7fe66b82cde8)
# $expected = ''
# Looks like you failed 2 tests of 2.
Run Code Online (Sandbox Code Playgroud)
看看is_deeply
Test :: More 的实现,似乎没有办法使用自定义漂亮的打印机或配置模块的详细程度.至少没有一个对我来说很明显.
以下是我们比较引用和非引用时会发生的情况:
https://metacpan.org/source/EXODIST/Test-Simple-1.302062/lib/Test/More.pm#L1121
它似乎是在呼唤_format_stack({vals => [...]})
而不是_format_stack(...)
https://metacpan.org/source/EXODIST/Test-Simple-1.302062/lib/Test/More.pm#L1139
tl; dris_deeply($this, $that) || diag explain $this
根据具体情况使用.
你好.我是应该受到责备的人is_deeply
.它故意设计为在出现故障时不会呕吐出潜在的巨大数据结构.相反,它停在第一个区别.因此,您可能不希望全局is_deeply
转储其参数.如果类型错了,如果你期望苹果并且有斑马,那么知道有多少斑马和他们的生活故事并没有多大意义.
没有受支持的方式来改变它的诊断,抱歉,它不太可能存在.Test :: More正在被Test2取代.Test :: More已在Test2之上实现,但由于向后兼容性原因而未利用其功能.
您可以使用Test2 :: Bundle :: More来更直接地访问Test2的功能,但它不是100%兼容,并且显示类似于如何is_deeply
.但是,它更加灵活,您可以找到一种方法来改变其诊断行为.看看Test2 :: Compare.
回到你的问题...... explain
根据具体情况使用.explain
使用正确配置的Data :: Dumper来转储数据结构.由于Test :: More函数返回它们是通过还是失败,因此您可以编写is_deeply($this, $that) || diag explain $this
.例如...
my $stuff = [5, 6, 8];
is_deeply $stuff, "" || diag explain $stuff;
not ok 2
# Failed test at /Users/schwern/tmp/test.plx line 17.
# Structures begin differing at:
# $got = ARRAY(0x7f80b3803078)
# $expected = ''
# [
# 5,
# 6,
# 8
# ]
Run Code Online (Sandbox Code Playgroud)
diag
是如何打印故障诊断(这是一种更礼貌的方式打印到STDERR).