Can I find out if all tests passed in Perl Test :: More?

I have a Perl script test written using Test :: More . Right before the release, and if all the tests pass, I would like to perform some cleaning steps. If any tests fail, I want to leave everything in place for troubleshooting.

Is there a Test :: More flag or some other best practice within a single test script to determine if everything is “good” as soon as the tests themselves are complete?

+6
unit-testing perl
source share
2 answers

You can access the current state of tests using Test :: Builder , available through 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 simply do the cleanup at the end of the script, but exit earlier if everything is ok with Test::More BAIL_OUT("reason why you are bailing"); .

Here you can find many other data and statistics on the status of your tests; see the documentation for Test :: Builder .

+10
source share

Here is what I came up with to avoid the "Unable to find object method" error at the bottom of this answer:

 #! /usr/bin/perl use strict; use warnings; use Test::More tests => 1; ok(int rand 2, 'this test randomly passes or fails'); my $FAILcount = 0; foreach my $detail (Test::Builder->details()) { if (${%$detail}{ok}==0) { $FAILcount++; } } if ($FAILcount == 0) { print "hooray!\n"; } else { print "aw... :(\n"; } 

On Solaris 10 with Perl v5.8.4 (with 31 registered patches) I got the following

 Can't locate object method "is_passing" via package "Test::Builder" 
0
source share

All Articles