python自学1——代码优化2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python自学1——代码优化2相关的知识,希望对你有一定的参考价值。

把接口测试的代码分别封装到了不同的模块中,只是简单的写入了类,类和属性等的使用还在学习中。
impTestcase.py

 1 #coding:utf-8
 2 import xlrd
 3 """import testcase from excel"""
 4 class impTestcase(object):
 5     
 6     def __init__(self):
 7         pass
 8         
 9     def importcase(self):
10         nrows = 0
11         rdict = {} #存储请求参数
12         returnres = {}
13         fname = testcase.xlsx
14         sheet_name=case1
15         bk = xlrd.open_workbook(fname) 
16         try:
17             sheet = bk.sheet_by_name(sheet_name)
18         except:
19             print("no sheet in %s named sheet1" %fname)
20      
21         nrows = sheet.nrows
22         ncols = sheet.ncols 
23         for i in range(0,ncols): 
24             data = sheet.col_values(i) 
25             rdict[data[0]]=data[1:]
26         
27         returnres[nrows]=nrows
28         returnres[rdict]=rdict
29         return returnres

 

sendreq.py

#coding:utf-8
import requests
from impTestcase import impTestcase
"""send requests
response = requests.post(url,json = data,headers = headers,verify=False) """
class sendreq(object):
    
    def __init__(self,nrows,rdict):
        self.nrows = nrows
        self.rdict = rdict

    def sendrequest(self):
        relist = []
        for i in range(0,self.nrows-1):
            url = self.rdict[url][i]    
     
            if self.rdict[headers][i]!=‘‘:
                headers = eval(self.rdict[headers][i]) #str to dict
            else:
                headers = {}
     
            if self.rdict[json][i]!=‘‘:
                json = self.rdict[json][i] #str to dict
            else:
                json = {}

            if self.rdict[params][i]!=‘‘:
                params = eval(self.rdict[params][i]) #str to dict
            else:
                params = {}
            
            try:
                if self.rdict[method]==get:
                    r = requests.get(url,headers = headers,params = params,verify=False)
                else:
                    r = requests.post(url,json = json,headers = headers,verify=False)
                r.raise_for_status()
                r.encoding=r.apparent_encoding

                relist.append(Casename: +self.rdict[casename][i])
                relist.append(Status: +str(r.status_code))
                relist.append(Date: +r.headers[Date])                 
            except Exception as e:
                print(e)    
                print(fail)
                relist.append(str(e))
        return relist

 

outputResult.py

#coding:utf-8
from sendreq import sendreq
"""xxx"""
class outputResult(object):

    def __init__(self,relist):
        self.relist = relist

    def output(self):
        fout=open(output.html,w,encoding=UTF-8, newline=‘‘) 
        fout.write(<html>) 
        fout.write(<head>) 
        fout.write(<meta charset = "UTF-8"/>) 
        fout.write(</head>) 
        fout.write(<body>)
        fout.write(<h1>report<h1>)
        fout.write(<table>)
        fout.write(<tr>)
        #fout.write(‘<td>%s</td>‘ % relist)
        for i in range(0,len(self.relist)):
            fout.write(<td>%s</td> % self.relist[i])
        fout.write(</tr>)
        fout.write(</table>)
        fout.write(</body>)
        fout.write(</html>) 
        fout.close()

 

main.py

#coding:utf-8
from impTestcase import impTestcase
from sendreq import sendreq
from outputResult import outputResult

def mainRun():

    tc = impTestcase()
    re = tc.importcase()
    send = sendreq(re[nrows],re[rdict]).sendrequest()
    print(send)
    out = outputResult(send)
    out.output()

mainRun()

 

下一步的计划是:
1. 学习模块、类、实例等OOP的知识;

2. 运用所学知识进一步优化代码;

3. 使用pycharm编写和管理代码。



以上是关于python自学1——代码优化2的主要内容,如果未能解决你的问题,请参考以下文章

python自学1——接口测试

暑假自学JAVA Web心得

如何优化C ++代码的以下片段 - 卷中的零交叉

python自学日志--基础篇

优化 C# 代码片段、ObservableCollection 和 AddRange

自学Python-Day1.1