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:
$ pm_test_more_subtest.pl
1..9
ok 1 - PRE subtest
1..5
not ok 1 - $sub_xml defined
# Failed test '$sub_xml defined'
# at ./pm_test_more_subtest.pl line 34.
not ok 2 - $sub_xml populated
# Failed test '$sub_xml populated'
# at ./pm_test_more_subtest.pl line 35.
not ok 3 - $args defined
# Failed test '$args defined'
# at ./pm_test_more_subtest.pl line 36.
not ok 4 - $args is hashref
# Failed test '$args is hashref'
# at ./pm_test_more_subtest.pl line 37.
not ok 5 - No tests indicated
# Failed test 'No tests indicated'
# at ./pm_test_more_subtest.pl line 52.
# Looks like you failed 5 tests of 5.
not ok 2 - xml_subtest(undef)
# Failed test 'xml_subtest(undef)'
# at ./pm_test_more_subtest.pl line 13.
1..5
ok 1 - $sub_xml defined
ok 2 - $sub_xml populated
not ok 3 - $args defined
# Failed test '$args defined'
# at ./pm_test_more_subtest.pl line 36.
not ok 4 - $args is hashref
# Failed test '$args is hashref'
# at ./pm_test_more_subtest.pl line 37.
not ok 5 - No tests indicated
# Failed test 'No tests indicated'
# at ./pm_test_more_subtest.pl line 52.
# Looks like you failed 3 tests of 5.
not ok 3 - xml_subtest("blah")
# Failed test 'xml_subtest("blah")'
# at ./pm_test_more_subtest.pl line 15.
1..5
ok 1 - $sub_xml defined
ok 2 - $sub_xml populated
not ok 3 - $args defined
# Failed test '$args defined'
# at ./pm_test_more_subtest.pl line 36.
not ok 4 - $args is hashref
# Failed test '$args is hashref'
# at ./pm_test_more_subtest.pl line 37.
not ok 5 - No tests indicated
# Failed test 'No tests indicated'
# at ./pm_test_more_subtest.pl line 52.
# Looks like you failed 3 tests of 5.
not ok 4 - xml_subtest("blah", undef)
# Failed test 'xml_subtest("blah", undef)'
# at ./pm_test_more_subtest.pl line 17.
1..5
ok 1 - $sub_xml defined
ok 2 - $sub_xml populated
ok 3 - $args defined
ok 4 - $args is hashref
not ok 5 - No tests indicated
# Failed test 'No tests indicated'
# at ./pm_test_more_subtest.pl line 52.
# Looks like you failed 1 test of 5.
not ok 5 - xml_subtest("blah", {})
# Failed test 'xml_subtest("blah", {})'
# at ./pm_test_more_subtest.pl line 19.
1..5
ok 1 - $sub_xml defined
ok 2 - $sub_xml populated
ok 3 - $args defined
ok 4 - $args is hashref
ok 5 - $sub_xml well-formed
ok 6 - xml_subtest($xml, {test_format => 1})
1..5
ok 1 - $sub_xml defined
ok 2 - $sub_xml populated
ok 3 - $args defined
ok 4 - $args is hashref
ok 5 - $sub_xml tag element
ok 7 - xml_subtest($xml, {test_tag => 1})
1..5
ok 1 - $sub_xml defined
ok 2 - $sub_xml populated
ok 3 - $args defined
ok 4 - $args is hashref
ok 5 - $sub_xml tag content
ok 8 - xml_subtest($xml, {test_content => 1})
ok 9 - POST subtest
# Looks like you failed 4 tests of 9.
|