I’m trying to use Test::More to unit test Perl functions that print to the screen.
I understand that this output may interfere with tools such as prove.
How can I capture this output so I can print it with diag(), and also run tests on the output itself?
UPDATE: IMHO, the correct answer to this question ought to be to use Test::Output:
Output:
I am leaving the original answer for reference as, I believe, it still illustrates a useful technique.
You can localize
STDOUTand reopen to a scalar before calling the function, restore afterward:Output:
For versions of
perlolder than 5.8, you probably need to use IO::Scalar, but I do not know much about how things worked before 5.8.