G'day space_monk,

I tried a few tests with callback formats I'd come across elsewhere (e.g. [\&function, @args] from Perl/Tk) but subtest emits a warning that the 2nd argument must be a coderef. Given this, your best bet might be something like:

subtest 'test name' => sub { my_subtest($xml, @other_args) };

I played around with this for a bit and it seems to work well. I was able to conditionally run subtest tests based on the arguments passed in: I don't know if that's exactly what you want to do with it but it certainly "can be called a number of times with different parameters".

#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 9; my $xml = '<tag>content</tag>'; pass('PRE subtest'); subtest 'xml_subtest(undef)' => sub { xml_subtest(undef) }; subtest 'xml_subtest("blah")' => sub { xml_subtest("blah") }; subtest 'xml_subtest("blah", undef)' => sub { xml_subtest("blah", undef) }; subtest 'xml_subtest("blah", {})' => sub { xml_subtest("blah", {}) }; subtest 'xml_subtest($xml, {test_format => 1})' => sub { xml_subtest($xml, {test_format => 1}) }; subtest 'xml_subtest($xml, {test_tag => 1})' => sub { xml_subtest($xml, {test_tag => 1}) }; subtest 'xml_subtest($xml, {test_content => 1})' => sub { xml_subtest($xml, {test_content => 1}) }; pass('POST subtest'); sub xml_subtest { my ($sub_xml, $args) = @_; plan tests => 5; ok(defined $sub_xml, '$sub_xml defined'); ok(length $sub_xml, '$sub_xml populated'); ok(defined $args, '$args defined'); ok(ref $args eq 'HASH', '$args is hashref'); if ($args->{test_format}) { like($sub_xml, qr{^<([a-z]+)>[^<]+</\1>$}, '$sub_xml well-formed'); } elsif ($args->{test_tag}) { is(($sub_xml =~ m{^<([a-z]+)>})[0], 'tag', '$sub_xml tag element'); } elsif ($args->{test_content}) { cmp_ok(($sub_xml =~ m{>([^<]+)<})[0], 'eq', 'content', '$sub_xml tag content'); } else { fail('No tests indicated'); } }

Test results:

-- Ken


In reply to Re: Test::More subtest parameters by kcott
in thread Test::More subtest parameters by space_monk

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!
  • 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, details, 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, summary, 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.