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

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 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.

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" );

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 1..4 ok 1 - mytest1 1..2 not ok 1 - sub1-test1 # Failed test 'sub1-test1' # at /Users/ken/tmp/pm_subtest_mod/ line 11. ok 2 - sub1-test2 # Looks like you failed 1 test of 2. not ok 2 - tmod # Failed test 'tmod' # at line 11. not ok 3 - mytest3 # Failed test 'mytest3' # at 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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2015-11-30 00:55 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (755 votes), past polls