An example of a PyUnit unit test in Python
- 2020-04-02 14:13:56
- OfStack
This article illustrates PyUnit unit testing in Python, similar to Erlang eunit unit testing, for your reference. Specific methods are as follows:
1. Widget.py file 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. Auto. Py file 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 implementation results are as follows:
[code] jobin @ jobin - desktop: ~ / work/python/py_unit $python auto. Py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
(link: #) 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.000s
FAILED (failures = 1)
(link: #)]
I hope this article has helped you with your Python programming.