Permalink
Browse files

Test::Output

  • Loading branch information...
1 parent 1313ca9 commit 0661666b916daaf84cca5f63e13a84467bdc21b0 @nunorc nunorc committed Jun 27, 2011
Showing with 68 additions and 2 deletions.
  1. +68 −2 CPAN/chapters/70-testing.pod
@@ -108,8 +108,6 @@ look at C<Test::Simple> X<Test::Simple> X<Module, Test::Simple>.
After C<Test::Simple> X<Test::Simple> X<Module, Test::Simple> this is the
next place to go. This module is a drop in replacement for C<Test::Simple>
X<Test::Simple> X<Module, Test::Simple> which means that you can start
-using it and keep previous tests already written using C<Test::Simple>
-X<Test::Simple> X<Module, Test::Simple>.
The first major difference is that sometimes you are not sure how many
tests you are going to run. To circumvent this problem you can use the
@@ -206,6 +204,74 @@ B<CPAN:> L<http://search.cpan.org/dist/Test-Output>
=end CPANinfo
+C<Test::Output> X<Test::Output> X<Module, Test::Output> is another handy
+module for testing. Sometimes you want to be able to test if your code
+is producing the correct output. So, we would like to capture the output
+of executing this code and compare it with the correct result. We can use
+this module to achieve this, for example, imagine you have a function that
+prints something to the standard output and we want to test if this function
+is working properly. For this we can use the C<stdout_is> function provided
+by this module has shown in the following snippet:
+
+=begin Perl
+
+ use Test::More tests => 1;
+ use Test::Output;
+
+ sub function {
+ print "testing is good!";
+ }
+
+ stdout_is(\&function, 'testing is good!', 'simple test');
+
+=end Perl
+
+C<stderr_is> can be used to validate the output sent to the standard error
+pipe in a similar way. If you wish to test the full output of your code,
+combining the standard and error output (typically C<STDERR> and C<STDOUT>)
+the C<combined_is> function can be used. For example:
+
+=begin Perl
+
+ use Test::More tests => 1;
+ use Test::Output;
+
+ sub function {
+ print STDOUT "hello";
+ print STDERR "World";
+ }
+
+ combined_is(\&function, 'helloWorld', 'simple test');
+
+=end Perl
+
+Another very commonly used function in this module is the C<output_is>
+function, which has ax extra argument, so that you can explicitly test
+what is being written to C<STDERR> and C<STDOUT>. Using a similar example
+to the ones already shown we could have something like:
+
+=begin Perl
+
+ use Test::More tests => 1;
+ use Test::Output;
+
+ sub function {
+ print STDOUT "hello";
+ print STDERR "World";
+ }
+
+ output_is(\&function, 'hello', 'World', 'simple test');
+
+=end Perl
+
+In this case we are using the C<output_is> function to validate that the
+output from running the code to C<STDOUT> is C<hello> and to C<STDERR> is
+C<World>.
+
+Similar to the functions described earlier this module also provides
+the typical variants: C<*_isnt>, C<*_like> and C<*_unlike>. For more
+information on these specific functions refer to the modules' documentation.
+
=head1 Test::TCP
X<Module, Test::TCP>X<Test::TCP>

0 comments on commit 0661666

Please sign in to comment.