Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Test::More subtest parameters

by kcott (Archbishop)
on Apr 17, 2013 at 16:51 UTC ( [id://1029178]=note: print w/replies, xml ) Need Help??


in reply to Test::More subtest parameters

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-23 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found