Python Assert assertion function

Source: Internet
Author: User

python assert断言是声明布尔值必须为真的判定,如果发生异常就说明表达式为假。
可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常。

Self.assertequal (a,b,msg=msg) #判断a与. B is consistent, msg similar to remark, can be empty

Self.assertnotequal (a,b,msg=msg) #判断a与b是否不一致

Self.asserttrue (A,msg=none) #判断a是否为True

Self.assertfalse (B,msg=none) #判断b是否为false

Self.assertalmostequal (A,b,places=none,msg=none,delta=none) #该判断过程有点复杂, the judging process is as follows

Note: Places and Delta cannot exist at the same time, otherwise the exception

#若a ==b, the direct input is correct, do not judge the following procedure

#若delta有数, places is empty, judging whether the absolute value of the difference between A and B is <=delta, satisfies is correct, otherwise the error

#若delta为空, places have number, judge the absolute value of the difference between B and a, take the decimal places bit, equal to 0 is correct, otherwise error

#若delta为空, places is empty, default assignment places=7 judged

Example Assertalmostequal (2,2) is correct,

Assertalmostequal (5,2,delta=4) correct

Assertalmostequal (5,2,delta=2) error

Assertalmostequal (2,2.005,places=1) correct

Assertalmostequal (2,2.05,places=3) error

Self.assertnotalmostequal (A,b,places=none,msg=none,delta=none) Ibid, but judged the opposite

Note that delta and places cannot exist at the same time or throw an exception

Example Assertnotalmostequal (2,2) error

Assertnotalmostequal (a,b,delta=c) #a不等于b while a-b>c is correct, otherwise error

Assertnotalmostequal (a,b,places=2)

#a不等于b at the same time |b-a|*0,01 not equal to 0 is correct, otherwise error

Self.assertsequenceequal #有空在研究, the source is very long

Self.assertlistequal #有空研究

Self.asserttupleequal #有空在研究

Self.assertsetequal #有空在研究

Self.assertin (A, a) determines whether a in B is valid, true, otherwise false

Example: Assertin ("2" in "23") succeeded

Assertin ("1" in "23") failed

Self.assertnotin (A, a) determines whether a in B is true or False if it is not established

Example: Assertin ("2" in "23") failed

Assertin ("1" in "23") succeeded

Self.assertis (A, A, B) determines whether the object of a is the same as, and true if it is set, otherwise false

Note, determine if the same object ID (a) is the same object if the ID is the same

Example a,b=1,1.0

Assertls (A, b) failed

a,b=1,1

Assertls (A, B) succeeded

Self.assertisnot (A, A, B) determines whether the object of a is the same as, and does not set true, otherwise false

Self.assertdictequal (A, B) #判断字典a和字典b是否相等, A, B is a dictionary

Self.assertdictcontainssubset

Self.assertitemsequal (A, a) #比较两字符串是否一致, with sorted (a) ==sorted (b)

Note: The sorted is sorted, the method is internal, A/b is listed, the respective list is generated, the sorted is sorted in the comparison

Self.assertmultilineequal (A, b) #比较a文本与b文本是否一致, even if you have more than one line, will distinguish

Self.assertless (A, b) #判断a <b is established or fails

Self.assertlessequal #判断a <=b is established, or fails

Self.assertgreater #判断a >b is established, or fails

Self.assertgreaterequal #判断a >=b is established, or fails

Self.assertisnone (obj= "") #判断obj =none was established, otherwise failed

Self.assertisnotnone #判断obj =none failed to set up, otherwise through

Self.assertisinstance (A, B) #判断a的数据类型是否为b, Isinstance (A, b) was established, or failed

Self.assertnotisinstance #判断同上相反

Self.assertraisesregexp #正则判断匹配, not looking carefully, the process is complicated

Self.assertregexpmatches (A, b) #正则匹配 match Re.search (B,a) has succeeded or failed

Note: A is a matched regular expression and must be of character type, B is the content to match

Self.assertnotregexpmatches #同上, judging the opposite

Python Assert assertion function

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.