Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell if all tests passed under Perl's Test::More?

I have a Perl test script written using Test::More. Right before exiting, and if all tests passed, I'd like to perform some cleanup actions. If any tests failed, I want to leave everything in place for troubleshooting.

Is there a flag within Test::More, or some other best practice within a single test script, to tell if "all is well" once the tests themselves are complete?

like image 578
aczarnowski Avatar asked Oct 01 '09 21:10

aczarnowski


1 Answers

You can access the current status of the tests with Test::Builder, available via Test::More->builder:

use strict;
use warnings;
use Test::More tests => 1;

ok(int rand 2, 'this test randomly passes or fails');

if (Test::More->builder->is_passing)
{
    print "hooray!\n";
}
else
{
    print "aw... :(\n";
}

Alternatively, you can just do your cleanup at the end of the script, but exit early if things go awry, with Test::More's BAIL_OUT("reason why you are bailing");.

There's lots of other data and statistics you can gather about the state of your tests; see the documentation for Test::Builder.

like image 109
Ether Avatar answered Sep 28 '22 08:09

Ether