This example describes the Python pyunit unit test, which is similar to the Erlang eunit unit test, and shares it for everyone's reference. Here's how:
The 1.widget.py file is as follows:
Copy the Code code as follows:
#!/usr/bin/python
# Filename:widget.py
Class Widget:
def __init__ (self, size = (40, 40)):
Self.size = Size
def getsize (self):
Return self.size
def resize (self, width, height):
If width < 0 or height < 0:
Raise ValueError, "illegal size"
Self.size = (width, height)
def dispose (self):
Passdefaulttestcase
2. auto.py files are as follows:
Copy the Code code as follows:
#!/usr/bin/python
# Filename:auto.py
Import UnitTest
From widget Import widget
Class Widgettestcase (UnitTest. TestCase):
def setUp (self):
Self.widget = Widget ()
def tearDown (self):
Self.widget = None
def testsize (self):
Self.assertequal (Self.widget.getSize (), (50, 40))
Def Suite ():
Suite = UnitTest. TestSuite ()
Suite.addtest (Widgettestcase ("Testsize"))
Return Suite
if __name__ = = "__main__":
Unittest.main (defaulttest = ' suite ')
3. The results of the implementation are as follows:
[code]jobin@jobin-desktop:~/work/python/py_unit$ python auto.py
.
----------------------------------------------------------------------
Ran 1 Test in 0.000s
Ok
jobin@jobin-desktop:~/work/python/py_unit$ python auto.py
F
======================================================================
Fail:testsize (__main__. Widgettestcase)
----------------------------------------------------------------------
Traceback (most recent):
File "auto.py", line at Testsize
Self.assertequal (Self.widget.getSize (), (50, 40))
Assertionerror: (40, 40)! = (50, 40)
----------------------------------------------------------------------
Ran 1 Test in 0.000s
FAILED (Failures=1)
Jobin@jobin-desktop:~/work/python/py_unit$[/code]
Hopefully this article will help you with Python programming.