計画的に実行するのはよいことですが
前回も紹介したように,
use strict;
print "1..10\n"; # 宣言部
for (1..10) {
print "ok $_\n";
}
このような宣言部の存在は,
単純そうに見えるTest Anything Protocolも,
skip_all
Perl 1.
use strict;
print "1..0\n";
exit;
これはいまでも特定の機能,
use strict;
use Test;
if ($^O eq 'MSWin32') {
plan tests => 0;
exit;
}
plan tests => 1;
ok 'tests for unix';
いまどきのTest::Moreでは意図しないところでテストがスキップされてしまうのを防ぐためtestsに0を指定することはできなくなっているので,
use strict;
use warnings;
use Test::More;
eval 'require Some::Module';
plan skip_all => 'this test requires Some::Module' if $@;
unless ($ENV{RELEASE_TESTING}) {
plan skip_all => 'set RELEASE_TESTING to test';
}
skip
ただし,
use strict;
use Test;
plan tests => 2;
skip('this test fails', 1 == 0);
ok 'portable test';
いまどきの書き方ならこうなりますね。
use strict;
use warnings;
use Test::More tests => 2;
SKIP: {
skip 'this test fails', 1;
ok 1 == 0;
}
pass 'portable test';
スキップするといっても,
> perl skip.t 1..2 ok 1 # skip this test fails ok 2 - portable test
todo
skipは上手に使えば便利な仕組みですが,
当初のtodoは,
use strict;
use Test;
plan tests => 2, todo => [1];
ok 0;
ok 1;
このテストを実行すると,
> prove todo.t todo.t .. # Failed test 1 in todo.t at line 4 *TODO* # todo.t line 4 is: ok 0; todo.t .. ok All tests successful. Test Summary Report ------------------- todo.t (Wstat: 0 Tests: 2 Failed: 0) TODO passed: 2 Files=1, Tests=2, 0 wallclock secs ( 0.03 usr + 0.03 sys = 0.06 CPU) Result: PASS
ただし,
use strict;
use warnings;
use Test::More tests => 2;
TODO: {
local $TODO = 'not implemented';
fail 'todo';
}
pass 'ok';
こちらの書き方の場合,
- ※1
ただし,
skipと違ってtodoは実際にテストを実行するため, 本当にそこで処理が止まってしまうようなエラーが出る場合はtodoではなくskipにしておく必要があります。また, Test::Moreには単なるskipと区別できるように, todo_ skipというコマンドが用意されていることも覚えておくとよいでしょう。