Testing a UNIX system with Perl the Cucumber way

April 10, 2013

I’ve found that using systems like cucumber to be quite handy when testing software, and I was hoping for a lightweight Perl solution that I could run on my UNIX systems without having to install a bunch of dependancies.

Enter testadmin.pl.

The files are stored in this github gist with a couple of simple examples. One tests if a user is logged in:

As a silly system administrator
When I run who
Then user pblair must be logged in

In order to satisfy this test, we look in w.step

our @output;

When qr{I run (\S.*)} => sub {
        my ($regex, $capture) = @_;
        @output = `$capture->[0]`;
        chomp for @output;
Then qr{user (\S+) must be logged in} => sub {
    my ($regex, $capture) = @_;
    my $user = $capture->[0];
    my @l = grep { /^$user/ } @output;
    die if scalar @l == 0;
    scalar @l;

We see that it captures the name of a user (supplied by the feature file) and checks the @output array for the given username. It returns the number of found users. The calling function will return a warning if we return 0, so as long as there’s 1 user, then this function will cause the hilighting of the definition file to be displayed as green.

Discussion, links, and tweets

Follow me on Twitter