Python单元测试PyUnit例子

1、创建个类

# ! encoding=utf-8

class Wiget:

    def __init__(self,size=(40,50)):

        self.size=size

 

    def getSize(self):

        return self.size

 

    def reSize(self,wigth,height):

        if wigth is not None and height is not None:

            self.size=(wigth,height)

            return self.size 

 

            raise ValueError,'illegal size'

 

    def dispass(self):

        pass

 

2、创建个测试类

import unittest

from src.pyunit.widget import Wiget

 

class WidgetTestCase(unittest.TestCase):

 

    def setUp(self):

        print('setUp')

        self.widget=Wiget()

 

    def tearDown(self):

        print('tearDown')

        self.widget.dispass()

        self.widget=None

 

    def testGetSize(self):

        print('testGetSize')

        self.assertEqual(self.widget.getSize(), (40, 50))

 

    def testReSize(self):

        print('testReSize')

        self.assertEqual(self.widget.reSize(50, 100), (50, 100))

 

 

def suite():

    suite = unittest.TestSuite()

    suite.addTest(WidgetTestCase("testReSize"))

    return suite

 

# 测试

if __name__ == "__main__":

#    unittest.main(defaultTest = 'suite')

    unittest.TextTestRunner().run(suite())

你可能感兴趣的:(python)