Python unittest 模块

来源:互联网 发布:飞机航班软件 编辑:程序博客网 时间:2024/06/06 18:18

最近在找一些Python 自带的模块的时候,发现一个小东东还很好玩的

 

我们写好一些方法后要去对这个方法做单元测试

Python 刚好有提供这样的模块,也很方便

下面的方法是从 Unittest.py 里找出来的

 

代码
import unittest

class IntegerArithmenticTestCase(unittest.TestCase):
    
def testAdd(self):  ## test method names begin 'test*'
        self.assertEquals((2 + 2), 3)
        self.assertEquals(0 
+ 11)
    
def testMultiply(self):
        self.assertEquals((0 
* 10), 0)
        self.assertEquals((
5 * 8), 40)

if __name__ == '__main__':
    unittest.main()

 

 

How to execute them ?

 

1. Create a file name "studyUnitTest.py"

2. Add above code into this file

3. Execute the script ,then you will find the result ,as follows

 

wenlezhou >>> python studyUnitTest.py
F.
======================================================================
FAIL: testAdd (__main__.IntegerArithmenticTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "studyUnitTest.py", line 5, in testAdd
    self.assertEquals((2 + 2), 3)
AssertionError: 4 != 3

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)