如何通过参Perl试运行时通过 prove

当编写测试在Perl生态系统,有时候你需要编写一个测试,你可以 通过一些参数。 例如,可能的测试与参数 检查所有文件和文件如果作为参数来指定的路径,该文件,然后 它将检查只有这一个文件。

即问题是,在Perl测变量 @ARGV 出现的价值观。

这里是一个例子测试 t/main.t:

use Test::More tests => 1;
use Data::Dumper;

pass('Loaded ok');

warn Dumper \@ARGV;

如果你的运行这个试验使用 perl t/main.t one two three,然后的结论将会看到,在可变 @ARGV 得到了值从命令行:

1..1
ok 1 - Loaded ok
$VAR1 = [
          'one',
          'two',
          'three'
        ];

但是如果你跑这个相同的试验使用 prove t/main.t one two three,然后输出将可以看出,在一系列 @ARGV 值从命令行不打 和判断SecTreas, prove 认这些论点作为参数,以测试,并作为参数,以他自己。

这里是全部产出的工作:

$VAR1 = [];
t/main.t .. ok
Cannot detect source of 'one'! at /usr/local/lib/perl5/5.28.1/TAP/Parser/IteratorFactory.pm line 256.
    TAP::Parser::IteratorFactory::detect_source(TAP::Parser::IteratorFactory=HASH(0x55663d4f4300), TAP::Parser::Source=HASH(0x55663d61cdc0)) called at /usr/local/lib/perl5/5.28.1/TAP/Parser/IteratorFactory.pm line 211
    TAP::Parser::IteratorFactory::make_iterator(TAP::Parser::IteratorFactory=HASH(0x55663d4f4300), TAP::Parser::Source=HASH(0x55663d61cdc0)) called at /usr/local/lib/perl5/5.28.1/TAP/Parser.pm line 472
    TAP::Parser::_initialize(TAP::Parser=HASH(0x55663d6a11d8), HASH(0x55663d4f4390)) called at /usr/local/lib/perl5/5.28.1/TAP/Object.pm line 55
    TAP::Object::new("TAP::Parser", HASH(0x55663d4f4390)) called at /usr/local/lib/perl5/5.28.1/TAP/Object.pm line 130
    TAP::Object::_construct(TAP::Harness=HASH(0x55663ce11a90), "TAP::Parser", HASH(0x55663d4f4390)) called at /usr/local/lib/perl5/5.28.1/TAP/Harness.pm line 852
    TAP::Harness::make_parser(TAP::Harness=HASH(0x55663ce11a90), TAP::Parser::Scheduler::Job=HASH(0x55663d33bf80)) called at /usr/local/lib/perl5/5.28.1/TAP/Harness.pm line 651
    TAP::Harness::_aggregate_single(TAP::Harness=HASH(0x55663ce11a90), TAP::Parser::Aggregator=HASH(0x55663ce11880), TAP::Parser::Scheduler=HASH(0x55663d33be78)) called at /usr/local/lib/perl5/5.28.1/TAP/Harness.pm line 743
    TAP::Harness::aggregate_tests(TAP::Harness=HASH(0x55663ce11a90), TAP::Parser::Aggregator=HASH(0x55663ce11880), "t/main.t", "one", "two", "three") called at /usr/local/lib/perl5/5.28.1/TAP/Harness.pm line 558
    TAP::Harness::__ANON__() called at /usr/local/lib/perl5/5.28.1/TAP/Harness.pm line 571
    TAP::Harness::runtests(TAP::Harness=HASH(0x55663ce11a90), "t/main.t", "one", "two", "three") called at /usr/local/lib/perl5/5.28.1/App/Prove.pm line 548
    App::Prove::_runtests(App::Prove=HASH(0x55663ce093a8), HASH(0x55663d22f6c8), "t/main.t", "one", "two", "three") called at /usr/local/lib/perl5/5.28.1/App/Prove.pm line 506
    App::Prove::run(App::Prove=HASH(0x55663ce093a8)) called at /usr/local/bin/prove line 13

该方案是利用 ::

团队 prove 是一种功能—什么是写后的双重结肠符号 :: prove 穿运行测试。

因此,如果如果你跑的测试与 prove t/main.t :: one two three,我们得到的预期成果:

$VAR1 = [
          'one',
          'two',
          'three'
        ];
t/main.t .. ok
All tests successful.
Files=1, Tests=1,  0 wallclock secs ( 0.04 usr  0.01 sys +  0.09 cusr  0.02 csys =  0.16 CPU)
Result: PASS

官方文件

这一段的出 perldoc prove 有关这个功能 prove:

  Arguments to Tests
    It is possible to supply arguments to tests. To do so separate them from
    prove's own arguments with the arisdottle, '::'. For example

     prove -v t/mytest.t :: --url http://example.com

    would run t/mytest.t with the options '--url http://example.com'. When
    running multiple tests they will each receive the same arguments.

其他文章