≡ Menu

Perl Testing with Test::More use_ok, can_ok, is, isnt, like, unlike Functions

In our previous test automation article, we learnt about how to write testcases for a simple perl script using Test::Simple and prove command.

In this tutorial, we’ll explore advanced perl testing functions that are part of Test::More perl module.

For those who are new to perl testing, start with this perl testing introduction article.

For this tutorial, we’ll use the following perl script example. The below perl module ExecCmd contains three member functions that executes a command, displays the output, and checks the existence of command.

package ExecCmd;

use Capture::Tiny ':all';
use Exporter;

@ISA = qw(Exporter);
@EXPORT = qw(execute_cmd is_exist);
@EXPORT_OK = qw(display_output);

sub is_exist
        my $file = shift;
        return 1 if ( -f $file );

        return 0;
sub execute_cmd
        my $cmd = shift;
        return unless $cmd;

        ($stdout, $stderr) = tee { system ($cmd) };

        return ( $stdout );

sub display_output
        my ( $cmd, $stdout, $stderr ) = @_;

        print "$cmd - output :\n$stdout\n";
        print "$cmd - error  :\n$stderr\n";

1. Test loading status of a Perl module – use_ok()

You might want to check the loading status of a perl module to confirm that the module is getting loaded without any issues. use_ok() is used to test loading of perl modules. when its able to load without any issue, it returns success otherwise failure.

use_ok ( 'ExecCmd' );

The corresponding line to be tested in perl script is:

use ExecCmd;

You can pass an optional argument to test the import symbols while loading a modules:

use_ok ( 'ExecCmd', 'display_output' );

The corresponding line to be tested in perl script is:

use ExecCmd qw(display_output);

2. Test perl module’s member – can_ok()

In order to test members of a module, i.e to confirm that it can be accessed via module’s namespace or by module object, use can_ok() as shown below:

#can_ok() - to test that the subroutines can be called using module's namespace
can_ok ( 'ExecCmd', 'execute_cmd' );

#can_ok() - if its exported then it would be available in main namespace. 
can_ok ( __PACKAGE__, 'execute_cmd' );

The above test will pass if execute_cmd() function is accessible via module namespace ExecCmd or main namespace.

It corresponds to the following line in the perl script:

#accessing via module's namespace

#if exported to main namespace,

In case if you are accessing members via module objects, then replace module name argument with module objects as shown below:

can_ok ( $object, 'execute_cmd' );

And the corresponding perl script line would be:


3. Test Perl module’s object – isa_ok()

It is possible to check whether the created perl module object is either valid or not. isa_ok() confirm that the object is a defined reference and the object belongs to the respective module mentioned in the second argument.

isa_ok($object, 'ExecCmd');

As shown above, it checks that $object is a defined reference of ExecCmd module.

The following test will fail, as the module name is not correct.

isa_ok($object, 'ExeCmd');

4. Test a matching expression – is() and isnt()

To compare two arguments Test::Simple provides ok().

Similarly, Test::More has a advanced function called is() and isnt() to compare arguments with ‘eq’ and ‘ne’ respectively.

is(is_exist("/bin/df"), 1, "Test the existence of /bin/df");

The above checks the return status of is_exist() to be 1, otherwise it fails.

ok 6 - Test the existencce of file /bin/df

When the test fails, it produces the diagnostics output so that user can understand and figure out what went wrong:

not ok 6 - Test the existence of /bin/df
#   Failed test 'Test the existence of /bin/df'
#   at ./test.t line 18.
#          got: '0'
#     expected: '1'

As shown above, it would display exactly what it “got” and is the “expected” value.

There is a negative function called isnt() to compare the given two arguments with ‘ne’ as shown below,

isnt(is_exist("/bin/wine"), 1, "Test the non-existencce of file /bin/wine");

The above test will pass if is_exist() returns the value except 1. The output is shown below:

ok 7 - Test the non-existencce of file /bin/wine

5. Test a regular expresssion – like() and unlike()

Test::More also provides functions like() and unlike() to match the arguments with regular expressions.

In the following example, like() is used to check for the existence of mount of a partition in df command output,

like (execute_cmd("df"), qr|/dev/sda7|, "Test the partition /dev/sda7 is mounted");

As shown above, the tests is to check that the pattern “/dev/sda7” exists in the output returned by execute_cmd() function and the output is shown below:

ok 8 - Test the partition /dev/sda7 is mounted

For reference, the df command output is shown below:

Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sda5      195069136 178327852   6975704  97% /
udev             2008336         4   2008332   1% /dev
tmpfs             806244       880    805364   1% /run
none                5120         0      5120   0% /run/lock
none             2015604       620   2014984   1% /run/shm
/dev/sda6       17843160   5893944  11053936  35% /mydata
/dev/sda7       11707200    573312  11133888   5% /backup

There is also a negative function called unlike() to check the expected pattern (second argument) is unmatched with the got value (first argument).

In the following example, it checks in the df output that there should be any partition that is 100% full.

unlike (execute_cmd("df"), qr|100%|, "Test that no partition is 100% full");

It checks and returns true when no partition is 100% full.

ok 8 - Test that no partition is 100% full
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.

  • Bob August 9, 2013, 8:54 am

    thanks, learnt something new today

  • Stan August 9, 2013, 9:34 pm

    I’ve seen this perl testing output when I install any perl modules from cpan.

    Now I know how that is getting done. Probably I can try to do my own test script for my custom perl modules. Thanks.

  • s June 24, 2015, 8:18 am

    A very confusing tutorial for someone new to perl . I would prefer if you put the whole test file instead of letting us guess the missing lines.