spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / awperl / 1 To page 1current pageTo page 3To page 4
[previous][next]

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


Perl Medic: Transforming Legacy Code. Chapter 3. Pt. 1

3.2.1 The Test Module

Test.pm was added in version 5.004 of Perl. By the time Perl 5.6.1 was released it was superseded by the Test::Simple module, which was published to CPAN and included in the Perl 5.8.0 core. Use Test::Simple instead.

1..5  
ok 1 - Can make a frobnitz
ok 2 - Can fliggle the frobnitz  
not ok 3 - Can grikkle the frobnitz  
ok 4 - delete the frobnitz t use  
Can ok 5 - Can 'a deleted frobnitz

If you inherit regression tests written to use Test.pm, it is still included in the Perl core for backward compatibility. You should be able to replace its use with Test::Simple if you want to start modernizing the tests.

3.2.2 The Test::Simple Module

When I say “simple,” I mean simple. Test::Simple exports precisely one function, ok(). It takes one mandatory argument, and one optional argument. If its first argument evaluates to true, it prints “ok”; otherwise it prints “not ok”. In each case it adds a number that starts at one and increases by one for each call to ok(). If a second argument is given, ok() then prints a dash and that argu¬ment, which is just a way of annotating a test.

Doesn’t exactly sound like rocket science, does it? But on such a humble foundation is the entire Perl regression test suite built. The only other requirement is that we know how many tests we expected to run so we can tell if something caused them to terminate prematurely. That is done by an argument to the use statement:

use Test::Simple tests => 5;

The output from a test run therefore looks like:
Note that the first line says how many tests are expected to follow. That makes life easier for code like Test::Harness (see Section 3.2.9) that reads this output in order to summarize it.

3.2.3 The Test::More Module

You knew there couldn’t be a module called Test::Simple unless there was something more complicated, right? Here it is. This is the module you’ll use for virtually all your testing. It exports many useful functions aside from the same ok() as Test::Simple. Some of the most useful ones are:

is($expression, $value, $description)
Same as ok($expression eq $value, $description). So why bother? Because is() can give you better diagnostics when it fails.

like($attribute, qr/regex/, $description)
Tests whether $attribute matches the given regular expression.

is_deeply($struct1, $struct2, $description)
Tests whether data structures match. Follows references in each and prints out the first discrepancy it finds, if any. Note that it does not compare the packages that any components may be blessed into.

isa_ok($object, $class)
Tests whether an object is a member of, or inherits from, a particular class.

can_ok($object_or_class, @methods)
Tests whether an object or a class can perform each of the methods listed.

use_ok($module, @imports)
Tests whether a module can be loaded (if it contains a syntax error, for instance, this will fail). Wrap this test in a BEGIN block to ensure it is run at compile time, viz: BEGIN {use_ok("My::Module")}

There’s much more. See the Test::More documentation. I won’t be using any other functions in this chapter, though.

Caveat: I don’t know why you might do this, but if you fork() inside the test script, don’t run tests from child processes. They won’t be recognized by the parent process where the test analyzer is running.

3.2.4 The Test::Exception Module

No, there’s no module called Test::EvenMore.2 But there is a module you’ll have to get from CPAN that can test for whether code lives or dies: Test::Exception. It exports these handy functions:

lives_ok()
Passes if code does not die. The first argument is the block of code, the second is an optional tag string. Note there is no comma between those arguments (this is a feature of Perl’s prototyping mechanism when a code block is the first argument to a subroutine). For example:

  lives_ok { risky_function() } "risky_function lives!";
dies_ok()
Passes if the code does die. Use this to check that error-checking code is operating properly. For example:

  dies_ok { $] / 0 } "division by zero dies!"; throws_ok()
For when you want to check the actual text of the exception. For example:

throws_ok { some_web_function() } qr/URL not found/, "Nonexistent page get fails";

The second argument is a regular expression that the exception thrown by the code block in the first argument is tested against. If the match succeeds, so does the test. The optional third argument is the comment tag for the test. Note that there is a comma between the second and third arguments.

2. Yet I once promised Mike Schwern a beer if he could come up with an excuse to combine the UNIVERSAL class and an export functionality into UNIVERSAL::exports as a covert tribute to James Bond. He did it. Schwern, I still owe you that beer . . . .


home / programming / awperl / 1 To page 1current pageTo page 3To page 4
[previous][next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: March 24, 2004

URL: http://webreference.com/programming/awperl/1