Skip to Content
Perl Testing: A Developer's Notebook
book

Perl Testing: A Developer's Notebook

by Ian Langworth, Chromatic
July 2005
Intermediate to advanced
200 pages
4h 31m
English
O'Reilly Media, Inc.
Content preview from Perl Testing: A Developer's Notebook

Chapter 8. Unit Testing with Test::Class

If you have experience in other object-oriented languages, you may have used unit testing to develop your test cases and test suites. Object-oriented unit testingframeworks are more popular with programming languages such as C# and Java, while the majority of Perl tests are procedural. This isn’t to say that one style is better than the other—the choice between styles depends on the goal and structure of your software.

Test::Class is a powerful testing library that allows you to design your tests in the xUnit style. Tests using Test::Class are classes, not just simple test files. This is more complicated to start, but it allows you to organize test cases more easily as well as minimize repetitive testing code, especially for heavily object-oriented projects.

This chapter demonstrates how to write unit testing code in Perl with Test::Class to take advantage of its benefits, including fixtures and inheritance.

Writing Test Cases

Consider a Queue object that stores items to access in first-in, first-out order. Queue allows you to enqueue and dequeue items, returning them in insertion order. You can query a Queue for how many items it contains. Sure, it’s simple enough to do this with Perl’s basic data structures, but the complexity of Queue could grow quickly as its uses supersede what a normal array provides.

This lab demonstrates how to test Queue by creating a module that subclasses Test::Class.

How do I do that?

Create a directory Queue/ and save ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Debugged

Perl Debugged

Peter Scott, Ed Wright
Pro Perl

Pro Perl

Peter Wainwright
Perl Hacks

Perl Hacks

Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe

Publisher Resources

ISBN: 0596100922Supplemental ContentErrata Page