Discussion:
Testing Output and Functionality
Jed Lund
2012-04-24 04:35:24 UTC
Permalink
Hello,

I was wondering if I could get some pointers towards a module or package
that would allow me to test the functionality of various elements of code
that I was developing while I also tested the STDERR and STDOUT output from
that element of code? I have been hunting around CPAN for the right fit
but so far I have only found STDERR/STDOUT tests or functionality tests not
a way to test both results from the same method call.

The most basic case would be to test the output of a method that returned
some defined result while also capturing any warnings prompted by malformed
input which did not cause catastrophic failure. The goal would be to
certify the output and the warning message from the same method call.

I understand that this is a fairly basic question but I am stuck trying to
find the right path forward.

Best Regards,

Jed
Andy Lester
2012-04-24 04:53:10 UTC
Permalink
Post by Jed Lund
that element of code? I have been hunting around CPAN for the right fit
but so far I have only found STDERR/STDOUT tests or functionality tests not
a way to test both results from the same method call.
Sounds like you're looking for Test::Output.

http://search.cpan.org/dist/Test-Output/

xoxo,
Andy

--
Andy Lester => ***@petdance.com => www.petdance.com => AIM:petdance
Jed Lund
2012-04-24 05:37:14 UTC
Permalink
Andy,

Thank you for the reply.

I did try Test::Output but I ran into a Smart::Comments incompatibility.
This is a sort of corner case to the issue I described where I am trying
to print the Smart::Comments but capture the STDOUT output for testing.
When I run the following;

use Test::Most;
use Test::Output;
use Smart::Comments '###';
stdout_is( sub{
print "Hello World\n";
### Goodby World ...
}, "Hello World\n", 'Test Hello World' );
done_testing();

I get the following error;

Can't locate object method "TELL" via package "Test::Output::Tie" at
C:/Perl/site/lib/Smart/Comments.pm line 438.

While a warn or carp statement in the same place seems to work just fine
the Smart::Comments don't. I kind of walked away at that point and started
looking somewhere else. Should I just be more persistent to fix this issue
or is there another solution?

Best Regards,

Jed
Post by Jed Lund
that element of code? I have been hunting around CPAN for the right fit
but so far I have only found STDERR/STDOUT tests or functionality tests not
a way to test both results from the same method call.
Sounds like you're looking for Test::Output.
http://search.cpan.org/dist/Test-Output/
xoxo,
Andy
--
Eirik Berg Hanssen
2012-04-24 06:44:04 UTC
Permalink
Post by Jed Lund
Hello,
I was wondering if I could get some pointers towards a module or package
that would allow me to test the functionality of various elements of code
that I was developing while I also tested the STDERR and STDOUT output from
that element of code? I have been hunting around CPAN for the right fit
but so far I have only found STDERR/STDOUT tests or functionality tests not
a way to test both results from the same method call.
This sounds like a job for ... Test::Trap!


The most basic case would be to test the output of a method that returned
Post by Jed Lund
some defined result while also capturing any warnings prompted by malformed
input which did not cause catastrophic failure. The goal would be to
certify the output and the warning message from the same method call.
my $result = trap { $obj->method(@args) }; # or my @results, for list
context
# Possibly:
$trap->warn_is_deeply( \@expected_warnings, "Got exactly the expected
warnings?" );
# ... but usually more usefully:
$trap->stderr_like( $pattern, "Trapped stderr matches expectations" );
# ... and you may still test $trap->stdout, $result/@result (or
$trap->return), and whatever else, from the same call.


Eirik
Jed Lund
2012-04-24 14:35:48 UTC
Permalink
Eirik,

Thank you for the suggestion. I had not seen Test::Trap in my searches so
I will investigate.

Best Regards,

Jed

On Mon, Apr 23, 2012 at 11:44 PM, Eirik Berg Hanssen <
Post by Eirik Berg Hanssen
Post by Jed Lund
Hello,
I was wondering if I could get some pointers towards a module or package
that would allow me to test the functionality of various elements of code
that I was developing while I also tested the STDERR and STDOUT output from
that element of code? I have been hunting around CPAN for the right fit
but so far I have only found STDERR/STDOUT tests or functionality tests not
a way to test both results from the same method call.
This sounds like a job for ... Test::Trap!
The most basic case would be to test the output of a method that returned
Post by Jed Lund
some defined result while also capturing any warnings prompted by malformed
input which did not cause catastrophic failure. The goal would be to
certify the output and the warning message from the same method call.
context
warnings?" );
$trap->stderr_like( $pattern, "Trapped stderr matches expectations" );
$trap->return), and whatever else, from the same call.
Eirik
David Golden
2012-04-24 11:07:45 UTC
Permalink
Post by Jed Lund
I understand that this is a fairly basic question but I am stuck trying to
find the right path forward.
It's not entirely basic, because you're testing an array of three
"outputs" for each functionality test instead of just one.

I'd probably use Capture::Tiny:

use Capture::Tiny qw/capture/;

my ($out, $err, $result) = capture { your_code_here };
# then test $out, $err, $result however you want

If that's repetitive, stick it in a loop or write a function to
abstract testing the three results against your expectations.

-- David
Jed Lund
2012-04-24 14:34:01 UTC
Permalink
David,

Thank you for your suggestion. I haven't gone down that road yet so I will
try it.

Best Regards,

Jed
Post by David Golden
Post by Jed Lund
I understand that this is a fairly basic question but I am stuck trying
to
Post by Jed Lund
find the right path forward.
It's not entirely basic, because you're testing an array of three
"outputs" for each functionality test instead of just one.
use Capture::Tiny qw/capture/;
my ($out, $err, $result) = capture { your_code_here };
# then test $out, $err, $result however you want
If that's repetitive, stick it in a loop or write a function to
abstract testing the three results against your expectations.
-- David
Loading...