Chapter 14

Testing

WHAT YOU WILL LEARN IN THIS CHAPTER:

  • Understanding basic tests
  • Learning the Test::More module in depth
  • Using different testing modules
  • Understanding xUnit style testing

WROX.COM CODE DOWNLOAD FOR THIS CHAPTER

The wrox.com code downloads for this chapter are found at http://www.wrox.com/remtitle.cgi?isbn=1118013847 on the Download Code tab. The code for this chapter is divided into the following major examples:

  • lib/TestMe.pm
  • t/testit.t.pm
  • lib/TestQuery.pm
  • t/query.t
  • lib/Person.pm
  • t/test_classes.t
  • t/test_classes.t
  • t/lib/TestsFor/Person.pm
  • lib/Customer.pm
  • t/lib/TestsFor/Customer.pm
  • lib/TV/Episode.pm

The author recently downloaded and built Perl version 5.15.9. All 521,047 tests passed. That’s right: Perl ships with more than a half-million tests. Not all of them are perfect, and sometimes they verify that a feature works the way it was written and not the way that was intended, but it’s still an incredible number. Few programming languages ship with more than a half-million tests. Testing your software is easy, and the Perl community is keen on testing. A few years ago, some people still argued against testing, but today professional Perl developers test more often than not.

BASIC TESTS

Consider the following line of code:

sub reciprocal { return 1 / shift }

Because the reciprocal of a number is 1 divided by that number, this looks like the canonical definition of a reciprocal. What could possibly go wrong?

When developers write tests, they often write a test ...

Get Beginning Perl now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.