Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Doozer (Beadle)
on Feb 28, 2013 at 10:42 UTC ( #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!

Comment on Test::More - Skip following tests if one test fails
Download Code
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 (Abbot) 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 (Abbot) 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
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? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2015-07-30 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls