Python PyUnit unit test instance

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

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.