码迷,mamicode.com
首页 > 编程语言 > 详细

python自学1——代码优化2

时间:2017-09-06 10:04:43      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:exce   blog   head   color   return   dict   str   测试   sel   

把接口测试的代码分别封装到了不同的模块中,只是简单的写入了类,类和属性等的使用还在学习中。
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

标签:exce   blog   head   color   return   dict   str   测试   sel   

原文地址:http://www.cnblogs.com/wyfighting/p/7482852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!