views:

716

answers:

6

Is there a Perl module that allows me to view diffs between actual and reference output of programs (or functions)? The test fails if there are differences.

Also, in case there are differences but the output is OK (because the functionality has changed) I want to be able to commit the actual output as future reference output.

+4  A: 

I tend to use more of the Test::Simple and Test::More functionality. I looked at PerlUnit and it seems to provide much of the functionality which is already built into the standard libraries with the Test::Simple and Test::More libraries.

Xetius
I also use Test::More. On the OO side of doing things, I think Test::Class is used more than Perlunit.
asksol
+9  A: 

Perl has excellent utilities for doing testing. The most commonly used module is probably Test::More, which provides all the infrastructure you're likely to need for writing regression tests. The prove utility provides an easy interface for running test suites and summarizing the results. The Test::Differences module (which can be used with Test::More) might be useful to you as well. It formats differences as side-by-side comparisons. As for committing the actual output as the new reference material, that will depend on how your code under test provides output and how you capture it. It should be easy if you write to files and then compare them. If that's the case you might want to use the Text::Diff module within your test suite.

Michael Carman
+2  A: 

For testing the output of a program, there is Test::Command. It allows to easily verify the stdout and stderr (and the exit value) of programs. E.g.:

   use Test::Command tests => 3;

   my $echo_test = Test::Command->new( cmd => 'echo out' );

   $echo_test->exit_is_num(0, 'exit normally');
   $echo_test->stdout_is_eq("out\n", 'echoes out');
   $echo_test->stderr_unlike( qr/something went (wrong|bad)/, 'nothing went bad' )

The module also has a functional interface too, if it's more to your liking.

Yanick
+2  A: 

The community standard workhorses are Test::Simple (for getting started with testing) and Test::More (for once you want more than Test::Simple can do for you). Both are built around the concept of expected versus actual output, and both will show you differences when they occur. The perldoc for these modules will get you on your way.

You might also want to check out the Perl QA wiki, and if you're really interested in perl testing, the perl-qa mailing list might be worth looking into -- though it's generally more about creation of testing systems for Perl than using those systems within the language.

Finally, using the module-starter tool (from Module::Starter) will give you a really nice "CPAN standard" layout for new work -- or for dropping existing code into -- including a readymade test harness setup.

mdxi
+6  A: 

As mentioned, Test::Differences is one of the standard ways of accomplishing this, but I needed to mention PerlUnit: please do not use this. It's "abandonware" and does not integrate with standard Perl testing tools. Thus, for all new test modules coming out, you would have to port their functionality if you wanted to use them. (If someone has picked up the maintenance of this abandoned module, drop me a line. I need to talk to them as I maintain core testing tools I'd like to help integrate with PerlUnit).

Disclaimer: while Id didn't write it, I currently maintain Test::Differences, so I might be biased.

Ovid
+3  A: 

I question those of you who recommend the use of PerlUnit. It hasn't had a release in 3 years. If you really want xUnit-style testing, have a look at Test::Class, it does the same job, but in a more Perlish way. The fact that it's still maintained and has regular releases doesn't hurt either.

Just make sure that it makes sense for your project. Maybe good old Test::More is all you need (it usually is for me). I recommend reading the "Why you should [not] use Test::Class" sections in the docs.

Hinrik