如何在Perl的Test :: More中声明后使用参数`tests`?

时间:2010-09-08 17:33:47

标签: perl testing

来自perldoc -f use

函数use的语法:

   use Module VERSION LIST
   use Module VERSION
   use Module LIST
   use Module
   use VERSION

但在这种情况下:

use Test::More tests => 5;

(它将测试次数设置为5)

表达式tests => 5的数据类型是什么? 是LIST还是其他什么?

如何在声明后使用此参数tests

2 个答案:

答案 0 :(得分:8)

是的,这是LIST提到的 - =>只是一种写作的奇特方式:

use Test::More ("tests", 5);

加载模块后又调用Test::More->import("tests", 5)

答案 1 :(得分:6)

您可以要求Test::More为您提供构建器对象:

use Test::More tests => 5;

my $plan = Test::More->builder->has_plan;

print "I'm going to run $plan tests\n";

您不必将测试数量设为文字。您可以计算它并将其存储在变量中:

use vars qw($tests);

BEGIN { $tests = ... some calculation ... }
use Test::More tests => $tests;

print "I'm going to run $tests tests\n";

您不必提前宣布计划:

use Test::More;

my $tests = 5;
plan( tests => $tests );

print "I'm going to run $tests tests\n";

你问过跳过测试。如果您想跳过所有测试,可以使用skip_all代替tests

use Test::More;

$condition = 1;

plan( $condition ? ( skip_all => "Some message" ) : ( tests => 4 ) );

pass() for 1 .. 5;

如果要将测试细分为组,也可以执行此操作。您计算出每个组中的测试数量,并将这些测试数量相加以创建计划。后来你知道要跳过多少:

use Test::More;

my( $passes, $fails ) = ( 3, 5 );
my( $skip_passes, $skip_fails ) = ( 0, 1 );

plan( tests => $passes + $fails );

SKIP: {
    skip "Skipping passes", $passes if $skip_passes;
    pass() for 1 .. $passes;
    }

SKIP: {
    skip "Skipping fails", $fails if $skip_fails;
    fail() for 1 .. $fails;
    }
相关问题