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.