UnitTest의 Python용 모듈. 버젼 2.1부터 built-in에 포함됨.
http://pyunit.sourceforge.net/
JUnit을 모델로 만들었기때문에 매우 유사.
테스팅 방법들
- assertEquals(a, b, "message when not equal") = assertEqual = failUnlessEqual
 - assertNotEquals(a, b, "message when equal") = assertNotEqual = failIfEqual
 - assert_(boolean, "message when false") = failUnless
 failIf(2 > 1)
- fail("message") 무조건 실패
 - failUnlessRaises(exceptionClass, callableObj, *args, **kwargs) = assertRaises
 - failUnlessEqual
 
PyUnit template
원하는 테스트만을 실행하고 싶다면,
test_ 로 시작하는 모든 테스트들 실행하기
   1 """all-testing framework 
   2  
   3 This module will search for scripts in the same directory named 
   4 test_XYZ.py.  Each such script should be a test suite that tests a 
   5 module through unittest.   This script will aggregate all 
   6 found test suites into one big test suite and run them all at once. 
   7 """
   8 
   9 import sys, os, re, unittest
  10 from optparse import OptionParser
  11 
  12 VERBOSITY=2
  13 
  14 def allTest():
  15     path = os.path.abspath(os.path.split(sys.argv[0])[0])
  16     files = os.listdir(path)
  17     test = re.compile(r"test_(.+)\.py$", re.IGNORECASE)
  18     files = filter(test.search, files)
  19     filenameToModuleName = lambda f: os.path.splitext(f)[0]
  20     moduleNames = map(filenameToModuleName, files)
  21     modules = map(__import__, moduleNames)
  22     load = unittest.defaultTestLoader.loadTestsFromModule
  23     return unittest.TestSuite(map(load, modules))
  24 
  25 if __name__ == "__main__":
  26     if len(sys.argv)>1:
  27         sys.argv
  28     parser=OptionParser()
  29     parser.add_option("-v", dest="verbose",type="int")
  30     options,args=parser.parse_args()
  31     options.ensure_value('verbose',VERBOSITY)
  32     unittest.TextTestRunner(verbosity=options.verbose).run(allTest())
optparse를 이용한 UnitTest --> [EncodingConverter.py] 의 main 부분. 이후 VimEditor 명령행에 다음을 입력한다.
:nmap <F5> :wa <CR> :! python % -u <CR>
예제코드
   1 """Example of Recursion and PyUnit and AssertiveProgramming and FunctionalProgramming
   2 """
   3 
   4 import unittest
   5 
   6 def fact(aNum):
   7     assert aNum >= 1 and type(aNum) == int
   8     if aNum == 1:
   9         return 1
  10     else:
  11         return aNum * fact(aNum-1)
  12 
  13 factFP = lambda n: n<=1 and 1 or factFP(n-1)*n
  14 
  15 class FactorialTest(unittest.TestCase):
  16     def test1(self):
  17         self.assertEquals(1, fact(1))
  18     def test2(self):
  19         self.assertEquals(2, fact(2))
  20         self.assertEquals(6, fact(3))
  21     def testNegativeOrFloat(self):
  22         self.assertRaises(AssertionError, fact, -2)
  23         self.assertRaises(AssertionError, fact, 1.1)
  24     def testOtherTypes(self):
  25         self.assertRaises(AssertionError, fact, 'a')
  26         
  27 if __name__=='__main__':
  28     unittest.main(argv=('','-v'))
SeeAlso PyUnit UnitTestingWithaDebugger
 BioHackersNet