Record Python interface automation test (second mesh)

Source: Internet
Author: User

A few simple demos were written in the first goal, and the method of calling get and post requests was encapsulated in a class, this time combining Python's own unittest framework to write an interface test demo using the previously encapsulated method

1.unittest Simple usage

#-*-coding:utf:8-*-ImportUnitTestclassTestMethod (unittest. TestCase):#defines a class that inherits from UnitTest. TestCase    #execute Setup () before each execution of the use case, and you can do some initialization work here    defsetUp (self):Print('setUp')    #Execute teardown after each use case    defTearDown (self):Print('TearDown')    defTest001 (self):#the use case in UnitTest must begin with Test        Print('test001')    deftest002 (self):Print('test002')if __name__=='__main': Unittest.main ()

The results of the operation are as follows:

If you do not want to execute the setup () and teardown () every time you execute the use case, you can use the class method instead, as long as it is executed once:

#-*-coding:utf:8-*-ImportUnitTestclassTestMethod (unittest. TestCase):#defines a class that inherits from UnitTest. TestCase    #execute Setup () before each execution of the use case, and you can do some initialization work here@classmethoddefSetupClass (CLS):Print('setup\n')    #Execute teardown after each use case@classmethoddefTeardownclass (CLS):Print('TearDown')    defTest001 (self):#the use case in UnitTest must begin with Test        Print('test001')    deftest002 (self):Print('test002')if __name__=='__main': Unittest.main ()

The results of the operation are as follows:

2. Interface Test Example

#-*-coding:utf:8-*-ImportUnitTest fromInterface.demoImportRunmain#from the previously encapsulated file, introduce the Runmain classImportHtmltestrunnerImportJSONclassTestMethod (unittest. TestCase):#defines a class that inherits from UnitTest. TestCase    defsetUp (self): Self.run= Runmain ()#instantiate an object in the initialization method so that it does not need to be instantiated in each use case    deftest01 (self): URL='HTTP://192.168.0.53:7001/COMMONSERVICE/API/CONTROL/CONTROLCUST/QUERY.V'Data= {            'Controlseq':'2018118325'} R= Self.run.run_main (URL,'POST', data)#call the Run_main method in the Runmain class        Print(r) Re=json.loads (R) self.assertequal (re['Status'],' $','Test Failed')
#注意我在进行断言前, the Json.loads () function of the JSON library is used to decode the returned result R in the previous step, otherwise the error "json.loadsstring indices must be integers" is called when the r[' status ' is invoked directly.
#原因是, in the previous example, to make the display more intuitive, the response to the server was encoded in JSON format using Json.dumps ()
deftest02 (self):URL ='HTTP://192.168.0.53:7001/COMMONSERVICE/API/CONTROL/CONTROLPROGRESS/QUERY.V'Data= { "Controlseq":"2018118325"} R= Self.run.run_main (URL,'GET', data)Print(r) Re=json.loads (R) self.assertequal (re["Status"],' $','Test Failed') #@unittest. Skip (' test03 ') # Use the Skip () method to indicate a skip case test03 deftest03 (self): URL='HTTP://192.168.0.53:7001/COMMONSERVICE/API/CONTROL/EXPRESSINFO/ADD.V'Data= { 'Controlseq':'2018118361', 'seq':'2939', 'type':'1'} R= Self.run.run_main (URL,'POST', data)Print(R)#Print (Type (r)) # view the type of return object RRe =json.loads (R)#print (type (re))Self.assertequal (re['Status'],' $','Test Failed')if __name__=="__main__": Unittest.main () #表示执行全部用例

3. Use Testsuite () to customize the execution case

#-*-coding:utf:8-*-ImportUnitTest fromInterface.demoImportRunmain#from the previously encapsulated file, introduce the Runmain classImportHtmltestrunnerImportJSONclassTestMethod (unittest. TestCase):#defines a class that inherits from UnitTest. TestCase    defsetUp (self): Self.run= Runmain ()#instantiate an object in the initialization method so that it does not need to be instantiated in each use case    deftest01 (self): URL='HTTP://192.168.0.53:7001/COMMONSERVICE/API/CONTROL/CONTROLCUST/QUERY.V'Data= {            'Controlseq':'2018118325'} R= Self.run.run_main (URL,'POST', data)#call the Run_main method in the Runmain class        Print(r) Re=json.loads (R) self.assertequal (re['Status'],' $','Test Failed')        #globals () [' userid '] = #定义全局变量    deftest02 (self):#print (userid) #使用case1中的全局变量, execution needs to execute all, can not only perform the following, or will errorURL ='HTTP://192.168.0.53:7001/COMMONSERVICE/API/CONTROL/CONTROLPROGRESS/QUERY.V'Data= {            "Controlseq":"2018118325"} R= Self.run.run_main (URL,'GET', data)Print(r) Re=json.loads (R) self.assertequal (re["Status"],' $','Test Failed')    #@unittest. Skip (' test03 ') # Skip use case test03    deftest03 (self): URL='HTTP://192.168.0.53:7001/COMMONSERVICE/API/CONTROL/EXPRESSINFO/ADD.V'Data= {            'Controlseq':'2018118361',            'seq':'2939',            'type':'1'} R= Self.run.run_main (URL,'POST', data)Print(R)#Print (Type (r)) # view the type of return object RRe =json.loads (R)#print (type (re)) #查看json对象解码后的类型Self.assertequal (re['Status'],' $','Test Failed')if __name__=="__main__":Suite = UnitTest. TestSuite ()#Call UnitTest's Testsuite (), which is understood as a container for managing a case (test suite)Suite.addtest (TestMethod ('test01'))#Add a use case to the test suite, "TestMethod" is the class name defined above, "test01" is the dawnSuite.addtest (TestMethod ('test02')) Suite.addtest (TestMethod ('test03')) Runner= UnitTest. Texttestrunner ()#use cases in the execution suiteRunner.run (Suite)

When you execute the file again, only the cases added to the test suite will be executed.

But if you are using the Pycharm to run the script, there are a few points to note, Pycharm here is a pit, troubled me for a long time, in the third goal of the small summary of the said

Record Python interface automation test (second mesh)

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.