Python unittest unit test methods and use cases

Source: Internet
Author: User
Tags shuffle

Python has a module with a unit test inside it, and pyunit is what we say:UnitTest

First, introduce the basic use of the next unittest:

1.import UnitTest
2. Define an inherited from UnitTest. Test Case Classes for TestCase
3. Define setup and teardown, and do some ancillary 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, the test purpose and the test content should be very 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. Invoke Unittest.main () Start test
7. If the test fails, the corresponding error prompts are printed. If the test passes without displaying anything, you can add the-v argument to display the details.

The following are common methods for UnitTest modules:
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 specific code applications:

First wrote a simple application: Import random
Import UnitTest

classTestsequencefunctions (unittest. TestCase):

defSetUp (self):
SELF.SEQ = Range (10)

defTest_shuffle (self):
# Make sure the shuffled sequence does not lose any elements
Random.shuffle (SELF.SEQ)
Self.seq.sort ()
Self.assertequal (SELF.SEQ, Range (10))

# should raise an exception for a immutable sequence
Self.assertraises (TypeError, Random.shuffle, (1, 2, 3))

defTest_choice (self):
element = Random.choice (SELF.SEQ)
Self.asserttrue (element in Self.seq)

defTest_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 codes returned by the 4 URLs below are 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 some URLs are not open, return 404, the test will be the error

If some URLs are not open, return 404, the test will be the error

ERROR:TEST_OK (__main__. Testurlhttpcode)
----------------------------------------------------------------------
Traceback (most recent call last):
File "jay.py", line, in 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, in Open_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)

There are other unittest methods used to perform more specific checks, such as:

Method Checks this New in
Assertalmostequal (A, b) round (a-b, 7) = = 0
Assertnotalmostequal (A, b) round (A-b, 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 with Unhashable OBJS 2.7
Assertdictcontainssubset (A, B) all of the key/value pairs in a exist 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 use the UnitTest module to do your own unit testing in more ways.

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.