【发布时间】:2016-02-19 10:55:06
【问题描述】:
函数foo 打印到控制台。我想测试控制台打印。如何在 python 中实现这一点?
需要测试这个功能,没有返回语句:
def foo(inStr):
print "hi"+inStr
我的测试:
def test_foo():
cmdProcess = subprocess.Popen(foo("test"), stdout=subprocess.PIPE)
cmdOut = cmdProcess.communicate()[0]
self.assertEquals("hitest", cmdOut)
【问题讨论】:
-
duplicate stackoverflow.com/questions/12998908/… tl;dr 使用 future 将 print 转换为内置函数或在替换的标准输出文件上断言
-
我不想嘲笑任何东西。事实上,我的实际
foo需要大约 8 个参数,它返回一个 json。我也想测试一下。
标签: python python-2.7 unit-testing console python-unittest