Chapter 18. Advanced Testing

The Test::More module provides some simple and general functions, but other Test::* modules provide more specific tests for particular problem domains so that we don’t have to write much code to do what we need. If we use it once, we’ll probably use it again, anyway.

In this chapter, we give you a taste of some of the more popular test modules. Unless we say otherwise, these modules are not part of the Perl standard distribution (unlike Test::More) and you’ll need to install them yourself. You might feel a bit cheated by this chapter since we’re going to say “See the module documentation” quite a bit, but we’re gently nudging you out into the Perl world. For much more detail, you can also check out Perl Testing: A Developer’s Notebook, which covers the subject further.

Testing Large Strings

We showed in Chapter 17 that when a test fails, Test::More can show us what we expected and what we actually got.

#!/usr/bin/perl
use Test::More 'no_plan';
is( "Hello Perl", "Hello perl" );

When I run this program, Test::More shows me what went wrong.

$ perl test.pl
not ok 1
#     Failed test (test.pl at line 5)
#          got: 'Hello Perl'
#     expected: 'Hello perl'
1..1
# Looks like you failed 1 test of 1.

What if that string is really long? We don’t want to see the whole string, which might be hundreds or thousands of characters long. We just want to see where they start to be different.

#!/usr/bin/perl use Test::More 'no_plan'; use Test::LongString; is_string( "The quick brown fox ...

Get Intermediate Perl now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.