Python PyUnit unit test instance, pythonpyunit

Source: Internet
Author: User

Python PyUnit unit test instance, pythonpyunit

This article describes Python PyUnit unit testing, which is similar to erlang eunit unit testing. The specific method is as follows:

1. The widget. py file is as follows:

Copy codeThe 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:

Copy codeThe 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.


How can I test python?

For web pages, python + selenium
For unit testing, python has its own testing framework PyUnit.
For http interface testing, you may need to write some frameworks by yourself. Use httplib, urllib, urllib2, socket, and other modules.
For GUI testing, python has Pywinauto
I don't know what function testing you are talking about?


Python or ruby

How long have you been learning computer? There are no absolute advantages or disadvantages between languages, and they are all tools to achieve the needs. The ease and efficiency of the script language never come before you learn it. Testing and learning scripts are also helpful. You can use a small amount of code to quickly implement test cases.
These two scripting languages are almost difficult (better to use than java). Learn one language and recommend python.

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.