サンプルコード

   1 import unittest
   2 
   3 class ExampleTestCase(unittest.TestCase):
   4     def testSuccess(self): # 成功
   5         assert 1 == 1
   6     def testFail(self): # 失敗
   7         assert 0 == 1
   8     #メソッド名をtestFoobarとかにすれば勝手に実行される。
   9 
  10 def main():
  11     testsuite = unittest.TestSuite()
  12     testsuite.addTest(unittest.makeSuite(ExampleTestCase))
  13     unittest.TextTestRunner().run(testsuite)
  14 
  15 if __name__ == '__main__':
  16     main()

上の実行例

F.
======================================================================
FAIL: testFail (__main__.ExampleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 7, in testFail
    assert 0 == 1
AssertionError

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

FAILED (failures=1)

Python/ユニットテスト (last edited 2006-05-31 17:14:15 by KeisukeUrago)