Inside Python comes with a unit test module, Pyunit is what we say: unittest
1, introduce the basic use of the following UnitTest method:
1) Import UnitTest
2) define an inheritance from UnitTest. Test Case Classes for TestCase
3) define setup and teardown, and do some auxiliary work before and after each test case.
4) Define the test case, with the name beginning with test.
5) A test case should only test one aspect, and the test purpose and test content should be clear. The main is to call Assertequal, assertraises and other assertion methods to determine whether the program execution results and expected values are consistent.
6) Call Unittest.main () to start the test
7) If the test does not pass, the appropriate error message will be output. If all tests pass without displaying anything, you can add the-v parameter to display the details.
2, the following is the common method of UnitTest module:
Assertequal (A, b) A = = B
Assertnotequal (A, b) a! = B
Asserttrue (x) bool (x) is True
Assertfalse (x) bool (x) is False
Assertis (A, b) A is B 2.7
Assertisnot (A, B) A is not B 2.7
Assertisnone (x) x is None 2.7
Assertisnotnone (x) x is not None 2.7
Assertin (A, b) a in B 2.7
Assertnotin (A, B) a not in B 2.7
Assertisinstance (A, B) Isinstance (A, B) 2.7
Assertnotisinstance (A, B) not isinstance (A, B) 2.7
Here's a look at the specific code application:
First a simple application was written:
Import Random
Import UnitTest
Class Testsequencefunctions (UnitTest. TestCase):
def setUp (self):
SELF.SEQ = Range (10)
def test_shuffle (self):
# Make sure the shuffled sequence does no lose any elements
Random.shuffle (SELF.SEQ)
Self.seq.sort ()
Self.assertequal (SELF.SEQ, Range (10))
# should raise an exception for an immutable sequence
Self.assertraises (TypeError, Random.shuffle, ())
def test_choice (self):
element = Random.choice (SELF.SEQ)
Self.asserttrue (Element Inself.seq)
def test_error (self):
element = Random.choice (SELF.SEQ)
Self.asserttrue (element not in Self.seq)
if __name__ = = ' __main__ ':
Unittest.main ()
Here is a simple application that tests whether the status code returned by the following 4 URLs is 200.
Import UnitTest
Import Urllib
class Testurlhttpcode (unittest. TestCase):
def setUp (self):
Urlinfo = [' http://www.baidu.com ', ' http://www.163.com ', ' http://www.sohu.com ', ' http://www.cnpythoner.com ']
Self.checkurl = Urlinfo
def Test_ok (self):
for M in Self.checkurl:
Httpcode = Urllib.urlopen (M). GetCode ()
Self.assertequal (httpcode,200)
if __name__ = = ' __main__ ':
Unittest.main ()
If the URL is not open, return 404, the test will be error
If the URL is not open, return 404, the test will be error
ERROR:TEST_OK (__main__. Testurlhttpcode)
----------------------------------------------------------------------
Traceback (most recent):
File "jay.py", line TEST_OK
Httpcode = Urllib.urlopen (M). GetCode ()
File "/usr/lib/python2.7/urllib.py", line +, in Urlopen
return Opener.open (URL)
File "/usr/lib/python2.7/urllib.py", line 207, in open
Return GetAttr (self, name) (URL)
File "/usr/lib/python2.7/urllib.py", line 462, in Open_file
return Self.open_local_file (URL)
File "/usr/lib/python2.7/urllib.py", line 476, Inopen_local_file
Raise IOError (E.errno, E.strerror, E.filename)
IOError: [Errno 2] No such file or directory: ' fewfe.com '
----------------------------------------------------------------------
Ran 1 Test in 1.425s
FAILED (Errors=1)
3There are also other unittest methods for performing more specific checks, such as:
Method Checks this New in
Assertalmostequal (A, b) round (A, 7) = = 0
Assertnotalmostequal (A, b) round (A, 7)! = 0
Assertgreater (A, b) a > B 2.7
Assertgreaterequal (A, b) a >= b 2.7
Assertless (A, B) a < b 2.7
Assertlessequal (A, b) a <= b 2.7
Assertregexpmatches (S, re) Regex.search (s) 2.7
Assertnotregexpmatches (S, re) not Regex.search (s) 2.7
Assertitemsequal (A, B) sorted (a) = = sorted (b) and works withunhashable OBJS 2.7
Assertdictcontainssubset (A, B) all the key/value pairs in Aexist in B 2.7
Assertmultilineequal (A, b) strings 2.7
Assertsequenceequal (A, b) sequences 2.7
Assertlistequal (A, b) lists 2.7
Asserttupleequal (A, b) tuples 2.7
Assertsetequal (A, B) sets or Frozensets 2.7
Assertdictequal (A, b) dicts 2.7
Assertmultilineequal (A, b) strings 2.7
Assertsequenceequal (A, b) sequences 2.7
Assertlistequal (A, b) lists 2.7
Asserttupleequal (A, b) tuples 2.7
Assertsetequal (A, B) sets or Frozensets 2.7
Assertdictequal (A, b) dicts 2.7
You can do your own unit tests in more ways with the UnitTest module.
Python unittest Basic Introduction