打印方法的单元测试 [英] unit test for a printing method
问题描述
测试(使用单元测试)打印
信息的方法的正确方法是什么?
例如:
def A(s):
打印''---''+ s +''---''
我想检查那个A(bla)真的打印出来了--- bla ---
谢谢
amit
What is the proper way to test (using unit test) a method that print
information?
for example:
def A(s):
print ''---''+s+''---''
and i want to check that A("bla") really print out "---bla---"
thanks
amit
推荐答案
noro写道:
noro wrote:
测试的正确方法是什么(使用单元测试)打印
信息的方法?
例如:
def A(s):
打印''---''+ s +''---''
我想检查A(bla)是否真的打印出来 - -bla ---"
What is the proper way to test (using unit test) a method that print
information?
for example:
def A(s):
print ''---''+s+''---''
and i want to check that A("bla") really print out "---bla---"
您可以将sys.stdout替换为cStringIO对象或任何其他
Diez
You can replace sys.stdout with a cStringIO-object or any other
file-protocol implementing object and such collect the data.
Diez
noro写道:
noro wrote:
什么是合适的方法测试(使用单元测试)打印
信息的方法?
例如:
def A(s) :
打印''---''+ s +''---''
我想检查A(bla) )真的打印出来--- bla ---
What is the proper way to test (using unit test) a method that print
information?
for example:
def A(s):
print ''---''+s+''---''
and i want to check that A("bla") really print out "---bla---"
http://docs.python。 org / lib / module-doctest.html
< / F>
http://docs.python.org/lib/module-doctest.html
</F>
noro写道:
noro wrote:
测试(使用单元测试)打印
信息的方法的正确方法是什么?
例如:
def A(s):
print''---''+ s +''---''
我想检查A(bla)是否真的打印出来了--- --- bla ---"
谢谢
amit
What is the proper way to test (using unit test) a method that print
information?
for example:
def A(s):
print ''---''+s+''---''
and i want to check that A("bla") really print out "---bla---"
thanks
amit
对于傻模块myprog.py:
def A(s):
打印''---''+ s +''---''
in test_myprog.py:
import unittest
$ b来自cStringIO的$ b导入StringIO#或来自StringIO ...
导入系统
导入myprog
类SomeIOTests(unittest。 TestCase):
def setUp(self):
self.held,sys.stdout = sys.stdout,StringIO()
def test_trivialArg(self):< br $>
myprog.A('''')
self.assertEqual(sys.stdout.getvalue(),''------ \ n'')
def test_simpleArg(self):
myprog.A(''simple'')
self.assertEqual(sys。 stdout.getvalue(),''--- simple --- \ n'')
def tearDown(个体经营):
sys.stdout = self.held
if __name__ ==''__ main__'':
unittest.main()
--Scott David Daniels
sc *********** @ acm。组织
这篇关于打印方法的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!