testing beginner

How to use prove command to run Perl tests?

Question

How to use prove command to run Perl tests?

The prove command is a shell utility for running Perl test scripts and interpreting their output using the Test Anything Protocol (TAP). It is not a Perl function or module you invoke inside a script. Instead, you use prove from your command line to execute .t test files.

What is the prove command?

prove is a core command-line tool that runs one or more Perl test scripts, parses their TAP output, and summarizes the results. Typically, you write test scripts using core testing modules like Test::Simple or Test::More. prove then runs these scripts and provides a concise report of passed and failed tests.

How to use prove

Follow these steps:

  • Write a test script named example.t using a test module—for example, Test::Simple.
  • From your shell prompt, run prove example.t to execute the tests.
  • prove runs the script, captures TAP output, and prints a summary.

You cannot use prove inside a Perl script like a function call—it will cause errors.

Minimal runnable test script example

Here is a minimal test script you can save as example.t and run using prove. It prints TAP-compliant output verified by prove.

#! /usr/bin/env perl
use strict;
use warnings;
use Test::Simple tests => 3;

ok(1 == 1, "One equals one");
ok(2 + 2 == 4, "Basic math works");
ok('Perl' =~ /Perl/, "Regex match works");

warn "Completed 3 tests\n"; # harmless STDERR output, won't confuse prove

To run this:

  • Save as example.t
  • Make executable: chmod +x example.t
  • Run tests: prove example.t

Key points and common pitfalls

  • prove is external to Perl scripts: It cannot be called inside Perl code.
  • Test files must emit valid TAP output: Use Test::Simple or Test::More to generate correct test plans and results.
  • Minimize printing to STDOUT in tests: Arbitrary print statements may confuse TAP parsers. Use warn or STDERR for debug messages.
  • Specify test plans explicitly: tests => N ensures prove can confirm all tests ran.

Summary

Write Perl test scripts using core test modules and run them with the shell command prove. Do not attempt to call prove inside Perl scripts. The example above is a minimal valid test script to get started.

Runnable Perl code example

#! /usr/bin/env perl
use strict;
use warnings;
use Test::Simple tests => 3;

ok(1 == 1, "One equals one");
ok(2 + 2 == 4, "Basic math works");
ok('Perl' =~ /Perl/, "Regex match works");

warn "Completed 3 tests\n";  # harmless debug output to STDERR

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 51ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
1..3
ok 1 - One equals one
ok 2 - Basic math works
ok 3 - Regex match works
STDERR
Completed 3 tests

Was this helpful?

Related Questions