代码之家  ›  专栏  ›  技术社区  ›  Oleg Razgulyaev

在Perl的test::more中,如何在声明后使用参数“test s”?

  •  3
  • Oleg Razgulyaev  · 技术社区  · 15 年前

    perldoc -f use
    函数的语法 use :

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

    但在这种情况下:

    use Test::More tests => 5;
    

    (它将测试数设置为5)

    expresion的数据类型是什么 tests => 5 ?
    是单子还是别的什么?

    这个参数怎么用 tests 在申报之后?

    2 回复  |  直到 15 年前
        1
  •  8
  •   zigdon    15 年前

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

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

    依次呼叫 Test::More->import("tests", 5) 加载模块后。

        2
  •  6
  •   brian d foy    15 年前

    你可以问 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;
        }