Hvordan til at videregive parametre til Perl-test, når de kører gennem prove

Når du skriver tests i Perl økosystem, nogle gange er du nødt til at skrive en test, som du kan gå nogle argumenter. For eksempel, du kan prøve med nogen argumenter skal kontrollere alle filer, og hvis det som argument for at angive stien til filen, så det vil kun denne ene fil.

Ie, problemet er, at i Perl-test variabel @ARGV optrådte værdier.

Her er et eksempel test t/main.t:

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

pass('Loaded ok');

warn Dumper \@ARGV;

Hvis du kører denne test bruger perl t/main.t one two three, så er konklusionen vil ses, at i de variable @ARGV fik værdier fra kommandolinjen:

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

Men hvis du kører den samme test ved hjælp af prove t/main.t one two three, så resultatet vil blive set i et array @ARGV værdier fra kommandolinjen ikke ramt og at dømme efter SecTreas, prove opfattet disse argumenter som argumenter til den test, og som argumenter til sig selv.

Her er den fulde produktion af arbejde:

$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

Løsningen er at bruge ::

Holdet prove er en funktion — hvad der er skrevet efter dobbelt kolon symbol :: prove gennemløb for at køre testen.

Så hvis du køre testen med prove t/main.t :: one two three, vi får det forventede resultat:

$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

Officiel dokumentation

Her er et uddrag af output perldoc prove om denne funktion 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.

Andre artikler