This article mainly introduces the Python PyUnit unit test instance, which is a very practical technique. if you need it, you can refer to the following example to describe the Python PyUnit unit test, it is very similar to erlang eunit unit testing and is shared with you for your reference. The specific method is as follows:
1. the widget. py file is as follows:
The code is 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. the auto. py file is as follows:
The code is 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 execution result is as follows:
[Code] jobin @ jobin-desktop :~ /Work/python/py_unit $ python auto. py
.
----------------------------------------------------------------------
Ran 1 test in 0.000 s
OK
Jobin @ jobin-desktop :~ /Work/python/py_unit $ python auto. py
F
========================================================== ====================================
FAIL: testSize (_ main _. WidgetTestCase)
----------------------------------------------------------------------
Traceback (most recent call last ):
File "auto. py", line 15, in testSize
Self. assertEqual (self. widget. getSize (), (50, 40 ))
AssertionError: (40, 40 )! = (50, 40)
----------------------------------------------------------------------
Ran 1 test in 0.000 s
FAILED (failures = 1)
Jobin @ jobin-desktop :~ /Work/python/py_unit $ [/code]
I hope this article will help you with Python programming.