≡ Menu

Introduction to Perl Testing Using Test::Simple and Prove

Testing is an important part of SDLC activity. By automating the testing process, you can save considerable amount of time spent on testing. There are several modules available in Perl to automate testing process.

In this introduction article, let us discuss about how to write basic and simple test cases using Test::Simple module in perl.

1. Write Test Cases with Test::Simple

You can write test cases in perl using the perl Test::Simple module which has the basic functionality for testing.

Before proceed to writing test case, first you need to plan the number of test cases and then the actual test cases.

#!/usr/bin/perl

use strict;
use warnings;

use Test::Simple tests => 2;

sub hello_world
{
return "Hello world!";
}

sub get_number
{
return int(rand(1000));
}
ok( hello_world( ) eq "Hello world!", "My Testcase 1" );
ok( get_number( ) > 0, "My Testcase 2" );

As shown in the above sample code-snippet, its a simple program that has two functions; one to print a statement, and another to return random number within 1000.

The below line in above program, imports the module Test::Simple and sets the number of test cases planned as 2.

use Test::Simple tests => 2;

Ok() is the function to test a given test case. The general syntax of this function is:

ok( actual-testcase, testcase-name );

In the above program, the following two test cases are defined using the ok function:

ok( hello_world( ) eq "Hello world!", "My Testcase 1" );
ok( get_number( ) > 0, "My Testcase 2" );

Let us take the first one, it calls the function(i.e:hello_world()) and matches the return value with a string(i.e: “Hello world!”). On success, it says “ok 1” otherwise “not ok 1” along with the test case name (i.e: My Testcase 1).

Similarly the second one, it calls the function get_number() and checks the return value is greater than 0. On success, it says “ok 2” otherwise “not ok 2” along with the test case name (i.e: My Testcase 2).

2. Execute the test program with exit status

The standard extension for a test case file is .t. However you can also keep .pl as an extension. In the example program, we just had both original code and test cases together in one single file and named as my_helloworld_test.t.

Execute the test case program as show below:

$ perl my_helloworld_test.t
1..2
ok 1 - My Testcase 1
ok 2 - My Testcase 2
$ echo $?
0

The above got executed successfully with the exit code as 0. The first line of output displays (i.e: 1..2 ) the number of test cases that will be executed, and the rest of the output displays the test case execution details of each test cases.

If both the test cases failed, the output will be as shown below:

$ perl my_helloworld_test.t
1..2
not ok 1 - My Testcase 1
# Failed test 'My Testcase 1'
# at my_helloworld_test.t line 29.
not ok 2 - My Testcase 2
# Failed test 'My Testcase 2'
# at my_helloworld_test.t line 30.
# Looks like you failed 2 tests of 2.
$ echo $?
2

In the above output, both test cases failed with the exit code as 2. From the exit code, you would be able to know the number of failed test cases. However you can not rely on it when the failures are more than 254.

When a test dies for some reason, the output will be as shown below:

$ perl my_helloworld_test.t
1..2
Undefined subroutine &main::hello_world1 called at my_helloworld_test.t line 29.
# Looks like your test exited with 255 before it could output anything.
$ echo $?
255

In the above output, it died due to undefined subroutine call (i.e: hello_world1()), but the actual name of subroutine is hello_world().

3. Run testcases with prove command

App::Prove implements the prove command. Prove command will display test summary for the test cases executed.

Now, execute my_helloworld_test.t program using prove command as shown below:

$ prove my_helloworld_test.t
hello_world.t .. ok
All tests successful.
Files=1, Tests=2, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.01 cusr 0.00 csys = 0.02 CPU)
Result: PASS
$ echo $?
0

The following is the output when the test cases fails:

$ prove my_helloworld_test.t
my_helloworld_test.t .. 1/2
# Failed test 'My Testcase 1'
# at my_helloworld_test.t line 29.

# Failed test 'My Testcase 2'
# at my_helloworld_test.t line 30.
# Looks like you failed 2 tests of 2.
my_helloworld_test.t .. Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/2 subtests

Test Summary Report
-------------------
my_helloworld_test.t (Wstat: 512 Tests: 2 Failed: 2)
Failed tests: 1-2
Non-zero exit status: 2
Files=1, Tests=2, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.01 cusr 0.00 csys = 0.02 CPU)
Result: FAIL

$ echo $?
1

The exit code of prove tells you only the success or failure of the execution.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Bhagyaraj February 5, 2013, 5:00 am

    Nice one,
    Please do consider Windows OS while running the test cases.
    It’s working on Windows 5.14 as well.
    Kindly provide more tutorials on perl like this.
    It will be interesting if you provide the articles on Webpages using Perl a well.
    Lerning Perl is easy.

  • Ben February 27, 2014, 9:52 pm

    Can you please elaborate more to understand it with some live system admin examples?

  • srikanth January 19, 2015, 5:40 am

    Hi Sir…..
    i’m working as a STORAGE MANUAL TESTING Engineering….
    i wn’t to upgrade my self in the “AUTOMATION” in STORAGE…
    i done with my basics of perl…..
    can you please let me know the books that AUTOMATES to fail the devices(HARD DISK, CONTROLLER etc)….
    plz do me a fewer sir…..
    THANKU SIR……:):)