2006年06月26日 星期一 11:49
我想用unittest写下面程序. import re lines = ['xover -100\r\n','xover 10-10\r\n','xover 10-','XOVER 10-100\r\n', 'mode ','mode reader\r\n','mode stream\r\n','mode case\r\n', 'group ','Group 10-10\r\n','GROUP cn.test\r\n','gRoUp cn.t1,cn.t2\r\n', 'list','list ','list active', 'newgroups aaa bbb gmt','newgroups 11','newgroups 060606 121212','newgroups 060606 121212 GMT', '','' ] command = (r'^mode [reader|stream]', r'^list [active|active.times|newsgroups|subscriptions]', r'^xover [0-9]+-[0-9]+', r'^group .+', r'^newgroups [0-9]+ [0-9]+ GMT' ) #yymmdd hhmmss [GMT] for com in command: digs = re.compile(com,re.IGNORECASE) for line in lines: if digs.match(line): print line, -------------------------------------------- netkiller 摄影,户外,无线电 中国无线电运动协会 http://netkiller.hikz.com -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 2959 bytes Desc: not available Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20060626/52221eb3/smime.bin
2006年06月26日 星期一 17:13
不明白"用unittest写"是什么意思,是说"用unittest测试其正确性"吗? On 6/26/06, netkiller <openunix at 163.com> wrote: > > 我想用unittest写下面程序. > > import re > lines = ['xover -100\r\n','xover 10-10\r\n','xover 10-','XOVER > 10-100\r\n', > 'mode ','mode reader\r\n','mode stream\r\n','mode case\r\n', > 'group ','Group 10-10\r\n','GROUP cn.test\r\n','gRoUp cn.t1, > cn.t2\r\n', > 'list','list ','list active', > 'newgroups aaa bbb gmt','newgroups 11','newgroups 060606 > 121212','newgroups 060606 121212 GMT', > '','' > ] > command = (r'^mode [reader|stream]', > r'^list [active|active.times|newsgroups|subscriptions]', > r'^xover [0-9]+-[0-9]+', > r'^group .+', > r'^newgroups [0-9]+ [0-9]+ GMT' > ) > #yymmdd hhmmss [GMT] > for com in command: > digs = re.compile(com,re.IGNORECASE) > for line in lines: > if digs.match(line): > print line, > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060626/b3903080/attachment.html
2006年06月27日 星期二 14:02
在 06-6-26,netkiller<openunix at 163.com> 写道: > 我想用unittest写下面程序. > > import re > lines = ['xover -100\r\n','xover 10-10\r\n','xover 10-','XOVER 10-100\r\n', > 'mode ','mode reader\r\n','mode stream\r\n','mode case\r\n', > 'group ','Group 10-10\r\n','GROUP cn.test\r\n','gRoUp cn.t1,cn.t2\r\n', > 'list','list ','list active', > 'newgroups aaa bbb gmt','newgroups 11','newgroups 060606 121212','newgroups 060606 121212 GMT', > '','' > ] > command = (r'^mode [reader|stream]', > r'^list [active|active.times|newsgroups|subscriptions]', > r'^xover [0-9]+-[0-9]+', > r'^group .+', > r'^newgroups [0-9]+ [0-9]+ GMT' > ) > #yymmdd hhmmss [GMT] > for com in command: > digs = re.compile(com,re.IGNORECASE) > for line in lines: > if digs.match(line): > print line, > 单元测试的单元应该是指类,而且要有返回值。下面程序是一个有错误的测试,根据返回的错误结果,调程序到正确为止,这就是测试驱动了。 import re import unittest class Foo: def foo(self, command, lines): result = '' #yymmdd hhmmss [GMT] for com in command: digs = re.compile(com,re.IGNORECASE) for line in lines: if digs.match(line): result += line return result class FooFileTestCase(unittest.TestCase): def setUp(self): self.test = Foo() def testfoo(self): lines = ['xover -100\r\n','xover 10-10\r\n','xover 10-','XOVER 10-100\r\n', 'mode ','mode reader\r\n','mode stream\r\n','mode case\r\n', 'group ','Group 10-10\r\n','GROUP cn.test\r\n','gRoUp cn.t1,cn.t2\r\n', 'list','list ','list active', 'newgroups aaa bbb gmt','newgroups 11','newgroups 060606 121212','newgroups 060606 121212 GMT', '','' ] command = (r'^mode [reader|stream]', r'^list [active|active.times|newsgroups|subscriptions]', r'^xover [0-9]+-[0-9]+', r'^group .+', r'^newgroups [0-9]+ [0-9]+ GMT' ) self.assertEqual(self.test.foo(command, lines), '''mode reader mode stream list active xover 10-10 XOVER 10-100 Group 10-10 GROUP cn.test gRoUp cn.t1,cn.t2 newgroups 060606 121212 GMT''') def suite(): suite = unittest.TestSuite() suite.addTest(FooFileTestCase('testfoo')) return suite if __name__ == "__main__": unittest.main(defaultTest = 'suite')
Zeuux © 2025
京ICP备05028076号