如何有条件地使用Test::Simple?
How to use Test::Simple conditionally?
我想添加选项来有条件地使用命令行选项测试我的程序,例如执行 &test
的 --test
。
不幸的是,即使我在 &test
子例程中添加 use Test::Simple tests => 1
,Perl 仍认为我想在其他地方进行测试。
是否可以仅在需要时使用 Test::Simple
甚至 Test::More
?
如果您希望整个 Test::More
语句仅在特定条件下加载,您可以执行以下操作:
if( $condition ) {
require Test::More ;
import Test::More ;
plan tests => 1 ;
# Place your tests here...
}
这将导致与 use Test::More tests => 1;
相同的结果。
您的问题导致 use
是一个编译时语句,无论是否在 if
语句(在运行时计算)内执行。
这是 use if
的解决方案。它看起来有点难看,因为您必须确保 if
(pragma)操作的变量必须设置为 "compile-time"(即 BEGIN
-块顺序).
my $test;
BEGIN{
GetOptions( "test" => $test) or die "wrong arguments"
}
myTest() if $test;
say "normal program";
sub myTest{
use if $test => 'Test::Simple', tests => 1;
ok("1", "tests are running");
}
我想添加选项来有条件地使用命令行选项测试我的程序,例如执行 &test
的 --test
。
不幸的是,即使我在 &test
子例程中添加 use Test::Simple tests => 1
,Perl 仍认为我想在其他地方进行测试。
是否可以仅在需要时使用 Test::Simple
甚至 Test::More
?
如果您希望整个 Test::More
语句仅在特定条件下加载,您可以执行以下操作:
if( $condition ) {
require Test::More ;
import Test::More ;
plan tests => 1 ;
# Place your tests here...
}
这将导致与 use Test::More tests => 1;
相同的结果。
您的问题导致 use
是一个编译时语句,无论是否在 if
语句(在运行时计算)内执行。
这是 use if
的解决方案。它看起来有点难看,因为您必须确保 if
(pragma)操作的变量必须设置为 "compile-time"(即 BEGIN
-块顺序).
my $test;
BEGIN{
GetOptions( "test" => $test) or die "wrong arguments"
}
myTest() if $test;
say "normal program";
sub myTest{
use if $test => 'Test::Simple', tests => 1;
ok("1", "tests are running");
}