Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test print statements?

Tags:

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?

like image 668
erikbstack Avatar asked Jun 20 '12 15:06

erikbstack


People also ask

How do you use print statements?

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.

What is the result of print () statement?

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#.

How does print () work explain?

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.


1 Answers

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.

like image 85
Emil Vikström Avatar answered Oct 18 '22 21:10

Emil Vikström