Python ñÂΥƥ¹¥ÈÍѥե졼¥à¥ï¡¼¥¯

Ãø¼Ô: Steve Purcell, <stephen_purcell at yahoo dot com>
Project website: http://pyunit.sourceforge.net/
ÏÂÌõ: ÎëÌÚ¡÷²­¥½¥Õ¥È¥¦¥§¥¢ÃæÉô»Ù¼Ò

ÆâÍÆ

³µ´Ñ

´·½¬¤Ë½¾¤¤ 'PyUnit' ¤È¸Æ¤Ð¤ì¤ë Python ñÂΥƥ¹¥ÈÍѥե졼¥à¥ï¡¼¥¯ (Python unit testing framework) ¤Ï¡¤ ÁïÌÀ¤Ê¤¢¤Î Kent Beck ¤È Eric Gamma ¤Ë¤è¤ë JUnit ¤Î Python ¸À¸ìÈǤǤ¢¤ë¡£ ¤Ò¤ë¤¬¤¨¤Ã¤Æ JUnit ¤Ï Kent's Smalltalk testing framework ¤Î Java ÈǤǤ¢¤ë¡£ ¤É¤Á¤é¤â¤½¤ì¤¾¤ì¤Î¸À¸ì¤ËÂФ¹¤ëñÂΥƥ¹¥È¤Î»ö¼Â¾å¤Îɸ½à¤Ç¤¢¤ë¡£

¤³¤Îʸ½ñ¤Ï PyUnit ¤ÎÀ߷פȻÈÍÑË¡¤Î Python ÆÃÍ­¤ÎÌ̤òÀâÌÀ¤¹¤ë¡£ ¥Õ¥ì¡¼¥à¥ï¡¼¥¯¤Î´ðËÜÀ߷פˤĤ¤¤Æ¤ÎÇطʾðÊó¤Ë¤Ä¤¤¤Æ¤Ï¡¤ ÆÉ¼Ô¤Ï Kent ¤Î¸¶ÏÀʸ "Simple Smalltalk Testing: With Patterns" ¤ò»²¾È¤µ¤ì¤¿¤¤¡£

PyUnit ¤Ï Python ¥Ð¡¼¥¸¥ç¥ó 2.1. ¤Î Python ɸ½à¥é¥¤¥Ö¥é¥ê¤Î°ìÉô¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£

°Ê²¼¤Î¾ðÊó¤Ï Python ¤ÎÃ챤ò²¾Äꤹ¤ë¡£ ¤³¤Î¸À¸ì¤ÏÈó¾ï¤Ë´Êñ¤Ê¤Î¤Ç»ä¤Ç¤µ¤¨¤Ê¤ó¤È¤«³Ø¤Ù¤¿¤·¡¤ Èó¾ï¤Ë½¬´·À­¤¬¤¢¤ë¤Î¤Ç»ä¤Ï¤â¤¦»ß¤á¤ë¤³¤È¤¬¤Ç¤­¤Ê¤¤¡£

¥·¥¹¥Æ¥àÍ×·ï

PyUnit ¤Ï¥Ð¡¼¥¸¥ç¥ó 1.5.2 °Ê¹ß¤Î¤É¤Îɸ½à Python ¤Ç¤âÆ°ºî¤¹¤ë¤è¤¦¤ËÀ߷פµ¤ì¤Æ¤¤¤ë¡£

PyUnit ¤ÏÃø¼Ô¤Ë¤è¤Ã¤Æ Linux (Redhat 6.0, 6.1 ¤È Debian Potato) ¾å¤Î Python 1.5.2, 2.0, 2.1 ¤Ç¥Æ¥¹¥È¤µ¤ì¤Æ¤¤¤ë¡£ PyUnit ¤Ï¡¤Window ¤È Mac ¤ò´Þ¤à¾¤Î Python ¥×¥é¥Ã¥È¥Õ¥©¡¼¥à¤Ç¤âƯ¤¯¤³¤È¤¬ÃΤé¤ì¤Æ¤¤¤ë¡£ ²¿¤Ç¤¢¤ì¤¦¤Þ¤¯Æ°¤«¤Ê¤¤¥×¥é¥Ã¥È¥Õ¥©¡¼¥à¤Þ¤¿¤Ï Python ¤Î¥Ð¡¼¥¸¥ç¥ó¤¬¤¢¤Ã¤¿¤Ê¤é¡¤»ä¤ËÃΤ餻¤Æ¤¯¤À¤µ¤¤¡£

PyUnit ¤ò JPython ¤È Jython ¤Ç»ÈÍѤ¹¤ëºÝ¤Î¾ÜºÙ¤Ë¤Ä¤¤¤Æ¤Ï¡¤ ¡ÖPyUnit ¤ò JPython ¤È Jython ¤Ç»È¤¦¤Ë¤Ï¡×. ¤ÎÀá¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£

PyUnit ¤ò»È¤Ã¤Æ¥Æ¥¹¥È¤ò½ñ¤¯¤Ë¤Ï

¥¤¥ó¥¹¥È¡¼¥ëÊýË¡

¥Æ¥¹¥È¤ò½ñ¤¯¤¿¤á¤ËɬÍפʥ¯¥é¥¹¤Ï unittest ¥â¥¸¥å¡¼¥ë¤Ë¤¢¤ë¡£ ¤³¤Î¥â¥¸¥å¡¼¥ë¤Ï Python 2.1 °Ê¹ß¤Îɸ½à Python ¥é¥¤¥Ö¥é¥ê¤Î°ìÉô¤Ç¤¢¤ë¡£ ¤â¤··¯¤¬¤â¤Ã¤È¸Å¤¤¥Ð¡¼¥¸¥ç¥ó¤Î Python ¤ò»È¤Ã¤Æ¤¤¤ë¤Ê¤é¡¤ ÊÌ¸Ä¤Ë PyUnit ¥Ç¥£¥¹¥È¥ê¥Ó¥å¡¼¥·¥ç¥ó¤«¤é¥â¥¸¥å¡¼¥ë¤òÆþ¼ê¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£

¤³¤Î¥â¥¸¥å¡¼¥ë¤ò·¯¼«¿È¤Î¥³¡¼¥É¤«¤é»È¤¨¤ë¤è¤¦¤Ë¤¹¤ë¤Ë¤Ï¡¤ ¥Õ¥¡¥¤¥ë unittest.py ¤òÃÖ¤¤¤Æ¤¢¤ë¥Ç¥£¥ì¥¯¥È¥ê¤ò·¯¤Î Python ¤Î¸¡º÷¥Ñ¥¹¤ËÆþ¤ì¤ë¤À¤±¤Ç¤è¤¤¡£ ¤½¤¦¤¹¤ë¤Ë¤Ï´Ä¶­ÊÑ¿ô $PYTHONPATH ¤ò¥»¥Ã¥È¤·¤Æ¤â¤è¤¤¤·¡¤ ·¯¤Î¸½ºß¤Î Python ¸¡º÷¥Ñ¥¹¤Ë¤¢¤ë¥Ç¥£¥ì¥¯¥È¥ê¡¤ ¤¿¤È¤¨¤Ð Redhat Linux ¥Þ¥·¥ó¤Ê¤é /usr/lib/python1.5/site-packages ¤Ê¤É¤Ë¥Õ¥¡¥¤¥ë¤òÃÖ¤¤¤Æ¤â¤è¤¤¡£

¤³¤ì¤ò¤¹¤ë¤«¡¤ ¤Þ¤¿¤Ï unittest.py ¤òÎãÂê¤Î¤¢¤ë¥Ç¥£¥ì¥¯¥È¥ê¤Ë¥³¥Ô¡¼¤·¤Ê¤¤¤È¡¤ PyUnit ¤ËƱº­¤ÎÎãÂê¤ò¼Â¹Ô¤Ç¤­¤Ê¤¤¤³¤È¤ËÃí°Õ¤µ¤ì¤¿¤¤¡£

TestCase ¤Î¾Ò²ð

ñÂΥƥ¹¥È¤òÁȤßΩ¤Æ¤ë´ðËÜŪ¤Ê¥Ö¥í¥Ã¥¯¤¬¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Ç¤¢¤ë¡£ ¤Ò¤È¤Ä¤Î¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Ï¤Ò¤È¤Ä¤Î¥·¥Ê¥ê¥ª¤Ç¤¢¤ê¡¤ ¤½¤ì¤¾¤ìÁ°½àÈ÷¤·ÀµÅöÀ­¤ò¥Á¥§¥Ã¥¯¤¹¤ëɬÍפ¬¤¢¤ë¡£ PyUnit ¤Ç¤Ï¡¤¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Ï unittest ¥â¥¸¥å¡¼¥ë¤Î TestCase ¥¯¥é¥¹¤Çɽ¸½¤µ¤ì¤ë¡£ ·¯¼«¿È¤Î¥Æ¥¹¥È¡¦¥±¡¼¥¹¤òºî¤ë¤Ë¤Ï¡¤TestCase ¤ÎÇÉÀ¸¥¯¥é¥¹¤ò½ñ¤¯É¬Íפ¬¤¢¤ë¡£

TestCase ¥¯¥é¥¹¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Ï¡¤ ¤Ò¤È¤Ä¤Î¥Æ¥¹¥È¡¦¥á¥½¥Ã¥É¤ò´°Á´¤Ë¼Â¹Ô¤Ç¤­¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ë¡£ ¤³¤Î¥á¥½¥Ã¥É¤Ë¤Ï¥ª¥×¥·¥ç¥ó¤È¤·¤ÆÁ°½àÈ÷¤È¸åÊÒÉÕ¤±¤Î¥³¡¼¥É¤¬È¼¤¦¡£

TestCase ¥¤¥ó¥¹¥¿¥ó¥¹¤ÎÃæ¤Ç¥Æ¥¹¥È¤ò¹Ô¤¦¥³¡¼¥É¤Ï´°Á´¤Ë¼«µë¼°¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ¤Ä¤Þ¤ê¡¤Ã±ÆȤޤ¿¤ÏǤ°Õ¸Ä¿ô¤Î¾¤Î¥Æ¥¹¥È¡¦¥±¡¼¥¹¤È¤ÎǤ°Õ¤ÎÁȤ߹ç¤ï¤»¤Ç ¼Â¹Ô¤Ç¤­¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£

ñ½ã¤Ê¥Æ¥¹¥È¡¦¥±¡¼¥¹¤òºî¤ë

ºÇ¤âñ½ã¤Ê¥Æ¥¹¥È¡¦¥±¡¼¥¹ÇÉÀ¸¥¯¥é¥¹¤Ï¡¤ ÆÃÄê¤Î¥Æ¥¹¥È¤ò¹Ô¤¦¥³¡¼¥É¤ò¼Â¹Ô¤¹¤ë¤¿¤á¤Ë¡¤ ñ½ã¤Ë runTest¥á¥½¥Ã¥É¤ò¾å½ñ¤­ÄêµÁ¤¹¤ë¡£

        import unittest

        class DefaultWidgetSizeTestCase(unittest.TestCase):
            def runTest(self):
                widget = Widget("The widget")
                assert widget.size() == (50,50), 'incorrect default size'
    

¤¿¤Àñ¤Ë Python Áȹþ¤ß¤Î assert ʸ¤ò»È¤Ã¤Æ¡¤ ¥Æ¥¹¥È¤ò¤·¤Æ¤¤¤ë¤³¤È¤ËÃí°Õ¤µ¤ì¤¿¤¤¡£ ¤â¤·¤â¥Æ¥¹¥È¡¦¥±¡¼¥¹¼Â¹Ô»þ¤Ë assert ʸ¤¬¼ºÇÔ¤¹¤ì¤Ð¡¤ AssertionError ¤¬¤Ò¤­µ¯¤³¤µ¤ì¡¤ ¥Æ¥¹¥È¤ò¤¹¤ë¥Õ¥ì¡¼¥à¥ï¡¼¥¯¤Ï¤½¤Î¥Æ¥¹¥È¡¦¥±¡¼¥¹¤ò¡Ö¼ºÇÔ¡×(failure) ¤Èǧ¼±¤¹¤ë¡£ ÌÀ¼¨Åª¤Ê assert ¸¡ºº¤«¤é¤Ò¤­µ¯¤³¤µ¤ì¤¿¤Î¤Ç¤Ï¤Ê¤¤Â¾¤ÎÎã³°¤Ï¡¤ ¥Æ¥¹¥È¤ò¤¹¤ë¥Õ¥ì¡¼¥à¥ï¡¼¥¯¤«¤é¡Ö¥¨¥é¡¼¡×(error) ¤È¤·¤Æǧ¼±¤µ¤ì¤ë¡£ ('¥Æ¥¹¥È¾ò·ï¤Ë¤Ä¤¤¤Æ¤ÎÊä­' Àá »²¾È)

¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Î¼Â¹ÔÊýË¡¤Ï¸å½Ò¤¹¤ë¡£ ¤µ¤·¤¢¤¿¤ê¡¤¤³¤Î¤è¤¦¤Ê¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤¹¤ë¤Ë¤Ï¡¤ °ú¿ô¤Ê¤·¤Ç¥³¥ó¥¹¥È¥é¥¯¥¿¤ò¸Æ¤Ö¤³¤È¤ËÃí°Õ¤µ¤ì¤¿¤¤¡£

        testCase = DefaultWidgetSizeTestCase()
    

Á°½àÈ÷¥³¡¼¥É¤ÎºÆÍøÍÑ: ¡Öºî¤êÉÕ¤±¡×¤òºî¤ë

¤µ¤Æ¡¤¤³¤Î¤è¤¦¤Ê¥Æ¥¹¥È¡¦¥±¡¼¥¹¤ÏÇüÂç¤Ê¸Ä¿ô¤Ë¤Ê¤êÆÀ¤ë¡£ ¤½¤·¤Æ¤½¤ÎÁ°½àÈ÷¤Ï¤¿¤À¤Î·«¤êÊÖ¤·¤Ë¤Ê¤êÆÀ¤ë¡£ ¾åµ­¤Î¥±¡¼¥¹¤Ç¡¤100 ¸Ä¤Î¥Æ¥¹¥È¡¦¥±¡¼¥¹ÇÉÀ¸¥¯¥é¥¹¤¬¤½¤ì¤¾¤ì Widget ¤ò¹½ÃÛ¤¹¤ë¤È¤·¤¿¤é¡¤¤½¤ì¤Ï¸«¶ì¤·¤¤½ÅÊ£¤Ë¤Ê¤ë¤À¤í¤¦¡£

¤µ¤¤¤ï¤¤¡¤»ä¤¿¤Á¤Ï setUp ¤È¤¤¤¦ hook ¥á¥½¥Ã¥É¤ò¼ÂÁõ¤·¤Æ¡¤ ¤³¤Î¤è¤¦¤ÊÁ°½àÈ÷¥³¡¼¥É (set-up code) ¤ò¤¯¤¯¤ê½Ð¤¹¤³¤È¤¬¤Ç¤­¤ë¡£ »ä¤¿¤Á¤¬¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë¤È¤­¡¤ ¥Æ¥¹¥È¤ò¤¹¤ë¥Õ¥ì¡¼¥à¥ï¡¼¥¯¤¬¡¤ »ä¤¿¤Á¤ËÂå¤ï¤Ã¤Æ¤½¤Î¥á¥½¥Ã¥É¤ò¼«Æ°Åª¤Ë¸Æ¤Ó½Ð¤¹¡£

        import unittest

        class SimpleWidgetTestCase(unittest.TestCase):
            def setUp(self):
                self.widget = Widget("The widget")

        class DefaultWidgetSizeTestCase(SimpleWidgetTestCase):
            def runTest(self):
                assert self.widget.size() == (50,50), 'incorrect default size'

        class WidgetResizeTestCase(SimpleWidgetTestCase):
            def runTest(self):
                self.widget.resize(100,150)
                assert self.widget.size() == (100,150), \
                       'wrong size after resize'
    

¥Æ¥¹¥È¤Î¼Â¹ÔÃ桤¤â¤· setUp ¥á¥½¥Ã¥É¤¬Îã³°¤ò¤Ò¤­µ¯¤³¤·¤¿¤Ê¤é¡¤ ¥Õ¥ì¡¼¥à¥ï¡¼¥¯¤Ï¥Æ¥¹¥È¤¬¥¨¥é¡¼¤Ë´Ù¤Ã¤¿¤È¸«¤Ê¤·¡¤ runTest ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤·¤Ê¤¤¡£

ƱÍͤˡ¤»ä¤¿¤Á¤Ï runTest ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤·¤¿¸åÊÒÉÕ¤±¤ò¤¹¤ë tearDown ¥á¥½¥Ã¥É¤òÍѰդǤ­¤ë¡£

        import unittest

        class SimpleWidgetTestCase(unittest.TestCase):
            def setUp(self):
                self.widget = Widget("The widget")
            def tearDown(self):
                self.widget.dispose()
                self.widget = None
    

¤â¤· setUp ¤¬À®¸ù¤·¤¿¤Ê¤é¡¤tearDown ¥á¥½¥Ã¥É¤Ï runTest ¤ÎÀ®Èݤˤ«¤«¤ï¤é¤º¼Â¹Ô¤µ¤ì¤ë¡£

¥Æ¥¹¥È¤ò¤¹¤ë¥³¡¼¥É¤Î¤¿¤á¤Î¡¤¤³¤Î¤è¤¦¤Êºî¶È´Ä¶­¤òºî¤êÉÕ¤± (fixture) ¤È¸Æ¤Ö¡£

Ê£¿ô¤Î¥Æ¥¹¥È¡¦¥á¥½¥Ã¥É¤ò¤â¤Ã¤¿ TestCase ¥¯¥é¥¹

¿¤¯¤Î¾®¤µ¤Ê¥Æ¥¹¥È¡¦¥±¡¼¥¹¤¬Æ±¤¸ºî¤êÉÕ¤±¤ò»È¤¦¤³¤È¤¬¤è¤¯¤¢¤ë¡£ ¤³¤Î¾ì¹ç¡¤»ä¤¿¤Á¤Ï SimpleWidgetTestCase ¤«¤é DefaultWidgetSizeTestCase ¤Î¤è¤¦¤Ê¿¤¯¤Î¾®¤µ¤Ê£±¥á¥½¥Ã¥É¤Î¥¯¥é¥¹¤òÇÉÀ¸¤¹¤ë¤³¤È¤Ë ½ª»Ï¤·¤Æ¤·¤Þ¤¦¡£ ¤³¤ì¤Ï»þ´Ö¤ÎϲÈñ¤Ç¤¢¤ê´«¤á¤é¤ì¤Ê¤¤¤«¤é¡¤JUnit ¤ÈƱ¤¸¤è¤¦¤Ë¡¤ PyUnit ¤Ï¤â¤Ã¤Èñ½ã¤Ê¤·¤¯¤ß¤òÍÑ°Õ¤·¤Æ¤¤¤ë¡£

        import unittest

        class WidgetTestCase(unittest.TestCase):
            def setUp(self):
                self.widget = Widget("The widget")
            def tearDown(self):
                self.widget.dispose()
                self.widget = None
            def testDefaultSize(self):
                assert self.widget.size() == (50,50), 'incorrect default size'
            def testResize(self):
                self.widget.resize(100,150)
                assert self.widget.size() == (100,150), \
                       'wrong size after resize'
    

¤³¤³¤Ç»ä¤¿¤Á¤Ï¡¤runTest ¥á¥½¥Ã¥É¤ÎÂå¤ï¤ê¤Ë¡¤ Æó¤Ä¤Î°Û¤Ê¤Ã¤¿¥Æ¥¹¥È¡¦¥á¥½¥Ã¥É¤òÍÑ°Õ¤·¤¿¡£ ³Æ¥¯¥é¥¹¡¦¥¤¥ó¥¹¥¿¥ó¥¹¤Ïº£¤ä test ¥á¥½¥Ã¥É¤Î°ì¤Ä¤ò¤½¤ì¤¾¤ì¼Â¹Ô¤¹¤ë¡£ ³Æ¥¤¥ó¥¹¥¿¥ó¥¹¤¬¸ÄÊÌ¤Ë self.widget ¤ò¹½ÃÛ¤·¡¤Ç˲õ¤¹¤ë¡£ »ä¤¿¤Á¤Ï¡¤¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤¹¤ë¤È¤­¡¤ ¼Â¹Ô¤¹¤Ù¤­¥Æ¥¹¥È¡¦¥á¥½¥Ã¥É¤ò»ØÄꤷ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ¥³¥ó¥¹¥È¥é¥¯¥¿¤Ë¥á¥½¥Ã¥É̾¤òÅϤ¹¤³¤È¤Ç¡¤¤³¤ì¤ò»ØÄꤹ¤ë¡£

        defaultSizeTestCase = WidgetTestCase("testDefaultSize")
        resizeTestCase = WidgetTestCase("testResize")
    

¥Æ¥¹¥È¡¦¥±¡¼¥¹¤ò¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤Ë¤Þ¤È¤á¤ë

¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Ï¡¤ ¥Æ¥¹¥ÈÂоݤε¡Ç½¤Ë¤·¤¿¤¬¤Ã¤Æ¥°¥ë¡¼¥×²½¤µ¤ì¤ë¡£ PyUnit ¤Ï¤³¤Î¤¿¤á¤Î¤·¤¯¤ß¤òÍÑ°Õ¤·¤Æ¤¤¤ë¡£ ¤½¤ì¤¬¡Ö¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¡×(test suite) ¤Ç¤¢¤ë¡£ ¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤Ï unittest ¥â¥¸¥å¡¼¥ëÆâ¤Î TestSuite ¥¯¥é¥¹¤Ë¤è¤Ã¤Æɽ¸½¤µ¤ì¤ë¡£

        widgetTestSuite = unittest.TestSuite()
        widgetTestSuite.addTest(WidgetTestCase("testDefaultSize"))
        widgetTestSuite.addTest(WidgetTestCase("testResize"))
    

¥Æ¥¹¥È¼Â¹Ô¤ÎÊص¹¤Î¤¿¤á¡¤¸å¤Ç¸«¤ë¤è¤¦¤Ë¡¤ È÷¤¨ÉÕ¤±¤Î¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤òÊÖ¤¹ callable ¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤ò¡¤ ³Æ¥Æ¥¹¥È¡¦¥â¥¸¥å¡¼¥ë¤ËÍÑ°Õ¤·¤Æ¤ª¤¯¤ÈÎɤ¤¡£

       def suite():
           suite = unittest.TestSuite()
           suite.addTest(WidgetTestCase("testDefaultSize"))
           suite.addTest(WidgetTestCase("testResize"))
           return suite
    

¤¢¤ë¤¤¤Ï

       class WidgetTestSuite(unittest.TestSuite):
           def __init__(self):
               unittest.TestSuite.__init__(self,map(WidgetTestCase,
                                                     ("testDefaultSize",
                                                      "testResize")))
    

(ÌÀ¤é¤«¤Ë¸å¼Ô¤Ïµ¤¤Î¼å¤¤¿Í¸þ¤±¤Ç¤Ï¤Ê¤¤)

¿¤¯¤Î»÷¤¿¤è¤¦¤Ê̾Á°¤Î¥Æ¥¹¥È´Ø¿ô¤ò°ú¿ô¤Ë¤·¤Æ TestCase ÇÉÀ¸¥¯¥é¥¹¤òºî¤ë¤³¤È¤Ï¶¦Ä̤Υѥ¿¡¼¥ó¤À¤«¤é¡¤ unittest ¥â¥¸¥å¡¼¥ë¤Ë makeSuite ¤È¤¤¤¦Êص¹´Ø¿ô¤¬ÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë¡£ ¤³¤Î´Ø¿ô¤Ï¡¤ £±¸Ä¤Î¥Æ¥¹¥È¡¦¥±¡¼¥¹¡¦¥¯¥é¥¹¤ÎÃæ¤Ë¤¢¤ë¤¹¤Ù¤Æ¤Î¥Æ¥¹¥È¡¦¥±¡¼¥¹¤«¤é¤Ê¤ë ¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤ò¹½ÃÛ¤¹¤ë¡£

       suite = unittest.makeSuite(WidgetTestCase,'test')
    

makeSuite ´Ø¿ô¤ò»È¤¦»þ¡¤ ¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤¬ÍÍ¡¹¤Ê¥Æ¥¹¥È¡¦¥±¡¼¥¹¤ò¼Â¹Ô¤¹¤ë½ç½ø¤Ï¡¤ cmp Áȹþ´Ø¿ô¤ò»È¤Ã¤Æ¥Æ¥¹¥È´Ø¿ô¤Î̾Á°¤ò¥½¡¼¥È¤·¤Æ·èÄꤵ¤ì¤ë½ç½ø¤Ç¤¢¤ë¤³¤È¤Ë¡¤ Ãí°Õ¤µ¤ì¤¿¤¤¡£

¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤òÆþ¤ì»Ò¤Ë¤¹¤ë

¤·¤Ð¤·¤Ð¡¤¥·¥¹¥Æ¥àÁ´ÂΤËÂФ·¤Æ¥Æ¥¹¥È¤ò°ìÅ٤˼¹Ԥ¹¤ë¤¿¤á¤Ë¡¤ ¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Î¥¹¡¼¥Ä¤ò¥°¥ë¡¼¥×²½¤·¤¿¤¤¤³¤È¤¬¤¢¤ë¡£ ¤³¤ì¤Ï´Êñ¤Ç¤¢¤ë¡£ Ê£¿ô¤Î TestCase ¤ò£±¸Ä¤Î TestSuite ¤Ë²Ã¤¨¤é¤ì¤ë¤è¤¦¤Ë¡¤ Ê£¿ô¤Î TestSuite ¤ò£±¸Ä¤Î TestSuite ¤Ë²Ã¤¨¤é¤ì¤ë¤«¤é¤Ç¤¢¤ë¡£

       suite1 = module1.TheTestSuite()
       suite2 = module2.TheTestSuite()
       alltests = unittest.TestSuite((suite1, suite2))
    

¥Æ¥¹¥È¥¹¡¼¥Ä¤òÆþ¤ì»Ò¤Ë¤¹¤ëÎã¤Ï¡¤ ÇÛÉۥѥ屡¼¥¸¤Î examples ¥µ¥Ö¥Ç¥£¥ì¥¯¥È¥ê¤Î alltests.py ¤Ë¸«¤é¤ì¤ë¡£

¥Æ¥¹¥È¤ò¤¹¤ë¥³¡¼¥É¤ÎÃÖ¤­¾ì½ê

¥Æ¥¹¥È¡¦¥±¡¼¥¹¤È¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤ÎÄêµÁ¤ò¡¤ ¥Æ¥¹¥ÈÂоݤΥ³¡¼¥É¤ÈƱ¤¸¥â¥¸¥å¡¼¥ë (Î㤨¤Ð 'widget.py') ¤ËÃÖ¤¤¤Æ¤â¤è¤¤¤¬¡¤ ¥Æ¥¹¥È¡¦¥³¡¼¥É¤òÊ̸ĤΥ⥸¥å¡¼¥ë¡¤Î㤨¤Ð 'widgettests.py' ¤ËÃÖ¤¯¤³¤È¤Ë¤Ï¤¤¤¯¤Ä¤«¤ÎÍøÅÀ¤¬¤¢¤ë¡£

ÂÐÏÃŪ¤Ë¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë

¤â¤Á¤í¤ó¡¤¤³¤ì¤é¤Î¥Æ¥¹¥È¤ò½ñ¤¯Á´ÂΤÎÌÜŪ¤Ï¡¤ »ä¤¿¤Á¤¬¥Æ¥¹¥È¤ò¼Â¹Ô¤Ç¤­¤ë¤è¤¦¤Ë¤¹¤ë¤³¤È¡¤ ¤½¤·¤Æ»ä¤¿¤Á¤Î¥½¥Õ¥È¥¦¥§¥¢¤¬Æ¯¤¯¤«¤É¤¦¤«Ê¬¤«¤ë¤è¤¦¤Ë¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£ ¥Æ¥¹¥È¡¦¥Õ¥ì¡¼¥à¥ï¡¼¥¯¤Ï¡¤¤¤¤¯¤Ä¤«¤Î 'TestRunner' ¥¯¥é¥¹¤ò»È¤Ã¤Æ¡¤ ¥Æ¥¹¥È¤¬¼Â¹Ô¤Ç¤­¤ë´Ä¶­¤òÍÑ°Õ¤·¤Æ¤¤¤ë¡£ ºÇ¤â°ìÈÌŪ¤Ê TestRunner ¤Ï TextTestRunner ¤Ç¤¢¤ë¡£ ¤³¤ì¤Ï¥Æ¥¹¥È¤ò¼Â¹Ô¤·¡¤¥Æ¥­¥¹¥È·Á¼°¤Ç·ë²Ì¤òÊó¹ð¤¹¤ë¡£

        runner = unittest.TextTestRunner()
        runner.run(widgetTestSuite)
    

¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¤TextTestRunner ¤Ï¤½¤Î½ÐÎϤò sys.stderr ¤Ë°õ»ú¤¹¤ë¡£ ¤·¤«¤·¡¤ ¤³¤ì¤Ï¥³¥ó¥¹¥È¥é¥¯¥¿¤ËÊ̤Υե¡¥¤¥ë=¥é¥¤¥¯¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤òÅϤ¹¤³¤È¤Ç¡¤ Êѹ¹¤Ç¤­¤ë¡£

¤³¤Î¤è¤¦¤Ê TextTestRunner ¤Î»ÈÍѤϡ¤Python ¥¤¥ó¥¿¡¼¥×¥ê¥¿¤Î¥»¥Ã¥·¥ç¥ó¤«¤éÂÐÏÃŪ¤Ë¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ëÍýÁÛŪ¤ÊÊýË¡¤Ç¤¢¤ë¡£

¥³¥Þ¥ó¥É¹Ô¤«¤é¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë

unittest ¥â¥¸¥å¡¼¥ë¤Ë main ¤È¤¤¤¦´Ø¿ô¤¬¤¢¤ë¡£ ¤³¤Î´Ø¿ô¤ò»È¤¦¤È¡¤¥Æ¥¹¥È¡¦¥â¥¸¥å¡¼¥ë¤ò¡¤ ¤½¤Î¥â¥¸¥å¡¼¥ëÆâ¤Î¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë¥¹¥¯¥ê¥×¥È¤Ø¤È´Êñ¤ËÊѤ¨¤é¤ì¤ë¡£ main ´Ø¿ô¤Ï¡¤unittest.TestLoader ¥¯¥é¥¹¤ò»È¤Ã¤Æ¡¤ ¸½ºß¤Î¥â¥¸¥å¡¼¥ëÆâ¤Î¥Æ¥¹¥È¡¦¥±¡¼¥¹¤ò¼«Æ°Åª¤Ë¸«ÉÕ¤±¤Æ¥í¡¼¥É¤¹¤ë¡£

¤·¤¿¤¬¤Ã¤Æ¡¤¤â¤··¯¤¬Á°½Ò¤Î test* ¤È¤¤¤¦Ì¿Ì¾´·½¬¤ò»È¤Ã¤Æ¥Æ¥¹¥È¡¦¥á¥½¥Ã¥É¤ò̿̾¤·¤Æ¤¤¤ë¤Ê¤é¤Ð¡¤ ·¯¤Î¥Æ¥¹¥È¡¦¥â¥¸¥å¡¼¥ë¤ÎËöÈø¤Ë²¼µ­¤Î¥³¡¼¥É¤òÃÖ¤¯¤³¤È¤¬¤Ç¤­¤ë¡£

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

¤¹¤ë¤È¡¤·¯¤¬¥³¥Þ¥ó¥É¹Ô¤«¤é·¯¤Î¥Æ¥¹¥È¡¦¥â¥¸¥å¡¼¥ë¤ò¼Â¹Ô¤·¤¿»þ¡¤ ¤½¤³¤Ë´Þ¤Þ¤ì¤ë¥Æ¥¹¥È¤¬¤¹¤Ù¤Æ¼Â¹Ô¤µ¤ì¤ë¡£ ÍøÍѲÄǽ¤Ê¥ª¥×¥·¥ç¥ó¤ò¸«¤ë¤Ë¤Ï¡¤ '-h' ¥ª¥×¥·¥ç¥ó¤òÉÕ¤±¤Æ¥â¥¸¥å¡¼¥ë¤ò¼Â¹Ô¤¹¤ì¤Ð¤è¤¤¡£

¥³¥Þ¥ó¥É¹Ô¤«¤éǤ°Õ¤Î¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë¤Ë¤Ï¡¤ unittest ¥â¥¸¥å¡¼¥ë¤ò¥¹¥¯¥ê¥×¥È¤È¤·¤Æ¼Â¹Ô¤¹¤ì¤Ð¤è¤¤¡£ ¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Þ¤¿¤Ï¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤Î̾Á°¤ò°ú¿ô¤È¤·¤ÆÍ¿¤¨¤ë¡£

        % python unittest.py widgettests.WidgetTestSuite
    

¤Þ¤¿¤Ï

        % python unittest.py widgettests.makeWidgetTestSuite
    

¥³¥Þ¥ó¥É¹Ô¤ÇÆÃÄê¤Î¥Æ¥¹¥È¤ò»ØÄꤹ¤ë¤³¤È¤â¤Ç¤­¤ë¡£ ¥â¥¸¥å¡¼¥ë 'listtests' ¤Î TestCase ÇÉÀ¸¥¯¥é¥¹¤Ç¤¢¤ë 'ListTestCase' (ÇÛÉۥѥ屡¼¥¸¤Î 'examples' ¥µ¥Ö¥Ç¥£¥ì¥¯¥È¥ê»²¾È) ¤òÁö¤é¤»¤ë¤Ë¤Ï¡¤¼¡¤Î¥³¥Þ¥ó¥É¤ò¼Â¹Ô¤¹¤ì¤Ð¤è¤¤¡£

        % python unittest.py listtests.ListTestCase.testAppend
    

¤³¤³¤Ç 'testAppend' ¤Ï¡¤ ¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤¬¼Â¹Ô¤¹¤Ù¤­¥Æ¥¹¥È¡¦¥á¥½¥Ã¥É¤Î̾Á°¤Ç¤¢¤ë¡£ ListTestCase ¥¯¥é¥¹¤Î¤¹¤Ù¤Æ¤Î 'test*' ¥á¥½¥Ã¥É¤ËÂФ·¤ÆƱ¥¯¥é¥¹¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤·¤ÆÁö¤é¤»¤ë¤Ë¤Ï¡¤ ¼¡¤ò¼Â¹Ô¤¹¤ì¤Ð¤è¤¤¡£

        % python unittest.py listtests.ListTestCase
    

GUI ¥Æ¥¹¥È¡¦¥é¥ó¥Ê

¥Æ¥¹¥È¤ò¼Â¹Ô¤¹¤ë¤¿¤á¤ËÍøÍѤǤ­¤ë¥°¥é¥Õ¥£¥«¥ë¤Ê¥Õ¥í¥ó¥È¡¦¥¨¥ó¥É¤¬¤¢¤ë¡£ ¤½¤ì¤Ï¡¤Âç¿¿ô¤Î¥×¥é¥Ã¥È¥Õ¥©¡¼¥à¾å¤Ç Python ¤È¤È¤â¤Ë½Ð²Ù¤µ¤ì¤Æ¤¤¤ë¥¦¥£¥ó¥É¥¦²½¥Ä¡¼¥ë¥­¥Ã¥È¤Ç¤¢¤ë Tkinter ¤ò»È¤Ã¤Æ½ñ¤«¤ì¤Æ¤¤¤ë¡£ ¤½¤Î³°´Ñ¤Ï JUinit GUI ¤È»÷¤Æ¤¤¤ë¡£

GUI ¥Æ¥¹¥È¡¦¥é¥ó¥Ê¤ò»È¤¦¤Ë¤Ï¡¤Ã±½ã¤Ë¼¡¤ò¼Â¹Ô¤¹¤ë¡£

        % python unittestgui.py
    
¤Þ¤¿¤Ï
        % python unittestgui.py widgettests.WidgetTestSuite
    

¤³¤³¤Ç¤â¡¤¤ä¤Ï¤ê¡¤¼Â¹Ô¤¹¤Ù¤­¥Æ¥¹¥È¤È¤·¤ÆÆþÎϤ¹¤ë̾Á°¤Ï¡¤ TestCase ¤Þ¤¿¤Ï TestSuite ¤Î ¥¤¥ó¥¹¥¿¥ó¥¹¤òÌá¤êÃͤȤ¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Î´°Á´½¤¾þ̾ (fully-qualified name) ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ¤É¤Î¥Æ¥¹¥È¤â¼Â¹Ô¤Î¤¿¤Ó¤ËºÆ¹½ÃÛ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤«¤é¡¤ ¹½ÃۺѤߤΥƥ¹¥È¤Î̾Á°¤Ç¤¢¤Ã¤Æ¤Ï¤¤¤±¤Ê¤¤¡£

¥Æ¥­¥¹¥È¡¦¥Æ¥¹¥È¡¦¥é¥ó¥Ê¤ÎÂå¤ï¤ê¤Ë GUI ¥Æ¥¹¥È¡¦¥é¥ó¥Ê¤òÍøÍѤ¹¤ë¤È¡¤ Îã¤Î¥¦¥£¥ó¥É¥¦¹¹¿·¤Î¤¿¤á¤Ë»þ´Ö¥ª¡¼¥Ð¥Ø¥Ã¥É¤¬²Ý¤»¤é¤ì¤ë¡£ »ä¤Î¥·¥¹¥Æ¥à¤Ç¤Ï¡¤1000 ¥Æ¥¹¥È¤¢¤¿¤ê 7 ÉÃ;ʬ¤Ë¤«¤«¤ë¡£ ¤³¤Î¿ôÃͤϴĶ­¤Ë¤è¤êÊѤï¤ë¤«¤â¤·¤ì¤Ê¤¤¡£

¥Æ¥¹¥È¤ò¥É¥­¥å¥á¥ó¥È²½¤¹¤ë

Ä̾¥Æ¥¹¥È¤Î¼Â¹Ô»þ¤Ë¤Ï¡¤ ¥Æ¥¹¥È¤Î̾Á°¤¬ TestRunner ¤Ë¤è¤Ã¤Æɽ¼¨¤µ¤ì¤ë¡£ ¤³¤Î̾Á°¤Ï¡¤¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Î¥¯¥é¥¹Ì¾¤È¡¤ ¤½¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤¬¼Â¹Ô¤¹¤ë¤è¤¦¤Ë½é´ü²½¤µ¤ì¤¿¥Æ¥¹¥È¡¦¥á¥½¥Ã¥É¤Î̾Á°¤«¤é¡¤ Ƴ½Ð¤µ¤ì¤ë¡£

¤·¤«¤·¡¤¤â¤··¯¤¬¥Æ¥¹¥È¡¦¥á¥½¥Ã¥É¤Ë doc-string ¤òÍ¿¤¨¤ì¤Ð¡¤ ¤½¤Î doc-string ¤ÎºÇ½é¤Î¹Ô¤¬¡¤¥Æ¥¹¥È¤Î¼Â¹Ô»þ¤Ëɽ¼¨¤µ¤ì¤ë¡£ ¤³¤ì¤Ï·¯¤Î¥Æ¥¹¥È¤ò¥É¥­¥å¥á¥ó¥È²½¤¹¤ë´Êñ¤Ê¤·¤¯¤ß¤òÍÑ°Õ¤¹¤ë¡£

        class WidgetTestCase(unittest.TestCase):
            def testDefaultSize(self):
                """Check that widgets are created with correct default size"""
                assert self.widget.size() == (50,50), 'incorrect default size'
    

¥Æ¥¹¥È¾ò·ï¤Ë¤Ä¤¤¤Æ¤ÎÊä­

»ä¤Ï¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Î¾ò·ï¤ò¥Á¥§¥Ã¥¯¤¹¤ë¤¿¤á¤Ë ¡Ö¼«²ÈÀ½¡×¤ÎɽÌÀµ¡¹½¤Ç¤Ï¤Ê¤¯¡¤ Python ¤ËÁȹþ¤ß¤ÎɽÌÀµ¡¹½¤ò»È¤¦¤³¤È¤ò´«¤á¤Æ¤­¤¿¡£ assert ¤Ïñ½ã¤Ç¤¢¤ê¡¤´ÊÌÀ¤Ç¤¢¤ê¡¤¤Ê¤¸¤ß¤â¤¢¤ë¡£

¤·¤«¤·¡¤¥Æ¥¹¥È¤ò Python ¤Î ('.pyo' ¥Ð¥¤¥È¥³¡¼¥É¡¦¥Õ¥¡¥¤¥ë¤òÀ¸À®¤¹¤ë) ºÇŬ²½¥ª¥×¥·¥ç¥óÉÕ¤­¤Ç¼Â¹Ô¤·¤¿¾ì¹ç¤Ï¡¤ assert ʸ¤Ï¥¹¥­¥Ã¥×¤µ¤ì¡¤ ¥Æ¥¹¥È¡¦¥±¡¼¥¹¤¬ÌòΩ¤¿¤º¤Ë¤Ê¤ë¡£

Python ¤ÎºÇŬ²½¥ª¥×¥·¥ç¥ó¤òÍ­¸ú¤Ë¤·¤Æºî¶È¤·¤¿¤¤¿Í¡¹¤Î¤¿¤á¤Ë¡¤ »ä¤Ï¥á¥½¥Ã¥É assert_ ¤ò TestCase ¥¯¥é¥¹¤Ë´Þ¤á¤¿¡£ ¤³¤ì¤ÏÁȹþ¤ß¤Î assert ¤Èµ¡Ç½Åª¤ËÅù²Á¤À¤¬¡¤ ºÇŬ²½¤Ë¤è¤Ã¤Æ¾Ã¤¨¤Ê¤¤¡£ ¤·¤«¤·¡¤¤è¤êÉÔÊؤǤ¢¤ê¡¤¤è¤ê½õ¤±¤Ë¤Ê¤é¤Ê¤¤¥¨¥é¡¼¡¦¥á¥Ã¥»¡¼¥¸¤Ë½ª¤ï¤ë¡£

        def runTest(self):
            self.assert_(self.widget.size() == (100,100), "size is wrong")
    

¤ª¤Þ¤±¤È¤·¤Æ¡¤»ä¤Ï TestCase ¤Ë failIf ¤È failUnless ¤Î¥á¥½¥Ã¥É¤âÍÑ°Õ¤·¤¿¡£

        def runTest(self):
            self.failIf(self.widget.size() <> (100,100))
    

¥Æ¥¹¥È¡¦¥±¡¼¥¹¤Ï fail ¤ò¸Æ¤Ó½Ð¤·¤Æ¡¤ ¨ºÂ¤Ë¼ºÇÔ¤¹¤ë¤³¤È¤â¤Ç¤­¤ë¡£

        def runTest(self):
            ...
            if not hasattr(something, "blah"):
                self.fail("blah missing")
                # or just 'self.fail()'
    

Åù¤·¤µ¤ò¥Æ¥¹¥È¤¹¤ë

ºÇ¤â°ìÈÌŪ¤Ê¥¿¥¤¥×¤ÎɽÌÀ¤Ï¡¤ Æó¤Ä¤ÎÃͤޤ¿¤Ï¥ª¥Ö¥¸¥§¥¯¥È¤Î´Ö¤ÎÅù¤·¤µ¤ÎɽÌÀ¤Ç¤¢¤ë¡£ ɽÌÀ¤¬¼ºÇÔ¤·¤¿»þ¡¤ ³«È¯¼Ô¤Ï¤½¤ÎÉÔÀµÃͤ¬¼ÂºÝ¤Ï¤É¤ó¤ÊÃͤÀ¤Ã¤¿¤Î¤«¤ò¸«¤¿¤¤¤Î¤¬ÉáÄ̤Ǥ¢¤ë¡£

TestCase ¤Ë¤Ï¡¤¤³¤ÎÌÜŪ¤Î¤¿¤á¤Ë assertEqual ¤È assertNotEqual ¤È¤¤¤¦¥á¥½¥Ã¥É¤Î¥Ú¥¢¤¬¤¢¤ë¡£ (¹¥¤ß¤Ë±þ¤¸¤Æ failUnlessEqual and failIfEqual ¤È¤¤¤¦ÊÌ̾¤â¤¢¤ë)

        def testSomething(self):
            self.widget.resize(100,100)
            self.assertEqual(self.widget.size, (100,100))
    

Îã³°¤ò¥Æ¥¹¥È¤¹¤ë

¥Æ¥¹¥È¤Ç¤Ï¡¤¤·¤Ð¤·¤Ð¡¤ ¤¢¤ë¾õ¶·¤ÇÎã³°¤¬¤Ò¤­µ¯¤³¤µ¤ì¤ë¤³¤È¤ò¥Á¥§¥Ã¥¯¤·¤¿¤¤¤³¤È¤¬¤¢¤ë¡£ ¤â¤·´üÂÔ¤µ¤ì¤ëÎã³°¤¬Á÷½Ð¤µ¤ì¤Ê¤±¤ì¤Ð¡¤¥Æ¥¹¥È¤Ï¼ºÇÔ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ¤³¤ì¤Ï´Êñ¤Ë¤Ç¤­¤ë¡£

        def runTest(self):
            try:
                self.widget.resize(-1,-1)
            except ValueError:
                pass
            else:
                fail("expected a ValueError")
    

ÉáÄÌ¡¤´üÂÔ¤µ¤ì¤ëÎã³°¤ÎȯÀ¸¸»¤Ï callable ¥ª¥Ö¥¸¥§¥¯¥È¤À¤«¤é¡¤ TestCase ¤Ë¤Ï assertRaises ¥á¥½¥Ã¥É¤¬¤¢¤ë¡£ ¥á¥½¥Ã¥É¤ÎºÇ½é¤ÎÆó¤Ä¤Î°ú¿ô¤Ï¡¤ except Àá¤Ë¸½¤ï¤ì¤ë¤è¤¦¤Ê´üÂÔ¤µ¤ì¤ëÎã³°¤È¡¤callable ¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ë¡£ »Ä¤ê¤Î°ú¿ô¤Ï callable ¥ª¥Ö¥¸¥§¥¯¥È¤ËÅϤµ¤ì¤ë°ú¿ô¤Ç¤¢¤ë¡£

        def runTest(self):
            self.assertRaises(ValueError, self.widget.resize, -1, -1)
    

ÀΤΥƥ¹¥È¡¦¥³¡¼¥É¤ò PyUnit ¤ÇºÆÍøÍѤ¹¤ë

´û¸¤Î¥Æ¥¹¥È¡¦¥³¡¼¥É¤ò»ý¤Ã¤Æ¤¤¤Æ¡¤PyUnit ¤«¤é¼Â¹Ô¤µ¤»¤¿¤¤¤¬¡¤ ¸Å¤¤¥Æ¥¹¥È´Ø¿ô¤ò TestCase ÇÉÀ¸¥¯¥é¥¹¤Ë¤¤¤Á¤¤¤ÁÊÑ´¹¤·¤¿¤¯¤Ï¤Ê¤¤¡¤ ¤È¤¤¤¦ÍøÍѼԤ⤤¤ë¤À¤í¤¦¡£

¤½¤Î¤¿¤á¤Ë¡¤ PyUnit ¤Ï FunctionTestCase ¥¯¥é¥¹¤òÍÑ°Õ¤·¤Æ¤¤¤ë¡£ ¤³¤ì¤Ï TestCase ¤ÎÇÉÀ¸¥¯¥é¥¹¤Ç¤¢¤ê¡¤ ´û¸¤Î¥Æ¥¹¥È´Ø¿ô¤ò¥é¥Ã¥×¤¹¤ë¤¿¤á¤Ë»È¤ï¤ì¤ë¡£ Á°½àÈ÷¤È¸åÊÒÉÕ¤±¤Î´Ø¿ô¤â¥ª¥×¥·¥ç¥ó¤È¤·¤Æ¥é¥Ã¥×¤Ç¤­¤ë¡£

²¼µ­¤Î¥Æ¥¹¥È´Ø¿ô¤¬Í¿¤¨¤é¤ì¤¿¤È¤­¡¤

        def testSomething():
            something = makeSomething()
            assert something.name is not None
            ...
    

¼¡¤Î¤è¤¦¤Ë¤·¤ÆÅù²Á¤Ê¥Æ¥¹¥È¡¦¥±¡¼¥¹¡¦¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃۤǤ­¤ë¡£

        testcase = unittest.FunctionTestCase(testSomething)
    

¤â¤·¥Æ¥¹¥È¡¦¥±¡¼¥¹¤ÎÁàºî¤Î°ìÉô¤È¤·¤Æ¸Æ¤Ó½Ð¤µ¤ì¤ë¤Ù¤­ÉÕ²ÃŪ¤Ê¡¤ Á°½àÈ÷¤È¸åÊÒÉÕ¤±¤Î¥á¥½¥Ã¥É¤¬¤¢¤ì¤Ð¡¤¤½¤ì¤é¤âÍ¿¤¨¤ë¤³¤È¤¬¤Ç¤­¤ë¡£

        testcase = unittest.FunctionTestCase(testSomething,
                                             setUp=makeSomethingDB,
                                             tearDown=deleteSomethingDB)
    

PyUnit ¤ò JPython ¤È Jython ¤Ç»È¤¦¤Ë¤Ï

PyUnit ¤ÏËÜÍè 'C' Python ¤Î¤¿¤á¤Ë½ñ¤«¤ì¤¿¤¬¡¤ ·¯¤Î Java ¤Þ¤¿¤Ï Jython ¤Î¥½¥Õ¥È¥¦¥§¥¢¤Î¤¿¤á¤Ë¡¤ Jython ¤ò»È¤Ã¤¿ PyUnit ¥Æ¥¹¥È¤ò½ñ¤¯¤³¤È¤Ï²Äǽ¤Ç¤¢¤ë¡£ Jython ¤ò»È¤Ã¤Æ JUnit ¥Æ¥¹¥È¤ò½ñ¤³¤¦¤È¤¹¤ë¤è¤ê¤â¡¤ ¤³¤Á¤é¤Î¤Û¤¦¤¬¹¥¤Þ¤·¤¤¤«¤â¤·¤ì¤Ê¤¤¡£ PyUnit ¤Ï Jython ¤ÎÀèÁĤǤ¢¤ë JPython 1.0 ¤ª¤è¤Ó 1.1 ¤Ç¤âÀµ¤·¤¯Æ°ºî¤¹¤ë¡£

¤â¤Á¤í¤ó¡¤Java ¤Ï TK GUI ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò»ý¤¿¤Ê¤¤¤«¤é¡¤ PyUnit ¤Î Tkinter ¥Ù¡¼¥¹¤Î GUI ¤Ï Jython ¤ÇÆ°ºî¤·¤Ê¤¤¡£ ¤·¤«¤·¡¤¥Æ¥­¥¹¥È¤À¤±¤Î¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ï¥Ð¥Ã¥Á¥êÆ°ºî¤¹¤ë¡£

¤½¤¦¤¹¤ë¤¿¤á¤Ë¤Ïñ¤Ëɸ½à C Python ¥é¥¤¥Ö¥é¥ê ¥â¥¸¥å¡¼¥ë¤Î¥Õ¥¡¥¤¥ë 'traceback.py', 'linecache.py', 'stat.py', 'getopt.py' ¤ò JPython ¤«¤é import ¤Ç¤­¤ë¾ì½ê¤Ë¥³¥Ô¡¼¤¹¤ë¤À¤±¤Ç¤è¤¤¡£ ¤³¤ì¤é¤Î¥Õ¥¡¥¤¥ë¤Ï C Python ¤Î¤É¤Î¥Ç¥£¥¹¥È¥ê¥Ó¥å¡¼¥·¥ç¥ó¤«¤é¤âÆþ¼ê¤Ç¤­¤ë¡£ (¤³¤Î¥¬¥¤¥É¥é¥¤¥ó¤Ï C Python 1.5.x ¤Îɸ½à¥é¥¤¥Ö¥é¥ê¤Ë´ð¤Å¤¤¤Æ¤¤¤ë¤«¤é¡¤ ¾¤Î¥Ð¡¼¥¸¥ç¥ó¤Î Python ¤Ë¤ÏÅö¤Æ¤Ï¤Þ¤é¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤)

¤½¤¦¤¹¤ì¤Ð·¯¤Ï PyUnit ¥Æ¥¹¥È¤ò C Python ¤Î¤È¤­¤ÈÁ´¤¯Æ±Íͤ˽ñ¤¯¤³¤È¤¬¤Ç¤­¤ë¡£

¤¿¤À¤·½ñ¤­

ɽÌÀ

¾å½Ò¤Î"¥Æ¥¹¥È¾ò·ï¤Ë¤Ä¤¤¤Æ¤ÎÊä­" Àá¤Î¤¿¤À¤·½ñ¤­¤ò»²¾È¤µ¤ì¤¿¤¤¡£

¥á¥â¥ê»ÈÍÑ

¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤Î¼Â¹ÔÃæ¤ËÎã³°¤¬¤Ò¤­µ¯¤³¤µ¤ì¤¿¤È¤­¡¤ ·ë²Ì¤È¤·¤Æ¤Î traceback ¥ª¥Ö¥¸¥§¥¯¥È¤¬¡¤ ¼ºÇԤξܺ٤ò¥Æ¥¹¥È¼Â¹Ô¤Î½ª¤ï¤ê¤Ë½ñ¼°²½¤·¤Æ°õ»ú¤Ç¤­¤ë¤è¤¦¤Ë¤¹¤ë¤¿¤á¡¤ ÊÝᤵ¤ì¤ë¡£Ã±½ã¤µ¤Ï¤µ¤Æ¤ª¤­¡¤¤³¤ÎÍøÅÀ¤Ï¡¤ traceback ¤Ë³ÊǼ¤µ¤ì¤¿¥í¡¼¥«¥ë¤ª¤è¤Ó¥°¥í¡¼¥Ð¥ëÊÑ¿ôÃͤθ¡»àÄ´ºº¤¬¡¤ ¾­Íè¤Î GUI TestRunner ¤Ë¤È¤Ã¤Æ²Äǽ¤Ë¤Ê¤êÆÀ¤ë¤È¤¤¤¦ÅÀ¤Ç¤¢¤ë¡£

ÀøºßŪ¤ÊÉûºîÍѤϡ¤¼ºÇÔÉÑÅÙ¤¬Èó¾ï¤Ë¹â¤¤¥Æ¥¹¥È¡¦¥¹¡¼¥Ä¤ò¼Â¹Ô¤·¤¿¤È¤­¡¤ ¤³¤ì¤é¤ÎÊÝᤵ¤ì¤¿ traceback ¥ª¥Ö¥¸¥§¥¯¥ÈÁ´ÂΤΥá¥â¥ê»ÈÍѤ¬ÌäÂê¤Ë¤Ê¤êÆÀ¤ë¤È¤¤¤¦ÅÀ¤Ç¤¢¤ë¡£ ¤â¤Á¤í¤ó¡¤Èó¾ï¤Ë¿¤¯¤Î¥Æ¥¹¥È¤¬¼ºÇÔ¤¹¤ë¤È¤·¤¿¤é¡¤ ¤³¤Î¥á¥â¥ê¡¦¥ª¡¼¥Ð¥Ø¥Ã¥É¤Ï·¯¤Ë¤È¤Ã¤ÆºÇ¤âº³ºÙ¤ÊÌäÂê¤Ç¤¢¤ë¡£

ÍøÍѾò·ï

·¯¤Ï¤³¤Î¥½¥Õ¥È¥¦¥§¥¢¤ò¡¤Python ¤½¤ì¼«¿È¤ËŬÍѤµ¤ì¤ëƱ¤¸¤¯¼«Í³¤Ê¾ò·ï¤Î²¼¤Ç¡¤ ¼«Í³¤ËÍøÍÑ¡¤²þÊÑ¡¤¤ª¤è¤ÓºÆÇÛÉÛ¤·¤Æ¤è¤¤¡£ »ä¤¬Í׵᤹¤ë¤³¤È¤Ï¤¿¤À¡¤»ä¤Î̾Á°¤È e-mail ¥¢¥É¥ì¥¹¤È¥×¥í¥¸¥§¥¯¥È URL ¤¬¥½¡¼¥¹¥³¡¼¥É¤ª¤è¤ÓÉտ魯¤ë¥É¥­¥å¥á¥ó¥Æ¡¼¥·¥ç¥ó¤Ë¡¤ »ä¤ò¸¶Ãø¼Ô¤È¤·¤Æǧ¤á¤ëµ­½Ò¤È¤È¤â¤Ë¡¤°Ý»ý¤µ¤ì¤ë¤³¤È¤Ç¤¢¤ë¡£
You may freely use, alter and redistribute this software under the same liberal terms that apply to Python itself. All I ask is that my name, e-mail address and the project URL be retained in the source code and accompanying documentation, with a credit for me as the original author.

»ä¤¬¤³¤Î¥½¥Õ¥È¥¦¥§¥¢¤ò½ñ¤¤¤¿Æ°µ¡¤Ï¡¤ À¤³¦¤Î¥½¥Õ¥È¥¦¥§¥¢ÉʼÁ¤Î²þÁ±¤Ë¾®¤µ¤Ê¹×¸¥¤ò¤¹¤ë¤³¤È¤À¤Ã¤¿¡£ »ä¤ÏÁ´Á³¸½¶â¤òÆÀ¤ë¤³¤È¤Ï´üÂÔ¤·¤Ê¤«¤Ã¤¿¡£ (¸å±ç¤ò´¿·Þ¤·¤Ê¤¤¤È¸À¤Ã¤Æ¤¤¤ë¤ï¤±¤Ç¤Ï¤Ê¤¤¤Ç¤¹)
My motive for writing this software was to make a small contribution to the improvement of software quality in the world; I didn't bargain on getting any cash. (That's not to say that sponsorship would be unwelcome.)

¾­Íè¤Î·×²è

¾­Íè¤Ë¸þ¤±¤Æ¤Î°ì¤Ä¤Î¼çÍפʷײè¤Ï TK GUI ¤È IDLE IDE ¤ÎÅý¹ç¤Ç¤¹¡£ ¥Ü¥é¥ó¥Æ¥£¥¢¤ò´¿·Þ¤·¤Þ¤¹¡ª

¤½¤ì°Ê³°¡¤»ä¤Ë¤Ï¥â¥¸¥å¡¼¥ë¤Îµ¡Ç½¤ò³ÈÄ¥¤¹¤ëÂ礭¤Ê·×²è¤Ï¤¢¤ê¤Þ¤»¤ó¡£ »ä¤Ï PyUnit ¤ò½ÐÍè¤ë¸Â¤êñ½ã¤Ë (¤·¤«¤·¡¤½ÐÍè¤ì¤Ð¡¤ ñ½ã¤Ë¤Ê¤ê¤¹¤®¤Ê¤¤¤è¤¦¤Ë¡ª) ÊݤäƤ­¤Þ¤·¤¿¡£ ¤Ê¤¼¤Ê¤é¡¤ ¥í¥°¡¦¥Õ¥¡¥¤¥ëÈæ³Ó¤Ê¤É¤Î¶¦Ä̤Υƥ¹¥Èºî¶È¤Î¤¿¤á¤Î¥Ø¥ë¥Ñ¡¼¡¦¥â¥¸¥å¡¼¥ë¤Ï¡¤ »ä¼«¿È¤è¤ê¤â¥Æ¥¹¥È¤Îºî¼Ô¤Î¤Û¤¦¤¬¤è¤êÎɤ¯½ñ¤±¤ë¤È¿®¤º¤ë¤«¤é¤Ç¤¹¡£

¥¢¥Ã¥×¥Ç¡¼¥È¤È¥³¥ß¥å¥Ë¥Æ¥£

¥Ë¥å¡¼¥¹¡¤¥¢¥Ã¥×¥Ç¡¼¥È¡¤¤½¤Î¾¤Ï ¥×¥í¥¸¥§¥¯¥È¤Î¥¦¥§¥Ö¥µ¥¤¥È ¤ÇÆþ¼ê¤Ç¤­¤Þ¤¹¡£

¥³¥á¥ó¥È¡¤Äó°Æ¡¤¥Ð¥°Êó¹ð¤ò´¿·Þ¤·¤Þ¤¹¡£ ñ½ã¤Ë»ä¤Ë e-mail ¤¹¤ë¤«¡¤¤Þ¤¿¤Ï¡¤¤³¤Á¤é¤Î¤Û¤¦¤¬¤¤¤Ã¤½¤¦Îɤ¤¤Î¤Ç¤¹¤¬¡¤ Èó¾ï¤ËÄãήÎ̤Π¥á¡¼¥ê¥ó¥°¡¦¥ê¥¹¥È ¤ËÆþ¤Ã¤Æ¥³¥á¥ó¥È¤òÅê¹Æ¤·¤Æ¤¯¤À¤µ¤¤¡£ ¶Ã¤¯¤Ù¤­¿ô¤Î¿Í¡¹¤¬¤¹¤Ç¤Ë PyUnit ¤ò»È¤Ã¤Æ¤ª¤ê¡¤ Èà¤é¤Ï¤ß¤Ê¶¦Í­¤¹¤Ù¤­Ãηäò»ý¤Ã¤Æ¤¤¤Þ¤¹¡£

¼Õ¼­

Python ¸À¸ì¤ËÂФ·¤Æ Guido ¤ÈÈà¤ÎÌçÄï¤Ë¿¤¤¤Ë´¶¼Õ¤·¤Þ¤¹¡£ ´¶¼Õ¤Î°õ¤Ë¡¤¼¡¤Î haiku (¤¢¤ë¤¤¤Ï¡¤ ¤â¤·¤½¤¦¸Æ¤Ó¤¿¤¤¤Ê¤é 'pyku') ¤ò½ñ¤­¤Þ¤·¤¿¡£

Guido van Rossum
'Gawky Dutchman' gave birth to
Beautiful Python

JUnit ¤ÎÏ«ºî¤ËÂФ·¤Æ Kent Beck ¤È Erich Gamma ¤Î¸ùÀÓ¤ò´î¤ó¤Çǧ¤á¤Þ¤¹¡£ ¤½¤ì¤Ï PyUnit ¤ÎÀ߷פò¡¤Æ¬Ç¾¤ò¤ï¤º¤é¤ï¤»¤Ê¤¤¤â¤Î¤Ë¤·¤Þ¤·¤¿¡£

Tim Voght ¤Ë¤â´¶¼Õ¤·¤Þ¤¹¡£ »ä¤¬ PyUnit ¤ò¼ÂÁõ¤·¤¿¸å¡¤Èà¤â¤Þ¤¿ `pyunit' ¥â¥¸¥å¡¼¥ë¤òÈà¤Î 'PyWiki' ¤È¤¤¤¦ WikiWikiWeb ¥¯¥í¡¼¥ó¤Î°ìÉô¤È¤·¤Æ¼ÂÁõ¤·¤Æ¤¤¤¿¤Î¤òȯ¸«¤·¤Þ¤·¤¿¡£ Èà¤Ï¿ÆÀڤˤ⡤ »ä¤¬¼«Ê¬¤Î¥Ð¡¼¥¸¥ç¥ó¤ò°ìÈÌ¥³¥ß¥å¥Ë¥Æ¥£¤Ë½Ð¤¹¤³¤È¤òµö¤·¤Æ¤¯¤À¤µ¤¤¤Þ¤·¤¿¡£

»ä¤ËÄó°Æ¤È¼ÁÌä¤Î¥á¡¼¥ë¤ò½ñ¤¤¤¿¿Í¡¹¤Ë¿¤¤¤Ë´¶¼Õ¤·¤Þ¤¹¡£ »ä¤Ï¥À¥¦¥ó¥í¡¼¥É¡¦¥Ñ¥Ã¥±¡¼¥¸¤Î CHANGES ¥Õ¥¡¥¤¥ë¤ËŬÀڤʥ¯¥ì¥¸¥Ã¥È¤ò½ñ¤­²Ã¤¨¤ë¤è¤¦¤ËÅؤá¤Þ¤·¤¿¡£

¤È¤ê¤ï¤± Jérôme Marant ¤Ë´¶¼Õ¤·¤Þ¤¹¡£ PyUnit ¤ò Debian ÍѤ˥ѥ屡¼¥¸²½¤·¤Æ¤¯¤À¤µ¤¤¤Þ¤·¤¿¡£

´ØÏ¢¾ðÊó


Ãø¼Ô¤Ë¤Ä¤¤¤Æ

Steve Purcell is just a programmer at heart, working independently writing, applying and teaching Open Source software.

He recently acted as Technical Director for a Web/WAP start-up, but spends most of his time architecting and coding large Java systems whilst counterproductively urging his Java-skilled colleagues to take up Python instead.


Steve Purcell, <stephen_purcell at yahoo dot com>
$Id: pyunit_ja.html,v 1.1 2003/09/22 14:14:30 steve Exp $