Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Test::More subtest parameters

by Khen1950fx (Canon)
on Apr 17, 2013 at 10:39 UTC ( #1029115=note: print w/replies, xml ) Need Help??

in reply to Test::More subtest parameters

You can pass parameters like this, borrowing some fudge from Anonymous Monk:
#!/usr/bin/perl use strict; use warnings; use Test::More tests => 3; pass("This is fudge"); subtest 'Here comes the fudge' => sub { pass("This is white chocolate fudge"); pass("This is dark chocolate fudge"); }; pass("Fudge is fattening"); done_testing();
Subtests can be further divided into subsubtests. They're nestable. Note that a subtest and all of its subtests count as one test.

Replies are listed 'Best First'.
Re^2: Test::More subtest parameters
by space_monk (Chaplain) on Apr 17, 2013 at 11:43 UTC

    Thanks for the reply...

    Your subtest is simply an anonymous routine which does not take any parameters. To properly illustrate what I wanted perhaps the code below will clarify....

    my $xml; sub my_subtest { like( $xml, qr/xml version=/, "XML Document"); # more tests on XML document text here.... } $xml = MyPackage::method1(); subtest 'Checking XML from method1' => \&my_subtest; $xml = MyPackage::method2(); subtest 'Checking XML from method2...' => \&my_subtest;
    I would like to pass $xml as a variable (plus other test parameters) to the subtest. I think the original fudge poster was close as it looks as though having a "fudge wrapper" is the only way this can be achieved. I did wonder if there was a more direct method or whether subtest passed on additional params to "my_subtest".
    If any of my proposed solutions have minor errors, it's because I don't waste my genius on trivial matters. :-P
      Anonymous subs to the rescue:
      #!/usr/bin/perl use warnings; use strict; { package MyPackage; sub method1 { '<?xml version="1.0"?>' } sub method2 { '[?xml version="2.0"?][a][/a]' } } use Test::More; sub my_subtest { like( shift, qr/xml version=/, "XML Document"); # more tests on XML document text here.... } for my $method (qw/method1 method2/) { subtest "Checking $method" => sub { my_subtest(MyPackage->$method) + }; }
      Update: Unfortunately, I am not able to use a sub returning a sub as in
      sub new_test { my ($test, $method) = @_; return sub { $test->(MyPackage->$method) }; } for my $method (qw/method1 method2/) { subtest "Checking $method", new_test(\&my_subtest, $method); }

      I am getting

      Type of arg 2 to Test::More::subtest must be sub {} (not subroutine en +try)
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029115]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-05-25 23:25 GMT
Find Nodes?
    Voting Booth?