Rob - 2003-06-12

Hi, I've been using CppUnit for a while now.  I find that I always forget to add new "test..." functions to the CPPUINT_TEST_SUITE BEGIN/END blocks.

In JUnit, any function in a test suite class that begins with "test" is automatically added to the test suite.  Wouldn't that be nice?

I just discovered a trick that can emulate this behavior using perl.  To use it, you will need a recent version of perl and a module called Embperl availiable from CPAN.

here is the perl script:
#!/usr/bin/perl -w
# This script is used to process epl files

use Embperl;

die "Need to specify at least one argument!\n"
  if not $ARGV[0];

Embperl::Execute($ARGV[0], $ARGV[0]);

Here is an example of the code in TestSkell.hpp.epl

my $file = $ARGV[0];
open(INPUT, $file);
@functions = ();
foreach $line (<INPUT>) {
   if ($line =~ m,(test\w+)\s*\(,) {
     push @functions, $1;

class TestCBody : public CppUnit::TestFixture  {
  [$ foreach $name (@functions) $]
  CPPUNIT_TEST( [+ $name +] );
  [$ endforeach $]
save the above in `file`.epl

Here is the unix makefile snippet that automates creating the source file.

%: %.epl
    rm -f $@
    perl -w $< > $@

Happy Coding!

add these lines to your makefile: