How to write basic tests with Test::More in Perl?
Question
How to write basic tests with Test::More in Perl?
Introduction to Testing with Test::More in Perl
Writing tests is an essential part of developing reliable Perl code. The Test::More module is the most widely used testing framework in Perl, providing a simple yet powerful way to write unit tests and validate your code’s behavior.
At its core, Test::More lets you write tests as small assertions that compare expected results with actual outputs. It prints the test results in a standardized TAP (Test Anything Protocol) format, which can be processed by many test harnesses.
Key Concepts in Test::More
- Importing and Planning: You start by importing the module and defining the number of tests you expect to run using
plan. - Basic Tests: Use functions like
ok(),is(),is_deeply()to assert conditions. - Test Count: You must declare the total number of tests planned, or use
done_testing()to auto-detect. - Sigils: Scalars
$represent single values, arrays@hold lists, and hashes%allow key-value pairs. Tests commonly check scalar or list values. - TMTOWTDI: Perl’s "There’s More Than One Way To Do It" philosophy means you can write tests in different styles;
Test::Moreencourages consistency via its simple interface.
Basic Example: Writing a Simple Test Script
The following Perl code demonstrates a basic test script using Test::More. It plans for 4 tests that check simple mathematical operations and string comparison.
use strict;
use warnings;
use Test::More tests => 4;
# Test 1: Check if 2 + 2 equals 4
is(2 + 2, 4, '2 + 2 should equal 4');
# Test 2: Check if "Hello" matches "Hello"
is("Hello", "Hello", 'Strings match');
# Test 3: Using ok to check a Boolean condition
ok(5 > 3, 'Five is greater than three');
# Test 4: Comparing array references deeply
my @array1 = (1, 2, 3);
my @array2 = (1, 2, 3);
is_deeply(\@array1, \@array2, 'Arrays are equal');
# Output will indicate pass/fail for each test on STDOUT
How to Run This Test Script
Save the above code in a file named test_example.pl and execute it via the command line:
perl test_example.pl
You should see output similar to:
ok 1 - 2 + 2 should equal 4
ok 2 - Strings match
ok 3 - Five is greater than three
ok 4 - Arrays are equal
1..4
Common Gotchas and Tips
- Plan vs done_testing: Use
plan tests => Nif you know how many tests you will run upfront. Alternatively, usedone_testing()at the end if your test count is dynamic. - Context Matters: Functions like
is()compare scalars,is_deeply()compares complex structures like arrays/hashes deeply. - Use Descriptive Test Names: The third argument to
is(),ok(), etc., should describe what the test checks. This helps when tests fail. - Perl Version:
Test::Morehas been part of Perl core since 5.6.1; all features used here are fully supported in modern Perl versions (5.10+).
Summary
Test::More makes it easy to create clear, maintainable tests with familiar Perl semantics and idioms. Start small by checking scalars and simple conditions, then progress to complex data structures with functions like is_deeply(). Consistent use of testing will dramatically improve your code quality and confidence when changing or refactoring code.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 66ms
1..4
ok 1 - 2 + 2 should equal 4
ok 2 - Strings match
ok 3 - Five is greater than three
ok 4 - Arrays are equal
(empty)Was this helpful?
Related Questions
- How to use Test::Deep for complex data structure comparison?
- How to test Mojolicious applications in Perl?
- How to test HTTP responses with Test::WWW::Mechanize in Perl?
- How to use prove command to run Perl tests?
- How to test database code with Test::mysqld in Perl?
- How to use Test::Fatal for exception testing in Perl?