是的。你爲什麼不給它一個旋風?
下面是一個例子,在我的測試類從unittest.TestCase
繼承和測試故意失敗:
#!/usr/bin/env python
import unittest
class TestAddition(unittest.TestCase):
def test_addition(self):
self.assertEquals(3, 1 + 1)
class TestSubtraction(unittest.TestCase):
def test_subtraction(self):
self.assertEquals(3, 1 - 1)
if __name__ == '__main__':
unittest.main()
% ./t.py
FF
======================================================================
FAIL: test_addition (__main__.TestAddition)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./t.py", line 8, in test_addition
self.assertEquals(3, 1 + 1)
AssertionError: 3 != 2
======================================================================
FAIL: test_subtraction (__main__.TestSubtraction)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./t.py", line 13, in test_subtraction
self.assertEquals(3, 1 - 1)
AssertionError: 3 != 0
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (failures=2)
是的,你爲什麼不嘗試一下:)順便說你的類應該從'單元測試繼承。 TestCase' – mouad 2011-05-09 09:31:27