You want to write unittest
-cases for a function like that:
def test_me(a): for b in c: print do_something(a,b)
At first I thought about just collecting the outputs of do_something
in a string and then returning it, to print and test the whole output together. But it's not always convinient because such loops could cause your buffer string to get very big, depending on the circumstances. So what can you do to test the output, when it is printed and not returned?
Use the PRINT statement to send data to the screen, a line printer, or another print file. The ON clause specifies the logical print channel to use for output. print.channel is an expression that evaluates to a number from -1 through 255.
The PRINT statement sends data to the display terminal or to another specified print unit. Specifies that data should be output to a Spooler print unit unit#.
The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.
print
prints to sys.stdout
, which you can reassign to your own object if you wish. The only thing your object needs is a write
function which takes a single string argument.
Since Python 2.6 you may also change print
to be a function rather than a language construct by adding from __future__ import print_function
to the top of your script. This way you can override print
with your own function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With