How to test print statements?

后端 未结 2 2027
后悔当初
后悔当初 2021-02-18 13:12

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 firs

2条回答
  •  日久生厌
    2021-02-18 13:59

    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.

提交回复
热议问题