Bailing out of tests

ChrisDolan on 2006-12-13T16:36:08

I'm using Test::WWW::Mechanize to walk a Catalyst-based website I'm building. Mech is great because it allows you to pick out links and forms from the returned HTML and follow them without needing to hard-code URLs in the test program.

My test is doing things like creating a user account, fiddling that account, logging off, etc. If the test code loses its way, I want the entire test to abort instead of continuing to try to submit forms that don't exist to user accounts that are absent.

Below is an example of the shorthand notation I've adopted. The "|| bail" suffix is a pleasantly concise way to say that the test in question is critical to the completion of all subsequent tests. The BAIL_OUT() function is a feature of Test::More that terminates the test run immediately.

use Test::More tests => 27;

sub bail { BAIL_OUT('Failed a critical test'); }

use_ok('Test::WWW::Mechanize::Catalyst', '...'); my $mech = Test::WWW::Mechanize::Catalyst->new();

ok($mech, 'Create mech instance') || bail; $mech->get_ok('http://localhost/', 'home page') || bail; $mech->content_contains('Welcome', 'home page content'); ...