Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Test::More - Skip following tests if one test fails

by Doozer (Scribe)
on Feb 28, 2013 at 10:42 UTC ( [id://1021037]=perlquestion: print w/replies, xml ) Need Help??

Doozer has asked for the wisdom of the Perl Monks concerning the following question:

Hello!

I am using Test::More in conjunction with Test::WWW::Selenium to run automated web browser tests. I have read the info on the CPAN man page about Test::More but am struggling to get my head around the SKIP function.

In my script I have a test that checks if the title of a web page matches a regular expression that I have defined (shown below).

like($sel->get_title(), 'm/Broadband Connection \| Congratulations/', "Parental control hijack page was seen after the router was factory reset");

I would like to make it so if the title does not match the regular expression (and therefore that test fails), the following 3 tests after it are skipped.

I have a feeling its quite straight forward but I cant seem to get my head around how to do it.

Any help would be much appreciated and more info can be provided if needed.

Thanks!

Replies are listed 'Best First'.
Re: Test::More - Skip following tests if one test fails
by tmharish (Friar) on Feb 28, 2013 at 11:38 UTC

    Why not skip tests based on the condition?

    SKIP: { my $title = $sel->get_title() ; unless( $title =~ m/Broadband Connection \| Congratulations/' +) { skip "Parental control hijack page was seen after the route +r was factory reset", 3 ; ## Since you are skipping tests you may not want to fail he +re. fail( "Parental control hijack page was seen after the rout +er was factory reset" ); } # 3 tests here. }
Re: Test::More - Skip following tests if one test fails
by tobyink (Canon) on Feb 28, 2013 at 12:27 UTC

    Take advantage of the fact that Test::More's ok, like, is, etc functions all return a boolean indicating the test's status.

    The two possibilities you should look at are:

    If when a test fails, you want to skip *all* the remaining tests in the file do:

    like(...) or BAIL_OUT("No point in continuing");

    If you just wish to skip three tests and then continue with some more tests in the file:

    SKIP: { like(...) or skip "The next three are irrelevant", 3; # the three tests to be skipped here. ok(...); ok(...); ok(...); } # will continue with this test ok(...);
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Thank you both for the responses, they are very helpful. I had a feeling I was over complicating what the instructions were saying. Your examples have made it very clear how to use the SKIP functionality and I have got it working how I need.

      Thanks again!

      Just an additional note (many months after the event, and frankly to help me remember if I search for this again), BAIL_OUT will not work in the same way as SKIP - it will cause the test to fail.

      If your uber condition is not something that should cause a test failure, you'll probably want to use tobyink's SKIP: arrangement or Ken's skip_all => $skip_reason below.

        Worse, BAIL_OUT will cause subsequent test scripts to not be run! Though nobody seems to get that impression from reading the documentation, IME.

        - tye        

Re: Test::More - Skip following tests if one test fails
by kcott (Archbishop) on Mar 01, 2013 at 03:54 UTC

    G'day Doozer,

    I'm not sure if this is applicable to your current situation, but if you can test for the skip condition up-front, you can do something like:

    if ($skip_condition) { plan skip_all => $skip_reason; } else { plan tests => $number_of_tests; } # Planned tests here

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1021037]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found