1.pyunit和junit差不多
1 import yate 2 import unittest 3 4 5 class TestYate(unittest.TestCase): 6 7 def testYate(self): 8 print(yate.start_response('text/html')) 9 print(yate.do_form('add_timing_data.py', ['TimeValue'], 'send')) 10 11 12 13 if __name__ == "__main__": 14 #import sys;sys.argv = ['', 'Test.testName'] 15 unittest.main()
默认 main 测试所有以test开头的方法,批量测试所有函数
2.像junit一样测试指定函数
(1)构造测试集合,在集合中指定要测试的函数
1 from widget import Widget 2 import unittest 3 # 执行测试的类 4 class WidgetTestCase(unittest.TestCase): 5 def setUp(self): 6 self.widget = Widget() 7 def tearDown(self): 8 self.widget = None 9 def testSize(self): 10 self.assertEqual(self.widget.getSize(), (40, 40)) 11 # 构造测试集 12 def suite(): 13 suite = unittest.TestSuite() 14 suite.addTest(WidgetTestCase("testSize")) 15 return suite 16 # 测试 17 if __name__ == "__main__": 18 unittest.main(defaultTest = 'suite')
(2)直接测试指定函数,相当于没有用pyunit
1 from widget import Widget 2 # 执行测试的类 3 class TestWidget: 4 def testSize(self): 5 expectedSize = (40, 40); 6 widget = Widget() 7 if widget.getSize() == expectedSize: 8 print "test [Widget]: getSize works perfected!" 9 else: 10 print "test [Widget]: getSize doesn't work!" 11 # 测试 12 if __name__ == '__main__': 13 myTest = TestWidget() 14 myTest.testSize()
(1)(2)代码引用地址 http://www.ibm.com/developerworks/cn/linux/l-pyunit/index.html
python菜鸟,看headfirst in python 第九章 manager your data 有感而想,频繁的测试应该引用python测试框架 pyunit