Python+selenium+unittest Test Framework 1-unittest Unit Test framework and assertions

Source: Internet
Author: User
Tags assert

UnitTest Unit Test Framework and assertions

I. Introduction to the UNITTEST Unit testing framework

1. Import UnitTest Module

Import UnitTest

2. Define test class

Class Login (UnitTest. TestCase):

3. Pre-conditions

    def setUp (self):        u' no preconditions can write pass'            

4. Test Case plus assertion

    # The test case must start with Test        print (" execute test Case"'1'# to assert that result is true  

5. Post conditions

    def TearDown (self):        u' no post condition can write pass'            

6. Running the main function

'__main__': Unittest.main ()  

Example:

#Import UnitTest ModuleImportUnitTestClassLogin (unittest. TestCase):DefSetUp (self): U‘‘‘No pre-conditions can write pass‘‘‘Passdef test01 (self):#Test cases must start with testprint ( " execute test Case 01 ") result =  ' 1 '  Self.asserttrue (result) # assert that result is true def  TearDown (self): U No post conditions can be written Passpassif" Span style= "COLOR: #800080" >__name__ = =  ' __main__ ' : Unittest.main ()        

Second, assert

UnitTest Common assertion Methods

1, assertequal (self, first, second,msg=none)

    def test02 (self):# Determine if two parameters are equal to        print (" execute test Case"'1'2' self.assertequal (result1,result2,msg="%s not equal to%s"% (RESULT1,RESULT2))     

2, assertnotequal (self, first, second,msg=none)

    def test03 (self):# Determine if two parameters are unequal        print (" execute test Case"'1'2' self.assertnotequal (result1,result2,msg="%s equals%s"% (RESULT1,RESULT2))     

3, Assertin (self, Member, Container,msg=none)

    def test04 (self):# determine if one parameter contains another parameter        print (" execute test Case"' 1'21 ' self.assertin (result1,result2,msg="%s does not contain%s"% (RESULT2,RESULT1))   

4, Assertnotin (self, member,container, Msg=none)

    def test05 (self):# determine if one parameter does not contain another parameter        print (" execute test Case"'1'2 ' self.assertnotin (result1,result2,msg="%s contains%s"% (RESULT2,RESULT1))   

5. Asserttrue (self, expr, msg=none)

    # Test If a parameter is true        print (" execute test Case"' self.asserttrue ' "%s" is not true 

6. Assertfalse (self, expr, msg=none)

    def test07 (self):# Determines if one parameter does not contain another parameter,        print (" execute test Case"' self.assertfalse "( result,msg="%s is not false"% (result))          

7, Assertisnone (self, obj, msg=none)

    def test08 (self):# determine if one parameter does not contain another parameter        print (" execute test Case") result = None Self.assertisnone (result,msg="%s is not none"% (result))        

8, Assertisnotnone (self, obj,msg=none)

    def test09 (self):# determine if one parameter does not contain another parameter        print (" execute test Case"'123'  Self.assertisnotnone (result,msg="%s is none"% (result))       

Example:

#Import UnitTest ModuleImportUnitTestClassLogin (unittest. TestCase):DefSetUp (self): U‘‘‘No pre-conditions can write pass‘‘‘Print"Start execution")#Passdef test01 (self):#Test cases must start with testPrint"Execute test Case 01") result =‘1‘Self.asserttrue (Result)#Assert that result is not truedef test02 (self):#Determine if two parameters are equalPrint"Execute test Case 02") RESULT1 =‘1‘RESULT2 =‘1‘Self.assertequal (result1,result2,msg="%s is not equal to%s"%(RESULT1,RESULT2))def test03 (self):#Determine if two parameters are not equalPrint"Execute test Case 03") RESULT1 =‘1‘RESULT2 =‘2‘Self.assertnotequal (result1,result2,msg="%s equals%s"%(RESULT1,RESULT2))def test04 (self):#Determine if a parameter contains another parameterPrint"Execute test Case 04") RESULT1 =‘1‘RESULT2 =‘21st‘Self.assertin (result1,result2,msg="%s does not contain%s"%(RESULT2,RESULT1))def test05 (self):#Determine if a parameter does not contain another parameterPrint"Execute test Case 05") RESULT1 =‘1‘RESULT2 =‘2‘Self.assertnotin (result1,result2,msg="%s contains%s"%(RESULT2,RESULT1))def test06 (self):#Test whether a parameter is truePrint"Execute test Case 06") result =‘1‘Self.asserttrue (result,msg="%s is not true"%Resultdef test07 (self):#Determine if a parameter does not contain another parameterPrint"Execute test Case 07") result =‘‘Self.assertfalse (result,msg="%s is not false"%(result))def test08 (self):#Determine if a parameter does not contain another parameterPrint"Execute test Case 08") result =None Self.assertisnone (result,msg="%s is not none"%(result))def test09 (self):#Determine if a parameter does not contain another parameterPrint"Execute test Case 09") result =‘123‘ Self.assertisnotnone (Result,msg= " %s is None "%def TearDown (self): U No post-conditions can be written Passprint ( " end ...  ") # Passif __name__ = =  __main__ ": Unittest.main ()  

Ps:unittest executes the setup, executes the test case test* (the test case is executed in the name order), and finally executes the teardown.

Python+selenium+unittest Test Framework 1-unittest Unit Test framework and assertions

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.