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.tusing a test module—for example,Test::Simple. - From your shell prompt, run
prove example.tto execute the tests. proveruns 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
proveis external to Perl scripts: It cannot be called inside Perl code.- Test files must emit valid TAP output: Use
Test::SimpleorTest::Moreto generate correct test plans and results. - Minimize printing to STDOUT in tests: Arbitrary print statements may confuse TAP parsers. Use
warnor STDERR for debug messages. - Specify test plans explicitly:
tests => Nensuresprovecan 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
1..3
ok 1 - One equals one
ok 2 - Basic math works
ok 3 - Regex match works
Completed 3 tests
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 test database code with Test::mysqld in Perl?
- How to use Test::Fatal for exception testing in Perl?
- How to test regular expressions with like in Perl Test::More?