[Israel.pm] Using perl as unit-testing framework

Gaal Yahas gaal at forum2.org
Mon Nov 23 00:49:55 PST 2009

Hi Assaf,

On Mon, Nov 23, 2009 at 1:34 AM, Assaf Gordon <gordon at cshl.edu> wrote:
> Hello,
> I have a package of command line programs (written in c/c++) and I want to add unit-testing to it.

Nitpick: it's generally not unittesting if done at the "run a process
and examine the results" level. Unit tests typically invoke particular
functions inside the original code directly. Just pointing this out in
case you haven't considered this approach yet; sometimes it's more
useful. http://code.google.com/p/googletest/ is a decent framework.

On the other hand, if you want to use Perl, you get:
1. Excellent built-in tools for examining textual output of your tools
2. Support for emitting test output in TAP, which as you seem to be
aware, allows you to get nice result summary data.

> It uses the gnu autotools, and I'd like users to be able to run "make check" to run the tests.

You need to eventually invoke Test::Harness with the proper args
(where your tests are, whether to be verbose, etc.). The generated
makefiles in most Perl distributions do this by calling
ExtUtils::Command::MM's test_harness. Look that up.

> I'm sure similar things have been done countless of times (probably using Perl).
> Do you have any recommendation on where to start ?

Assuming you do use Perl to harness your tests, start by "perldoc -f
system". Make sure you understand the list argument form, and read how
to interpret $?. Then use IPC::Run3 to wrap system and manage stdio

> Important features are:
> 1. minimum end-user requirements (preferably - will not require them to install modules with CPAN)

Remain open to reconsidering this point. CPAN has a great deal of
useful test modules (though most of them are aimed at helping in
writing unittests). You can of course start without them, but at least
take a look at what's around so you can decide to refactor later.

> 2. Set command line arguments and STDIN, check STDOUT, STDERR and exitcode (and of course segfaults and signals).

IPC::Run3 as I mentioned; though now that I think of it it would be
useful to have a Test::Invoke module that wrapped this.

> 3. relatively easy to implement
> If the answer is RTFM, please point me to a good FM (for example, the main wiki for TAP has a lot of information, except starting examples for just two languages: C++ and PERL.... http://testanything.org/wiki/index.php/Testing_with_TAP )
> Thanks,
>  -gordon
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl

Hope this helps.

Gaal Yahas <gaal at forum2.org>

More information about the Perl mailing list