How can simply send info to stdout in a Test?

2020-03-01 11:40发布

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.

标签: perl testing
1条回答
够拽才男人
2楼-- · 2020-03-01 12:29

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;
查看更多
登录 后发表回答