jkeenan1 has asked for the wisdom of the Perl Monks concerning the following question:

For a talk I am preparing on Perl testing, I would appreciate any *very* simple illustrations of how Perl testing tools such as Test::More are used to test code written in other languages.

Ideally, I would like an example of Test::More being used to test something written in shell or in C, as these are the cases which I think my audience will most quickly grasp. I haven't done enough in either of these languages myself to know what a good example would be.

Links or small code examples equally welcome.

Thanks in advance.

Jim Keenan

  • Comment on Examples of Perl testing other languages

Replies are listed 'Best First'.
Re: Examples of Perl testing other languages
by eyepopslikeamosquito (Archbishop) on Nov 17, 2004 at 05:38 UTC

    I often run commands, then verify the return code, stdout and stderr. Generally, I like to put as much logic as I can in support functions (such as run_cmd_capture below), so the test script itself is short and reads easily. Here is a simple example.

    use strict; use warnings; use Test::More tests => 6; sub read_file_contents { my $fname = shift; local $/; open(my $fh, '<', $fname) or die "error: open '$fname': $!\n"; <$fh>; } sub run_cmd_capture { my $cmd = shift; my $tmpfile = "klink-$$.tmp"; my $outstr = `$cmd 2>$tmpfile`; my $rc = $? >> 8; my $errstr = read_file_contents($tmpfile); unlink($tmpfile) or die "error: unlink '$tmpfile': $!\n"; return ($rc, $outstr, $errstr); } my $outcmd = qq|$^X -le "print 'Klink'"|; my ($rc, $outstr, $errstr) = run_cmd_capture($outcmd); cmp_ok( $rc, '==', 0, "command '$outcmd' rc is zero" ); is( $outstr, "Klink\n", "command '$outcmd' stdout is Klink" ); is( $errstr, "", "command '$outcmd' stderr is empty" ); my $errcmd = qq|$^X -le "print STDERR 'Klink'"|; ($rc, $outstr, $errstr) = run_cmd_capture($errcmd); cmp_ok( $rc, '==', 0, "command '$errcmd' rc is zero" ); is( $outstr, "", "command '$errcmd' stdout is empty" ); is( $errstr, "Klink\n", "command '$errcmd' stderr is Klink" );
      Thanks. This was helpful.

      Jim Keenan

Re: Examples of Perl testing other languages
by itub (Priest) on Nov 17, 2004 at 04:59 UTC
    This is not a very exciting example, but I tried this for testing a wacky sort program in C (but the language doesn't matter at all, as the program is just called via backticks):

    use List::Util 'shuffle'; use Test::More; my $N = 20; # number of tests; max list length my $M = 20; # random number range plan tests => $N; for (1 .. $N) { my @a = shuffle map { int(rand($M)) } 1 .. $N; my @got = map {chomp; $_} `./mysort <<EOF @a -1 EOF`; my @s = sort { $a <=> $b } @a; is_deeply(\@got, \@s) or diag "@got\n@s\n"; }
Re: Examples of Perl testing other languages
by dragonchild (Archbishop) on Nov 17, 2004 at 13:25 UTC
    You might want to check out Testing Zork-style game where I asked the exact same question. I found that Expect worked really well for me, but I was working with a console program.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Examples of Perl testing other languages
by tagg (Acolyte) on Nov 21, 2004 at 18:59 UTC
    FYI, the FreeBSD regression test suite is being switched over to using Test::Harness.