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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With