views:

208

answers:

4

I'm looking for some tutorials showing how I could test C programs by writing Perl programs to automate testing.

Basically I want to learn automation testing with Perl programs. Can anyone kindly share such tutorials or any experiences of yours which can help me kick-start this process?

+9  A: 

Perl tests usually use TAP. There are a several C libraries for TAP. Watch this Perl testing presentation.

Alexandr Ciornii
A: 

You should look at Perl Testing: A Developer's Notebook by chromatic and Ian Langworth.

I keep meaning to buy a copy but as yet I've just skimmed it at perlmongers meetings. But it seems to be spot on what you're looking for.


UPDATE:

Hm, and this shows I should read the question - testing C programs with Perl, not testing Perl programs with Perl.

The book may still be useful (in that you should probably be writing test scripts and using Test::More and friends) but you will need to write a set of Perl functions to control your C if you take that approach. Basically

sub run_my_c_program {
    my @args=@_;

    #Set up test environment according to @args

    system "my-c-program";

    # Turns restults into a $rv data structure

    return $rv;
}

and then check $rv in the same way as a normal Perl test:

is_deeply(run_my_c_program(...),
          { .. what I think it returns ..}, 
          ".. description of what I'm testing ..");
ijw
Chapter 9 in particular is of interest. There are sections on testing programs and shared libraries. This is a very good book, BTW.
daotoad
+2  A: 

If you want to start learning how to use Perl to test external programs, start with learning to use Perl to test Perl bits. The Test::More module is a good place to start. Once you understand that, look at all of the other Test::* modules on CPAN to see if one of those modules does the sort of thing you need to do.

If you have a specific question, ask about that. This question is really too broad for anyone to provide a useful answer.

brian d foy
A: 

I don't have a tutorial but I used to be on a test team that tested a C++ compiler. The test harness (home brewed) we used was written in perl and it worked for us for many years. Perl was ideal because we could easily use it to invoke the tools to build the program, capture the output for later insertion into our test logs (using the "backticks" to run the compiler. For example: $compilerOutput = \cl -?; ), then, if the build was successful, run the test programs and capture their output, again for insertion into our test logs.

Other benefits we got from using Perl:

  • For pass/fail detection, Perl's regular expression support was helpful.
  • Our program (the compiler) was ported during the course of its life to many different hardware architectures. Since Perl source was available and in C, we could get perl running on a new architecture as soon as a C compiler was available (which is usually the first language implemented in a new environment after an assembler).
  • Lots of documentation, books, help available.

-Ron

Ron Pihlgren