O'Reilly logo

Perl Testing: A Developer's Notebook by Chromatic, Ian Langworth

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required