Skip to main content

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.

Comments

Popular posts from this blog

20 perl programming tips

1. List all Installed Perl Modules from Unix Command Line Get a list of all installed perl modules as shown below. $ perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC' /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/Filter.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/LinkExtor.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/PullParser.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/Parser.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/TokeParser.pm ..... In the above example, File::Find  and  File::Spec::Functions  module are used to list all installed modules. -M option  loads the module. It executes  use module  before executing the script -T option  enables taint checking, which instructs perl to keep track of data from the user and avoid doing anything insecure w...

Perl and Excel: How to Read, Write, Parse Excel Files using Perl

If you want to manipulate excel files programmatically, you can use Perl Spreadsheet module, which provides an object interface that makes it easier to create and parse Excel files. Install Spreadsheet WriteExcel Module Method 1: Standard install using make Download the zipped tar file of  Spreadsheet-ParseExcel  and  Spreadsheet-WriteExcel  from cpan. Untar and unzip the module as follows: tar -zxvf Spreadsheet-WriteExcel.tar.gz cd to the directory the tar creates. Execute the steps below to to install the Spreadsheet-WriteExcel module. perl Makefile.PL make make test make install Use the above procedure to install Spreadsheet-ParseExcel also. Method 2: CPAN.pm install If you have CPAN.pm configured you can install the module as follows: perl -MCPAN -e 'install "Spreadsheet::WriteExcel"' perl -MCPAN -e 'install "Spreadsheet::ParseExcel"' For additional installation help refer to: How To Install Perl Modules Manually and Using ...

How to Connect to MySQL from Perl

How do I connect to a MySQL database from a perl program? use perl DBI module to connect to a MySQL database as explained below. If you don’t have perl DBI and DBD::mysql module installed, install perl module using cpan as we discussed earlier. # perl -MCPAN -e shell cpan> install DBI cpan> install DBD::mysql On a very high level, you’ll have to do the following three steps to connect to a MySQL database and get data. 1. Connect to the MySQL Database In the DBI module, you’ll use the connect function as shown below. $dbc = DBI->connect($source, $username, $password) DBI->connect function takes the following three arguments: $source – This is in the format of “DBI:mysql:[database]:[hostname]”. Replace the [database] and [hostname] with values from your system. In the example shown below, it is connecting to the database called “mycompany” that is running on the localhost. $username – The username that is used to connect to the MySQL database. $pass...