tags:

views:

152

answers:

3

According to Test::More documentation, it will exit with certain exit codes depending on the out come of your tests. My question is, how do I check these exit codes?

ps. I am trying to build a harness myself. Is there a simple harness I can use?

ps2. Test::Harness did the trick for me. In particular execute_tests function. This function returns all the statistics I want. Thank you everyone who gave useful links.

+4  A: 

It's a process return code. If running under some Unix variant's shell, you can usually retrieve it as $?. (which probably makes your question more about bash than perl)

Common idioms:

perl your_file.t
if [[ $? -gt 10 ]]; then
  echo "Wow, that's a lot of failures"
elif [[ $? -gt 0 ]]; then
  echo "Tests failed"
else
  echo "Success!"
fi

Alternately, if you're only interested in success/failure:

perl your_file.t || echo "Something bad happened."
JB
+7  A: 

Any decent harness program (such as prove) will do that for you, there is absolutely no reason for you to do that yourself.

Leon Timmermans
Actually I am building a harness myself. I'll give prove a try and see. Thanks.
Thushan
+1  A: 

If you're calling your test program from Perl, then the hard (and traditional) way involves doing dark and horrid bit-shifts to $?. You can read about that in the system function documentation if you really want to see how to do it.

The nice way involves using a module which gives you a system style function that processes return values for you:

use IPC::System::Simple qw(systemx EXIT_ANY);

my $exit_value = systemx( EXIT_ANY, 'mytest.t' );

The EXIT_ANY symbol allows your script to return any exit value, which we can then capture. If you just want to make sure that your scripts are passing (ie, returning a zero exit status), and halt as soon as any fail, that's IPC::System::Simple's default behaviour:

use IPC::System::Simple qw(systemx);

systemx( 'mytest.t' );  # Run this command successfully or die.

In all the above examples, you can ask for a replacement system command rather than systemx if you're happy for the possibility of the shell getting involved. See the IPC::System::Simple documentation for more details.

There are other modules that may allow you to easily run a command and capture its exit value. TIMTOWTDI.

Having said that, all the good harnesses should check return values for you, so it's only if you're writing our own testing testers that you should need to look at this yourself.

All the best,

Paul

Disclosure: I wrote IPC::System::Simple, and so may have some positive bias toward it.

pjf