HTTP Interface Automation Test framework implementation

Source: Internet
Author: User
Tags python script

I. Description of TEST requirements

A series of HTTP interface functional tests on the service backend.

Input: Constructs different parameter input values according to the interface description

Output: XML file

Eg:http://xxx.com/xxx_product/test/content_book_list.jsp?listid=1

Second, the realization method

1. Use Python script to drive test

2, the Use of Excel table management test data, including use case management, test data entry, test results display and so on, this needs to encapsulate an Excel class.

3, call the HTTP interface using the Python encapsulated API can be

4, the test needs the HTTP assembly character to turn processing can

5, set 2 checkpoints, the return value field in the XML file (obtained by parsing XML); XML file correctness (file comparison)

6, the first implementation of the test in a semi-automatic way, that is, manually check the output of the XML file is correct, once the correct storage of the XML file, for subsequent regression testing the expected results, if the error is found manually corrected to the expected file. (Note that not every test is a manual check of the file, only the first test is checked)

Third, Excel table style

Implementation of code (code is the king, there are comments can easily be seen clearly)

1. Test framework Code

[Python]  View Plain  copy #****************************************************************   #  testframe.py   # author     : vince   # Version     : 1.1.2   # date       :  2011-3-14   # description:  Automation test Platform    #********************************************       import os,sys, urllib, httplib, profile,  datetime, time   from xml2dict import xml2dict   import  win32com.client   from win32com.client import dispatch   import  xml.etree.elementtree as et   #import  MySQLdb      #Excel表格中测试结果底色    ok_color=0xffffff   ng_color=0xff   #NT_COLOR =0xffff   nt_color= 0xc0c0c0   &Nbsp;  #Excel表格中测试结果汇总显示位置    testtime=[1, 14]   testresult=[2, 14]      #Excel模版设置    #self. titleindex=3        # Test Case header row index    #self in Excel. casebegin =4        # Test Case start line index    #self in Excel. argbegin   =3       # Parameter Start column index    #self in Excel. argcount  =8        # Number of parameters supported in Excel    class create_excel:       def __init__ (self,  sfile, dtitleindex=3, dcasebegin=4, dargbegin=3, dargcount=8):            self.xlapp = win32com.client.dispatch (' ET. Application ')     #MS:excel  wps:et            try:                self.book = self.xlapp.workbooks.open (SFile)            except:                print_error_info ()                 print  "Failed to open file"                 exit ()            self.file=sFile            self.titleindex=dtitleindex            self.casebegin=dcasebegin            self.argbegin=dargbegin           self.argcount= dargcount           self.allresult=[]                       self.retcol=self.argbegin+ self.argcount           self.xmlCol=self.retCol+1           self.resultCol=self.xmlCol+1           def close (self):           # Self.book.Close (savechanges=0)            self.book.save ()            self.book.close ()              #self. xlApp.Quit ()             del self.xlapp                   def read_data (self, isheet, irow, icol):            try:               sht =  Self.book.Worksheets (isheet)                 svalue=str (sht. Cells (Irow, icol). Value)            except:                self.close ()                 print (' failed to read data ')                 exit ()             #去除 '. 0 '             if svalue[-2:]== '. 0 ':                sValue = sValue[0:-2]            return sValue        &NBSp; def write_data (Self, isheet, irow, icol, sdata, color=ok_color):            try:                sht = self.book.worksheets (isheet)                 sht. Cells (Irow, icol). Value = sdata.decode ("Utf-8")                 sht. Cells (Irow, icol). interior.color=color                Self.book.Save ()            except:                self.close ()                 print (' Write data failed ')                  exit ()               # Get the number of use cases            def get_ncase (self, isheet):            try:                return self.get_nrows (isheet)-self.casebegin+1            except:                self.close ()                 print (' Failed to get Case number ')                 exit ()               def get_nrows (self,  isheet):           try:         &nBsp;      sht = self.book.worksheets (iSheet)                 return sht. usedrange.rows.count           except:                self.close ()                 print (' Get nrows failed ')                 exit ()           def  get_ncols (self, isheet):           try:                sht =  Self.book.Worksheets (isheet)                 return sht. usedrange.columns.count           except:                self.close ()                 Print (' Get Ncols failed ')                exit ()               def del_testrecord (self,  Suiteid):           try:                 #为提升性能特别从For循环提取出来                 nrows=self.get_nrows (Suiteid) +1                ncols=self.get_ncols (Suiteid) +1                begincol=self.argbegin+self.argcount                                 #提升性能                 sht = self.book.worksheets (Suiteid)                    for row in range (self.casebegin, nrows):                     For col in range (begincol, ncols):                        str=self.read_data (Suiteid,  row, col)                          #清除实际结果 []                         startpos = str.find (' [')                         if  startpos>0:                            str = str[0:startpos].strip ()                             self.write_data (Suiteid, row, col, str, ok _color)                         else:                             #提升性能                              sht. Cells (Row, col). interior.color = ok_color                     #清除TestResul列中的测试结果, set to nt                    self.write_data (suiteid, row,   self.argbegin+self.argcount+1,  '   ',  ok_color                     self.write_data (suiteid, row,  self.resultcol,  ' NT ',  nt_color)            except:                self.close ()                print (' Failed to erase data ')                exit ()       #执行调用     Def httpinvoke (Ipport, url):       conn = httplib. Httpconnection (Ipport)        conn.request ("GET",  url)         rsps = conn.getresponse ()        data =  Rsps.read ()        conn.close ()        return  data      #获取用例基本信息 [interface,argcount,[argnamelist]]   def get_caseinfo (Data,  suiteid):       caseinfolist=[]        Sinterface=data.read_data (suiteid, 1, 2)         argcount=int ( Data.read_data (suiteid, 2, 2)                  #获取参数名存入ArgNameList        ArgNameList=[]       for i  In range (0, argcount):           argnamelist.append (Data.read_data (suiteid, data.titleindex, data.argbegin+i))                  caseinfolist.append (sinterface)         caseinfolist.append (Argcount)        caseinfolist.append (ArgNameList)         return caseinfolist      #获取输入    def get_ Input (data, suiteid, caseid, caseinfolist):       sarge= '          #参数组合        for j in range (0,  CASEINFOLIST[1]):           if data.read_data (SuiteID,  data. Casebegin+caseid, data.argbegin+j)  !=  "None":                sarge=sarge+caseinfolist[2][j]+ ' = ' +data.read_data (SuiteID,  DATA.CASEBEGIN+CASEID, DATA.ARGBEGIN+J) + ' & '                 #去掉结尾的 & characters        if sarge[-1:]== ' & ':            sArge = sArge[0:-1]           sInput=caseinfolist[0]+sArge     #组合全部参数         return sInput       #结果判断     Def assert_ Result (Sreal, sexpect):  

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.