2

当我尝试和时,我得到以下回溯prove factorial

Cannot detect source of 'factorial'! at /usr/share/perl/5.18/TAP/Parser/IteratorFactory.pm line 263.
    TAP::Parser::IteratorFactory::detect_source('TAP::Parser::IteratorFactory=HASH(0x2856b08)', 'TAP::Parser::Source=HASH(0x2856bc8)') called at /usr/share/perl/5.18/TAP/Parser/IteratorFactory.pm line 213
    TAP::Parser::IteratorFactory::make_iterator('TAP::Parser::IteratorFactory=HASH(0x2856b08)', 'TAP::Parser::Source=HASH(0x2856bc8)') called at /usr/share/perl/5.18/TAP/Parser.pm line 469
    TAP::Parser::_initialize('TAP::Parser=HASH(0x28569e8)', 'HASH(0x23d8cb8)') called at /usr/share/perl/5.18/TAP/Object.pm line 58
    TAP::Object::new('TAP::Parser', 'HASH(0x23d8cb8)') called at /usr/share/perl/5.18/TAP/Object.pm line 133
    TAP::Object::_construct('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser', 'HASH(0x23d8cb8)') called at /usr/share/perl/5.18/TAP/Harness.pm line 779
    TAP::Harness::make_parser('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser::Scheduler::Job=HASH(0x28235d0)') called at /usr/share/perl/5.18/TAP/Harness.pm line 578
    TAP::Harness::_aggregate_single('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser::Aggregator=HASH(0x2740ae0)', 'TAP::Parser::Scheduler=HASH(0x2823570)') called at /usr/share/perl/5.18/TAP/Harness.pm line 670
    TAP::Harness::aggregate_tests('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser::Aggregator=HASH(0x2740ae0)', 'factorial') called at /usr/share/perl/5.18/TAP/Harness.pm line 485
    TAP::Harness::__ANON__() called at /usr/share/perl/5.18/TAP/Harness.pm line 498
    TAP::Harness::runtests('TAP::Harness=HASH(0x2131db8)', 'factorial') called at /usr/share/perl/5.18/App/Prove.pm line 554
    App::Prove::_runtests('App::Prove=HASH(0x2120170)', 'HASH(0x2606428)', 'TAP::Harness', 'factorial') called at /usr/share/perl/5.18/App/Prove.pm line 512
    App::Prove::run('App::Prove=HASH(0x2120170)') called at /usr/bin/prove line 11

这是我的factorial.pm

package factorial;

use Exporter qw(import);

BEGIN {
    our @EXPORT_OK  = qw(factorial);
}

sub factorial
{
    my ($parm) = @_;

    my $factorial = 1;

    if ($parm == 0)
    {
        return $factorial;
    }

    foreach my $i (1..$parm) {
        $factorial *= $i;
    }

    return $factorial;
}

1;

我的factorial.t

#!/usr/bin/perl -w
use Test::More;

use factorial 'factorial';
is(factorial(0), 1, "boundary case");
is(factorial(1), 1, "factorial(1)");
is(factorial(2), 2, "factorial(2)");
is(factorial(6), 720, "factorial(6)");

done_testing;

我通过运行这个程序验证了我的factorial模块工作正常(或者,至少,正确地做一件事)。

#!/usr/bin/perl -w

use factorial 'factorial';

my $parm = 5;
print "Factorial($parm) = ", factorial($parm), "\n";
4

1 回答 1

6

手册页prove声明它接受选项和文件或目录:

prove [options] [files or directories]

它不会为您做任何名称扩展。

你给出prove了一个参数factorial,而我怀疑你的测试文件被命名为factorial.t

以下prove命令应该都是有效的:

prove factorial.t
prove fac*
prove <directory that contains factorial.t>

注意:最后两个命令可以运行多个测试,具体取决于工作目录的内容,或者您​​传递到的目录prove

于 2015-03-30T17:36:12.803 回答