Python's pyunit Unit test example

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.