Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Most painless way to update the number of tests run?

by nysus (Parson)
on Feb 12, 2017 at 03:06 UTC ( [id://1181777]=perlquestion: print w/replies, xml ) Need Help??

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

Wondering if anyone has a nifty way of automatically changing (I'll settle for quickly, though) the number of tests to run on this line:

use Test::More tests => 23;

Mabe there is some nifty vim script for this?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Most painless way to update the number of tests run?
by BillKSmith (Monsignor) on Feb 12, 2017 at 04:23 UTC
    This section form the Test::More documentation provides a solution, even if not exactly what you asked.
    There are cases when you will not know beforehand how many tests your script is going to run. In this case, you can declare your tests at the end. use Test::More; ... run your tests ... done_testing( $number_of_tests_run ); Sometimes you really don't know how many tests were run, or it's too difficult to calculate. In which case you can leave off $number_of_tests_run.
    Bill

      So would it be any harm just leave off the number of tests until you are done writing all the tests? I guess I'm just not entirely clear on why keeping this number accurate is important, then. I guess so Test::More will know if all test ran successfully so it can give an accurate final report as to whether all tests were completed successfully?

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        You can comment some test off when working on a feature, but the number of tests will tell you when you forget to uncomment them. Also, if you run a test in a loop (some theories say you should never do that), the number of tests checks the number of iterations.

        Also, we use Test::Spec at work. If you place a test to a wrong part of the code (outside of an it/they ), it could never run. Having the number of tests specified means you know the test didn't run.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        Perhaps I'm reading too far into this... is this all you are looking for?

        use warnings; use strict; use Test::More; { # 5 tests is 5, 5, "five ok"; is 25/5, 5, "25 by 5 ok"; } { # 6 tests is 6, 6, "six ok"; is 6**6/6**2, 1296, "odd division ok" } { # unexpected test my $ok; $ok = eval { die "another test!?!\n"; 1; }; is $ok, undef, "we died expectedly"; like $@, qr/^another test/, "...err msg ok"; } done_testing();
Re: Most painless way to update the number of tests run?
by kcott (Archbishop) on Feb 12, 2017 at 06:40 UTC
Re: Most painless way to update the number of tests run?
by dsheroh (Monsignor) on Feb 12, 2017 at 09:34 UTC
    Personally, I haven't specified a single test count since Test::More added done_testing() as an option. While I'm certain there are use cases where the count itself can be important information, keeping it accurate was never more than pointless busywork for me, so I stopped doing it as soon as that was an option.
Re: Most painless way to update the number of tests run?
by Your Mother (Archbishop) on Feb 13, 2017 at 15:50 UTC

    I bundle stuff up to make individual count idiosyncrasies easier to track and I leave them all as done_testing() without a count until I am done writing them and then I enter the counts. The counts RFC:SHOULD be there. There are cases where the tests will fail or skip and without the count, they might appear to pass. Simplistic example of my prefered approach–

    Update: Added an idiom I use and like.

    use strictures; use Test::More; subtest "This stuff with namespace and test count isolation" => sub { my @whatever = 1 .. 3; ok $_, "$_ is okay" for @whatever; return done_testing(scalar @whatever) if $ENV{FULL_MOON}; ok "one more", "One more is okay"; done_testing(1+@whatever); }; subtest "That stuff" => sub { my %whatever = ( 1 => "One", 2 => "Two" ); ok $_, "$whatever{$_} is okay" for keys %whatever; done_testing(scalar keys %whatever); }; done_testing(2);
Re: Most painless way to update the number of tests run?
by Anonymous Monk on Feb 14, 2017 at 19:35 UTC
    There is no reason to automatically update the number of tests run. Running the tests tells you how many tests were run, so adjust the number accordingly. Before you object remember that you, by your own admission, are not a professional and are never going to be a professional so it is OK if you don't understand. You never will.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-16 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found