Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can simply send info to stdout in a Test?

Tags:

testing

perl

It appears that simply putting a say, print, etc into a .t doesn't work. The output is hidden. So when using Test::More and Test::Tester how can I simply print something? I want this so I can play with some code while determining how to test it. note: it's ok if it's sent to stderr or only viewable using verbose. Also I dried using diag but that didn't appear to work just anywhere in the test.

like image 716
xenoterracide Avatar asked May 28 '11 15:05

xenoterracide


1 Answers

If you run a test script directly, you will see the output of print -- tests are just Perl code. However, if you run your tests using a harness, what you see in the output will be determined by the harness, especially its verbosity level, and by whether you print to STDOUT or STDERR.

For another way to print messages within tests, see Diagnostics in the documentation for Test::More, notably:

diag(...);
note(...);

Experimenting with a script like this will quickly illustrate how things work:

# Example usages:
#     perl     some_test.t   # We see everything in output.
#     prove    some_test.t   # We see only diag() and STDERR.
#     prove -v some_test.t   # Everything again.

# In some_test.t
use strict;
use warnings;
use Test::More;

pass;

diag("diag()");
note("note()");
print        "STDOUT\n";
print STDERR "STDERR\n";

done_testing;
like image 142
FMc Avatar answered Oct 20 '22 22:10

FMc