no_plan
このTODOの例のように,
たとえば特定の環境ではスキップしたいテストがあったとしましょう。伝統的な書き方では最初にテストの件数を宣言しなければなりませんから,
use strict;
print "1..2\n"; # 宣言部
if ($^O ne 'MSWin32') {
print "ok 1\n";
}
else {
print "ok 1 # Skip\n";
}
print "ok 2\n";
このくらいであればスキップの機構を使わず,
use strict;
my $total = $^O ne 'MSWin32' ? 2 : 1;
print "1..$total\n"; # 宣言部
my $count = 0;
if ($^O ne 'MSWin32') {
printf "ok %d\n", ++$count;
}
printf "ok %d\n", ++$count;
これを21世紀風のTest Anything Protocolで書き直すと,
use strict;
my $count = 0;
if ($^O ne 'MSWin32') {
printf "ok %d\n", ++$count;
}
printf "ok %d\n", ++$count;
print "1..$count\n"; # 宣言部
ここではいちいち自分でカウンタの値を増やしていますが,
no_planの問題点
ところが,
use strict;
use Test::More 'no_plan'; # Test::Moreの宣言部はここ
if ($^O ne 'MSWin32') {
pass;
}
pass;
# Test Anything Protocolの宣言部はここに出力
このTest::Moreのテスト計画の宣言部と実際にTest Anything Protocolの宣言部が出力される場所のズレは,
たとえば,
あるいは,
use strict;
use Test::More 'no_plan';
if (my $pid = fork) {
wait;
pass "parent";
}
else {
pass "child";
}
これは,
use strict;
use Test::More tests => 2;
Test::More->builder->use_numbers(0); # テストが順不同でも許す
Test::More->builder->no_ending(1); # child側の整合性チェックを省略する
if (my $pid = fork) {
wait;
pass "parent";
}
else {
pass "child";
}