分类: Python/Ruby
2010-10-14 10:33:41
被测代码:
def split(line, types=None, delimiter=None):
fields = line.split(delimiter)
if types:
fields = [ ty(val) for ty,val in zip(types,fields) ]
return fields
测试代码:
# testsplitter.py
import splitter
import unittest
# Unit tests
class TestSplitFunction(unittest.TestCase):
def setUp(self):
# Perform set up actions (if any)
pass
def tearDown(self):
# Perform clean-up actions (if any)
pass
def testsimplestring(self):
r = splitter.split('GOOG 100 490.50')
self.assertEqual(r,['GOOG','100','490.50'])
def testtypeconvert(self):
r = splitter.split('GOOG 100 490.50',[str, int, float])
self.assertEqual(r,['GOOG', 100, 490.5])
def testdelimiter(self):
r = splitter.split('GOOG,100,490.50',delimiter=',')
self.assertEqual(r,['GOOG','100','490.50'])
# Run the unittests
if __name__== '__main__':
unittest.main()
执行结果:
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
测试函数要以test开头。
更多参考: