Perl Testing 读书笔记

上一篇 / 下一篇  2012-04-03 14:30:02 / 个人分类:perl相关

 

Perl Testing 读书笔记

装载模块,测试模块

要测试如下模块代码,AnalyzeSentence.pm

package AnalyzeSentence;

use strict;
use warnings;

use base 'Exporter';

our $WORD_SEPARATOR = qr/\s+/;
our @EXPORT_OK = qw( $WORD_SEPARATOR count_words words );

sub words {
 my $sentence = shift;
 return split( $WORD_SEPARATOR, $sentence );
}

sub count_words {
 my $sentence = shift;
 return scalar words( $sentence );
}

1;

测试脚本如下,analyze_sentence.pl

#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 5;

my @subs = qw( words count_words );

use_ok( 'AnalyzeSentence', @subs );
can_ok( __PACKAGE__, 'words' );
can_ok( __PACKAGE__, 'count_words' );

my $sentence = 'Queen Esther, ruler of the Frop-Human Alliance, briskly devours a monumental
                 ice cream sundae in her honor.';

my @words = words( $sentence );
ok( @words == 17, 'words() should return all words in sentences' );

$sentence = 'Rampaging ideas flutter greedily. ';
my $count = count_words( $sentence );

ok( $count == 4, 'count_words() should handle simple sentences' );

运行结果如下

D:\loveperl>perl analyze_sentence.pl
1..5
ok 1 - use AnalyzeSentence;
ok 2 - main->can('words')
ok 3 - main->can('count_words')
ok 4 - words() should return all words in sentences
ok 5 - count_words() should handle simple sentences

D:\loveperl>prove analyze_sentence.pl
analyze_sentence....ok
All tests successful.
Files=1, Tests=5,  0 wallclock secs ( 0.00 cusr +  0.00 csys =  0.00 CPU)

 

测试脚本做了5项测试,use_ok(),判断加载模块和符号列表是否正常,2个can_ok()用来确认判断是否将被测模块中的函数导出到当前的namespace,

剩下的2个ok,检测模块中的函数输出是否正常。


TAG:

 

评分:0

我来说两句

Open Toolbar