[Turn]python_ Common assertion assert

Source: Internet
Author: User

Original address: Http://www.jianshu.com/p/eea0b0e432da

Python automated testing to find elements and operations, if the elements are easy to look at, I believe you can be more proficient in writing use case script, but light operation may not be enough, and sometimes need to judge the expected results.

  • Common
  • The use of several common assertions is described here to help you judge the expected results to some extent.

      • assertEqual
      • assertNotEqual
      • assertTrue
      • assertFalse
      • assertIsNone
      • assertIsNotNone
    • Assertequal and Assertnotequal
      • assertEqual: If two values are equal, pass
      • assertNotEqual: If two values are not equal, the pass
      • How to use:
        assertEqual(first,second,msg)It is first compared with the other second , if the equivalence is passed; the msg information printed on the failure is selected, and the assertion assertnotequal is reversed.
    • Asserttrue and Assertfalse
      • assertTrue: Determines if the bool value is true, the pass
      • assertFalse: If the bool value is false, pass
      • How to use:
        assertTrue(expr,msg)The expression is express entered, if the expression is true, pass, msg Optional; assertFalse If the expression is false, pass
    • Assertisnone and Assertisnotnone
      • Assertisnone: Does not exist, then pass
      • Assertisnotnone: exists, then pass
      • How to use:
        assertIsNone(obj,msg)Check if an element exists
    • Summarize:
    • Common
      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.7Assertisnot (A, b) a is  notB 2.7Assertisnone (x) x isNone 2.7Assertisnotnone (x) x is  notNone 2.7Assertin (A, b) ainchB 2.7Assertnotin (A, b) a not inchB 2.7Assertisinstance (A, B) isinstance (A, b)2.7Assertnotisinstance (A, b) notIsinstance (A, B) 2.7

      Other

      Assertalmostequal (A, b) round (A, 7) = =0 assertnotalmostequal (A, b) round (a-B, 7)! =0 Assertgreater (A, b) a> B 2.7Assertgreaterequal (A, b) a>= B 2.7Assertless (A, b) a< b 2.7Assertlessequal (A, b) a<= B 2.7Assertregexpmatches (S, re) Regex.search (s)2.7Assertnotregexpmatches (S, re) notRegex.search (s) 2.7Assertitemsequal (A, B) sorted (a)= = Sorted (b) andWorks with Unhashable OBJS 2.7Assertdictcontainssubset (A, B) all the key/value pairsinchA existinchB 2.7Assertmultilineequal (A, b) strings2.7Assertsequenceequal (A, b) sequences2.7Assertlistequal (A, b) lists2.7Asserttupleequal (A, b) tuples2.7Assertsetequal (A, b) setsorFrozensets 2.7Assertdictequal (A, b) dicts2.7Assertmultilineequal (A, b) strings2.7Assertsequenceequal (A, b) sequences2.7Assertlistequal (A, b) lists2.7Asserttupleequal (A, b) tuples2.7Assertsetequal (A, b) setsorFrozensets 2.7assertDictEqual (A, b) dicts 2.7
        • Time to use: So when should we use assertions? If there is no specific purpose, the assertion should be used in the following cases:
          • Defensive programming
          • Run-time detection of program logic
          • Contractual checks (e.g. preconditions, post conditions)
          • Constants in the program
          • Check the document
        • Example: Adding assertions in defensive programming
          Adding comments at the beginning of the code is a good start, but people are less likely to read and update these comments, which quickly become obsolete. But for assertions, we can document this code at the same time, and if these assertions are violated, it will directly cause a simple and straightforward failure.
          assert inch (x, Y, z) if  target = =x:    run_x_code ()elif  target = =y:  run_y_code ()Else :   assert target = = Z  run_z_code ()

[Turn]python_ Common assertion assert

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.