"Python Skill series" Unittest:python comes with Test module "reprint"

Source: Internet
Author: User
Tags shuffle

This article is reproduced from http://www.cnpythoner.com/post/303.html****

1 python unittest unit test methods and use cases2 3 inside Python comes with a unit test module, Pyunit is what we say: UnitTest4 5 first introduce the basic use of the following unittest:6 71.ImportUnitTest82. Define an inheritance from UnitTest. Test case classes for TestCase93define setup and teardown to do some ancillary work before and after each test case. Ten4. Define the test case, with the name beginning with test.  One5a 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.  A6. Call Unittest.main () to start the test -7. If the test does not pass, the appropriate error message will be output. If the test is all passed then nothing is displayed, then you can add-The v parameter displays detailed information.  -  the the following are common methods for UnitTest modules: -Assertequal (A, b) A = =b -Assertnotequal (A, b) a! =b -Asserttrue (x) bool (x) isTrue +Assertfalse (x) bool (x) isFalse -Assertis (A, B) a isB 2.7 +Assertisnot (A, B) a is  notB 2.7 AAssertisnone (x) x isNone 2.7 atAssertisnotnone (x) x is  notNone 2.7 -Assertin (A, B) ainchB 2.7 -Assertnotin (A, B) a not inchB 2.7 -Assertisinstance (A, B) Isinstance (A, B) 2.7 -Assertnotisinstance (A, B) notIsinstance (A, B) 2.7 -  in Here's a look at the specific code application: -  to first a simple application was written: + ImportRandom - ImportUnitTest the  * classtestsequencefunctions (unittest. TestCase): $ Panax Notoginseng    defsetUp (self): -SELF.SEQ = Range (10) the  +    deftest_shuffle (self): A        #Make sure the shuffled sequence does no lose any elements the 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,)) -  the    defTest_choice (self): -element =Random.choice (SELF.SEQ)WuyiSelf.asserttrue (elementinchself.seq) the  -    defTest_error (self): Wuelement =Random.choice (SELF.SEQ) -Self.asserttrue (element not inchself.seq) About  $  - if __name__=='__main__': - Unittest.main () -  A here is a simple application that tests whether the status code returned by the following 4 URLs is 200.  + ImportUnitTest the ImportUrllib -  $ classTesturlhttpcode (unittest. TestCase): the    defsetUp (self): theUrlinfo = ['http://www.baidu.com','http://www.163.com','http://www.sohu.com','http://www.cnpythoner.com'] theSelf.checkurl =Urlinfo the  -    defTest_ok (self): in         forMinchSelf.checkurl: theHttpcode =Urllib.urlopen (M). GetCode () theSelf.assertequal (httpcode,200) About  the if __name__=='__main__': the Unittest.main () the  + If the URL is not open, return 404, the test will be error -  the If the URL is not open, return 404, the test will be errorBayi  theERROR:TEST_OK (__main__. Testurlhttpcode) the---------------------------------------------------------------------- - Traceback (most recent): -File"jay.py", Line 12,inchTEST_OK theHttpcode =Urllib.urlopen (M). GetCode () theFile"/usr/lib/python2.7/urllib.py", line 86,inchUrlopen the     returnopener.open (URL) theFile"/usr/lib/python2.7/urllib.py", line 207,inchOpen -     returngetattr (self, name) (URL) theFile"/usr/lib/python2.7/urllib.py", Line 462,inchOpen_file the     returnself.open_local_file (URL) theFile"/usr/lib/python2.7/urllib.py", Line 476,inchOpen_local_file94     RaiseIOError (E.errno, E.strerror, E.filename) theIOError: [Errno 2] No such fileorDirectory'fewfe.com' the  the----------------------------------------------------------------------98Ran 1 Testinch1. 425s About  -FAILED (Errors=1)101  102 103 There are also other unittest methods for performing more specific checks, such as:104  theMethod Checks that Newinch106Assertalmostequal (A, b) round (A, 7) = =0107Assertnotalmostequal (A, b) round (A, 7)! =0108Assertgreater (A, b) a > B 2.7109Assertgreaterequal (A, b) a >= b 2.7 theAssertless (A, B) a < b 2.7111Assertlessequal (A, b) a <= b 2.7 theAssertregexpmatches (S, re) Regex.search (s) 2.7113Assertnotregexpmatches (S, re) notRegex.search (s) 2.7 theAssertitemsequal (A, B) sorted (a) = = sorted (b) andWorks with Unhashable OBJS 2.7 theAssertdictcontainssubset (A, B) all the key/value pairsinchA existinchB 2.7 theAssertmultilineequal (A, b) strings 2.7117Assertsequenceequal (A, b) sequences 2.7118Assertlistequal (A, b) lists 2.7119Asserttupleequal (A, b) tuples 2.7 -Assertsetequal (A, B) setsorFrozensets 2.7121Assertdictequal (A, b) dicts 2.7122Assertmultilineequal (A, b) strings 2.7123Assertsequenceequal (A, b) sequences 2.7124Assertlistequal (A, b) lists 2.7 theAsserttupleequal (A, b) tuples 2.7126Assertsetequal (A, B) setsorFrozensets 2.7127Assertdictequal (A, b) dicts 2.7 - 129You can do your own unit tests in more ways with the UnitTest module.

"Python Skill series" Unittest:python comes with Test module "reprint"

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.