Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Why and What to Test

Why and What to Test

In this lesson — part of Testing Fundamentals — you'll learn why and what to test in Perl and why it matters in real work.

Why it matters

Tests prove your code works and keep it working as you change it.

Key ideas

  • What to test
  • Arrange-Act-Assert
  • Running a test suite
  • Good vs. brittle tests

In practice

Here's how it looks in idiomatic Perl:

use strict; use warnings;
use Test::More;                      # core testing framework
sub add { $_[0] + $_[1] }
is(add(2, 3), 5, 'adds integers');
ok(add(-1, 1) == 0, 'cancels out');
like('error: 42', qr/^error/, 'message prefix');
is_deeply([sort 3,1,2], [1,2,3], 'sorts list');
done_testing();                      # no need to pre-count

Perl note: Test::More emits the TAP protocol consumed by prove; prefer done_testing() over a hard-coded tests => N plan, and use is_deeply for deep structure comparison rather than stringifying references.

Try it yourself

Exercise: In Perl, write three tests for a function that reverses a string.

Recap

You now understand why and what to test and can apply it in Perl. Mark this lesson complete and continue to the next one.