[Israel.pm] Testing your CPAN Module

Yuval Kogman lists at woobling.org
Thu Jul 1 10:17:02 PDT 2004


On Thu, Jul 01, 2004 at 16:32:01 +0300, Shlomo Yona wrote:

> What easy to use tools are available to construct tests in
> Perl?

Test::Harness eats very simple input - anything that makes it is usually
as straightforward - see Test::Simple, Test::More.

> Here are some examples for things I'd like to test: * That all
> dependencies are available (how would I define them at the first
> place? How can I make sure they exist?)

Makefile.PM and Build.PM have this data in them, and ExtUtils::MakeMaker
and Module::Build can both probe for dependancies.

Otherwise Test::Simple (i think, could be Test::More) has a
use_ok("Module::Name"); that you could use.

> * How can I change the behaviour of the test suite depending on
> available prerequisites?

Test::Without::Module can be used to hide modules and prevent them from
loading - this can be used effectively with

	eval {
		require Module;

	if ($@){
		print "Loaded";
	} else {
		print "Punt";

> * How do I "test my tests"?

XP techniques do this, or you could mock failing tests, and so forth.

I very much like the approach of failing your tests before passing them.
If they are simple enough, however, the dependancy of test and function
on one another should provide ample verification.

Mock objects (Test::MockObject) have a nice way of faking a 'session' of
manipulations on an object that is up to spec, so if they pass on the
mocks (Should you choose to do that), but fail on the real, your mock
and real are out of synch. If they fail on the mock, then you should
verify your mock, or it could be a bug in testing.

Unit testing in general is fine grained enough that this normally
doesn't happen.

> Take for example, the module Lingua::HE::Sentence, which
> I'll demonstrate today at the lecture. The module takes a
> UTF-8 encoded Hebrew text and segments it into sentences
> according to some heuristics. So, I figure that the
> following things are something worth testing:
> * Perl version above some predetermined number

	use 5.008; # perl 5.8 or more
	use 5.8.0; # this check only available since 5.6.0, methinks

but really let the Makefile.PL take care of this.

> * take a text file (which is part of the distribution) and
> have the module segment it and then check for each special
> case if the segmentation was correct.

I  put the data for test files in the __DATA__ section - it's a bit
cleaner, and an intuitive way of associating a test with it's data.

> There seem to be quite a lot of testing modules out there.
> Which one is good for which task?

Test::More is probably the way to start - it's good, logical, and well
tested. The rest should pop up when you start needing them ;-)

 ()  Yuval Kogman <nothingmuch at woobling.org> 0xEBD27418  perl hacker &
 /\  kung foo master: /me sneaks up from another MIME part: neeyah!!!!!

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
Url : http://perl.org.il/pipermail/perl/attachments/20040701/26c20f9f/attachment.pgp 

More information about the Perl mailing list