Testing with Test::More

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");

perl t/basic.t

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

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.

use strict; use warnings; use Test::More tests => 4; is_deeply([1, 2], [1, 2], "lists match"); cmp_ok(10, '>', 5, "bigger"); isnt(1, 2, "different"); unlike("clean", qr/error/, "no errors");

perl t/asserts.t

1..4
ok 1 - lists match
ok 2 - bigger
ok 3 - different
ok 4 - 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.

use strict; use warnings; use Test::More tests => 1; eval { die "boom\n" }; like($@, qr/boom/, "dies with boom");

perl t/dies.t

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

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the plan line 1..3 in a test file mean?

    Answer

    The number of tests the file will run

    The plan is the promise of how many checks follow, and the runner treats a different count as a failure.

  2. What does done_testing() at the end of a test file do?

    Answer

    It prints the plan at the end, so you need not count the tests first

    The file shows the plan after the last check, which is why the count need not be written at the top.

  3. Which assertion compares two nested data structures?

    Answer

    is_deeply

    is_deeply walks both structures and shows the first place where they differ.

  4. Which assertion checks that 10 is greater than 5?

    Answer

    cmp_ok(10, '>', 5, "bigger")

    cmp_ok takes the operator as an argument and prints it in the failure message.

  5. What does this test file print? use Test::More tests => 2; is(2 + 2, 4, "sum"); isnt(3, 4, "not equal");

    Answer

    1..2 / ok 1 - sum / ok 2 - not equal

    The plan comes first with tests => 2, and both comparisons pass.

  6. What does this test file print? use Test::More tests => 1; like("error 42", qr/\d+/, "digits");

    Answer

    1..1 / ok 1 - digits

    The string holds digits, so the pattern matches and the check passes.

  7. What does this test file print? use Test::More tests => 1; cmp_ok(10, '>', 5, "bigger");

    Answer

    1..1 / ok 1 - bigger

    The comparison holds, and the plan line is printed first because the count is in the import.

  8. What does this test file print? use Test::More; ok(1, "one"); ok(1, "two"); done_testing();

    Answer

    ok 1 - one / ok 2 - two / 1..2

    Without a plan in the import, the checks print first and done_testing writes the count at the end.

  9. A test file declares tests => 3 but only two checks run. What happens?

    Answer

    The file fails with a message that it planned 3 tests but ran 2

    The plan is compared with the number of checks, and a mismatch is a failure of the file.

  10. Why is is($got, $want, "sum") better than ok($got == $want, "sum")?

    Answer

    It prints both values when the check fails

    The specific assertion shows the expected and the received value, which a bare ok cannot do.

Article author: Arthur Isaev