Testing with Test::More

Contents
The first test file
Assertions
Run with prove
Test layout

The first test file

Test::More ships with Perl, so a test suite needs no install. A test file is a normal Perl program that prints one line per check and finishes with a plan.

t/basic.t

use strict; use warnings; use Test::More tests => 3; is(2 + 2, 4, "addition works"); ok(1, "truth is true"); like("error 42", qr/\d+/, "finds digits");

ok 1 - addition works ok 2 - truth is true ok 3 - finds digits 1..3

The plan at the top states how many tests will run. done_testing() at the bottom is the modern alternative when you do not want to count.

Assertions

Test::More has a function for every common comparison, and each one prints a useful diff on failure. Pick the most specific function available instead of wrapping everything in ok.

is_deeply([1, 2], [1, 2], "lists match"); cmp_ok(10, '>', 5, "bigger"); isnt(1, 2, "different"); unlike("clean", qr/error/, "no errors");

For code that should die, use a block plus like on $@. For code that should not, wrap it and fail if $@ is set.

eval { die "boom\n" }; like($@, qr/boom/, "dies with boom");

Run with prove

prove is the standard runner. Give it the folder and it finds every .t file, runs each in its own process, and prints one summary line per file.

prove -l t/

t/basic.t .. ok All tests successful. Files=1, Tests=3, 0 wallclock secs

The -l flag adds the lib folder to the search path, so tests can load the module under test without an install step. Add -v when a single test needs to explain itself.

Test layout

The layout that CPAN expects works for private projects too. One module, one test file, named after the module with the same path inside t.

project/ ├── lib/My/Text.pm ├── t/basic.t ├── cpanfile └── Makefile.PL

When a test fails, run that file alone with perl and read the diff. The failure message names the expected and the received value, which is usually enough to find the bug without a debugger.

Next: strict, warnings and diagnostics

Article author: Arthur Isaev

Related articles
Perl
Perl course: zero to hero in 50 lessons
What is Perl and where it runs
Install Perl and check the version
Your first Perl script
The debugger as a REPL
Scalars and sigils
Numbers
Strings
Interpolation and quoting
Arrays
push, pop, shift and splice
Hashes
keys, values, exists and delete
Scalar and list context
Conditions and comparison
while, for, next and last
foreach and the default variable
Subroutines
Arguments in @_
References
Nested data structures
Regular expressions: match and anchors
Captures and named groups
Substitution with s///
split and join
grep and map
Sort and the Schwartzian transform
Read and write files
File tests and metadata
Directories and paths
die, warn and eval
Modules and Exporter
CPAN and cpanm
Objects with bless
Modern objects with Moo
Testing with Test::More
strict, warnings and diagnostics
Options with Getopt::Long
Environment and %ENV
system, backticks and pipes
One-liners with -n and -p
CSV and TSV processing
JSON with JSON::PP
HTTP clients with HTTP::Tiny
Sockets and IO::Socket
Databases with DBI
Capstone: todo CLI
Capstone: log report
Performance and profiling
Modern Perl tour
Hero roadmap: the whole course on one page
Install Perl on Windows 11
Install Perl on Ubuntu
Install Perl on Rocky Linux
Install Perl on macOS
Install Perl on FreeBSD
Perl in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: