Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

G'day eyepopslikeamosquito,

One issue I see with this is that you need to know how many tests are in TMod::sub1() in order to specify tests => 5 in t1.pl. If that's intended to be common code, then whenever the common code changes all test scripts that use it will also need to be changed.

One way around this would be to use Test::More's subtest() function. Consider this slight rewrite of the code you posted.

t1.pl:

use strict; use warnings; use FindBin (); use lib "$FindBin::Bin"; use TMod; use Test::More tests => 4; ok( 1 == 1, "mytest1" ); subtest tmod => sub { TMod::sub1() }; ok( 3 == 4, "mytest3" ); ok( 4 == 4, "mytest4" );

TMod.pm:

package TMod; use strict; use warnings; use Test::More; sub sub1 { plan tests => 2; ok( 'sub1' eq 'sub99', "sub1-test1" ); ok( 42 == 42, "sub1-test2" ); } 1;

Sample run:

$ perl t1.pl 1..4 ok 1 - mytest1 1..2 not ok 1 - sub1-test1 # Failed test 'sub1-test1' # at /Users/ken/tmp/pm_subtest_mod/TMod.pm line 11. ok 2 - sub1-test2 # Looks like you failed 1 test of 2. not ok 2 - tmod # Failed test 'tmod' # at t1.pl line 11. not ok 3 - mytest3 # Failed test 'mytest3' # at t1.pl line 12. ok 4 - mytest4 # Looks like you failed 2 tests of 4.

With this setup, you can add, remove or change the tests in TMod::sub1() without ever needing to change any of the calling scripts. You could also pass arguments to TMod::sub1() to control which tests are run with sub1() dynamically determining the value for plan tests => $num_tests; (again, without requiring any changes to the calling scripts).

-- Ken


In reply to Re: Eliminating duplicated code in multiple test scripts using Test::More by kcott
in thread Eliminating duplicated code in multiple test scripts using Test::More by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (3)
    As of 2014-07-31 02:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (244 votes), past polls