写了个小 demo 自动生成异常参数用例,读取 exel 的用例,随手写的,有注释,比较容易懂。代码比较简单,后面也懒得改了,目前在想接口的业务逻辑这一块能不能封装起来,希望有大神能够指点一下,算是抛砖引玉吧。
写这篇文章的目的就是:请问大家一般怎么去封装接口业务逻辑,感觉没什么头绪,等后面有时间琢磨下。

1、生成各种异常参数的用例,伪代码,懒得写成类了(重度懒癌患者),自动生成的代码我只写了参数那一列,其他列我也懒得补了,因为给别的项目组员的妹子用的,让她自己完善其他列好了。
异常参数取决于 list_str_test 这个列表,需要什么类型的异常就填什么

#coding=utf-8
import  xlrd
from xlutils import copy
import  time
param_list=[]
case_list=[]
list_str_test=['null','','1234567899999','测试中文','testenglish','#@!@#$%']
try:
    data=xlrd.open_workbook("appointmentcheck.xlsx")#打开exel
except Exception,e:
    print e
else:
    sheet = data.sheet_by_index(0)#取第一个sheet页
    rows= sheet.nrows#取这个sheet页的所有行数
    cols=sheet.ncols


    #print urls
    for i in range(1,rows):
        #case_list.append(sheet.row_values(i))
        #print 'caselist',self.case_list
        param_list.append(sheet.cell(i,7).value) #遍历读取每行的参数
    #循环将每行的参数生成字典
    #print param_list
    #每行的参数生成一个字典,有多少行参数就生成多少个字典
    list_param_new=[]
    print '111111',param_list
    for param in param_list:
        dict=eval(param)
        #print  dict
        for x in dict:
            dict_copy=dict.copy()
            for i in list_str_test:
                dict_copy[x]=i
                dict_copy_2=dict_copy.copy()
                list_param_new.append(dict_copy_2)
                #print  i,dict_copy
                #print 'i=====',i,list_param_new
                #print 'x=',x,'dict_copy======',dict_copy
#print list_param_new
new_book=copy.copy(data)
sheet_new= new_book.get_sheet(0)

j=1
for parameter in list_param_new:
    sheet_new.write(j,7,u'%s'%parameter)
    j+=1


new_book.save('%s_test_auto.xls'%time.strftime('%Y%m%d%H%M%S'))

2、读取用例发送请求

# -*- coding:utf-8 -*-
from xlutils import copy
import  xlrd
import requests,time
import logging
import  time
import json
logging.basicConfig(level=logging.INFO)
#debug,info,warning,error
class test():
    def __init__(self,filepath,case_list=[],):
        self.filepath=filepath
        self.case_list=case_list

    def read(self):
        try:
            data =xlrd.open_workbook(self.filepath)#打开exel
        except Exception,e:
            print '路径不存在或其他错误',e
            print self.filepath
            return  e
        else:
            sheet = data.sheet_by_index(2)#取第一个sheet页
            rows= sheet.nrows#取这个sheet页的所有行数
            for i in range(1,rows):
                self.case_list.append(sheet.row_values(i))
            #print 'caselist',self.case_list
            return self.case_list

    def interfaceTest(self):
        #res_flags,request_urls,responses
        headers = {'content-type': 'application/json'}
        res_flags =[]
        request_urls =[]
        responses = []

        #print 'interfaceTest的case_list=====',self.case_list
        for case in self.case_list:
            try:
                product = case[1] #项目描述
                case_id = case[2]  #用例id,提bug的时候用
                interface_name = case[3] #接口名称,也是提bug的时候用
                case_detail = case[4] #用例描述
                method = case[5] #请求方式
                url = case[6] #请求url
                param = case[7] #入参
                res_check = case[9] #预期结果
                tester = case[11] #测试人员
               # print product,case_id,interface_name,case_detail,method,url,param,res_check,tester
                #print case,'case..........'
                print method,'5555'
                #print case,'6666'
            except Exception,e:
                return '测试用例格式不正确!%s'%e
            if param == '':
                new_url = url#请求报文
                request_urls.append(new_url)
            else:
                new_url = url+'?'+param#请求报文
                request_urls.append(new_url)

            if method.upper()=='GET':
                #get请求
                #print new_url
                results = requests.get(new_url).text
                #print results
                responses.append(results)
                #res =self.readRes(results,res_check)
            else:
                #post请求
                # print new_url,'2222'
                # print type(new_url),'3333'
                # print param
                r = requests.post(new_url,headers=headers).text
                #print 'post请求======',results
                responses.append(r)
                #print 'case===',case,'newurl===',new_url,'results=====',results
            if res_check in r:
                res_flags.append('pass')
            else:
                res_flags.append('fail')
        book = xlrd.open_workbook(self.filepath)
        new_book=copy.copy(book)
        sheet = new_book.get_sheet(0)
        i = 1
        for request_url,response,flag in zip(request_urls,responses,res_flags):
            sheet.write(i,9,u'%s'%request_url)
            sheet.write(i,10,u'%s'%response)
            sheet.write(i,11,u'%s'%flag)
            i+=1
        new_book.save('%s_testresult.xls'%time.strftime('%Y%m%d%H%M%S'))


if __name__ == '__main__':
    a=test('case.xlsx') #填入用例路径
    a.read()
    a.interfaceTest()


↙↙↙阅读原文可查看相关链接,并与作者交流