Beispiel #1
0
class RePlay(object):
    def __init__(self, replayid):
        self.replayid = replayid
        self.excel = Excel()
        self.req = API()

    def getReplayMethod(self):

        allmethod = []
        allid = []

        id = self.replayid  #当前依赖的名称 例如test01
        allid.append(id)
        while True:

            num = self.excel.get_row_num(id)  #根据当前依赖id找到编号
            id = self.excel.getValue(num,
                                     getRelyID())  # 找到所在接口 查看所依赖的名称  用于下一次循环使用
            if id == "":  #添加最后一个依赖的方法 这里是循环到了 最后一个依赖id  这是添加最后一个依赖id的方法 如果这个接口 依赖的用例编号为空 则在列表加入当前的方法名
                allmethod.append(self.excel.getValue(num, getMethod()))

                break  #结束循环
            else:  #如果当前不为空  则加入当前的 接口 的名称 继续循环查找是否还有依赖的接口
                allid.append(id)
                allmethod.append(self.excel.getValue(num, getMethod()))

        allmethod.reverse()  #翻转列表内容  按顺序发送接口请求
        #print("所有的依赖方法",allmethod)
        #print("所有的依赖id",allid)
        return [allmethod, allid]

    def runTest(self):
        allMethod, allid = self.getReplayMethod()[0], self.getReplayMethod()[
            1]  #取得当前接口所有依赖的方法
        for j in range(1, self.excel.allCase()):  #查询当前exce的用例数量
            for i in allMethod:
                if self.excel.getValue(j, getMethod()) == i:

                    testid = self.excel.getValue(j, getId())
                    url = self.excel.getValue(j, getUrl())  #获取url
                    method = self.excel.getValue(j, getMethod())  #获取方法
                    args = self.excel.getValue(j, getRequestDate())  #获取账号
                    keyvalue = self.excel.getValue(
                        j, getRelyData2())  #提取当前接口的响应数据并保存
                    forApidata = self.excel.getValue(j,
                                                     getRelyFied())  #放入本次接口的数据
                    value = self.excel.getValue(
                        j, getInputKey())  # 提取本次接口需要的数据用以发送给后台
                    if testid in allid:  #进一步判断 当前方法是否属于这个用例id
                        result = getattr(self.req, i)(url,
                                                      method=method,
                                                      thisApi=forApidata,
                                                      args=args,
                                                      extract=keyvalue,
                                                      lastApi=value)
Beispiel #2
0
class TestCase(object):

    def __init__(self):
        self.sql = BaseQuery() #sql查询
        self.excel = Excel() #excel操作
        self.req = API() #接口基本信息
        self.post = Distribution() #发送请求的类


    def startCase(self): #执行测试用例
        self.caseNum = self.excel.allCase()
        self.Pass = 0
        self.no = 0
        for i in range(1,self.caseNum):
            #self.req.method = self.excel.getValue(i, getMethod())  # 设置本次请求的方法 *********
            #self.req.url = self.excel.getValue(i, getUrl())  #设置本次请求url
            self.method = self.excel.getValue(i,getMethod()) #请求方法
            self.responseKey = self.excel.getValue(i, getresponseKey())  # 获取存储 接口响应的关键字
            self.responseSave = self.excel.getValue(i, getresponseSave())  # 存储接口的方式
            self.key = self.excel.getValue(i, getInputKey())  # 获取输入框输入值
            self.value = self.excel.getValue(i, getInputValue())  # 获取文本输入键
            self.url = self.excel.getValue(i, getUrl())  # 获取每个用例的url
            self.testid = self.excel.getValue(i, getId())  # 获取到case id
            self.replayid = self.excel.getValue(i, getRelyID())  # 获取到依赖id
            self.getvaluemethod = self.excel.getValue(i, getValueMethod())  # 依赖取值方法
            self.keyValue = self.excel.getValue(i, getRelyData2())  # 依赖上一个接口的关键字
            self.forApidata = self.excel.getValue(i, getRelyFied())  # 放入本次接口的数据
            self.reqBody = self.excel.getValue(i, getRequestDate())  # 请求体放置的内容
            self.save = self.excel.getValue(i, getParameter())  # 保存文本
            self.expect =self.excel.getValue(i,getExpect())
            isrun = self.excel.getValue(i, getExecute())  # 是否执行


            if isrun == "":
                print("发现第{0}测试用例是否执行栏出现空格现象".format(self.testid))
                break
            else:
                if isrun == "yes" or isrun == "Yes" or isrun == "YES":
                   #testid,responseKey,responseSave,url,method,replayid,getvaluemethod,keyValue,forApidata,reqBody,save,key,value
                    self.httpContent()

                elif isrun == "no" or isrun == "No" or isrun == "NO":
                    pass
                    #print("{0}跳过测试用例的执行".format(self.testid))
                else:
                    print("{0}出现未知命令".format(self.testid))
        print("通过%d个接口测试,未通过%d个接口测试"%(self.Pass,self.no))
    def httpContent(self):
        #进行内容逻辑处理
        function = hasattr(self.req,self.method)
        """
        self.req 请求体内容
        self.method 接口地址
        """

        if function:
            if self.replayid =="":
                result = getattr(self.req, self.method)(self.url, method=self.method, thisApi=self.forApidata,args=self.reqBody, extract=self.keyValue, lastApi=self.key)
                expect = eval(self.expect)
                try:
                    if result["StateCode"] == expect[0] and result["StateMsg"] == expect[1]:
                        if len(expect) == 3:

                            if result["ResultType"] == expect[2]:
                                print("接口:%s 测试通过 编号:%s" %(self.method,self.testid))
                                self.Pass += 1
                            elif result["ResultType"] != expect[2]:

                                print("接口:%s 测试不通过 编号:%s 响应状态:%s" % (self.method, self.testid, result["ResultType"]))
                                self.no += 1
                        else:
                            print("接口:%s 测试通过 编号:%s" %(self.method,self.testid))
                            self.Pass += 1
                    else:
                        print("接口:%s 测试未通过 编号:%s" %(self.method,self.testid))
                        self.no += 1
                except Exception as e:
                    print("主运行模块报错>>>>>\t",e)
            else:
                self.replay = RePlay(self.replayid)
                self.replay.runTest()


                result = getattr(self.req, self.method)(self.url, method=self.method, thisApi=self.forApidata,args=self.reqBody, extract=self.keyValue, lastApi=self.key)
                print(result)
                if type(result) == list:
                    result = result[-1]
                expect = eval(self.expect)
                try:
                    if result["StateCode"] == expect[0] and result["StateMsg"] == expect[1]:
                        if len(expect) == 3:
                            if result["ResultType"] == expect[2]:
                                print("接口:%s 测试通过 编号:%s" %(self.method,self.testid))
                                self.Pass += 1
                            elif result["ResultType"] != expect[2]:
                                print("接口:%s 测试不通过 编号:%s 响应状态:%s" %(self.method,self.testid,result["ResultType"]))
                                self.no += 1
                        else:

                            print("接口:%s 测试通过 编号:%s" %(self.method,self.testid))
                            self.Pass += 1
                    else:

                        print("接口:%s 测试未通过 编号:%s" %(self.method,self.testid))
                        self.no += 1
                except Exception as e:
                    print("主运行模块报错>>>>>\t", e)
Beispiel #3
0
class TestCase(object):

    def __init__(self):
        self.sql = BaseQuery()  # sql查询
        self.excel = Excel()  # excel操作
        self.req = API()  # 接口基本信息


    def startCase(self): #执行测试用例
        self.caseNum = self.excel.allCase()
        self.Pass = 0
        self.no = 0
        for i in range(1,self.caseNum):
            self.testid = self.excel.getValue(i,getId())
            self.url = self.excel.getValue(i,getUrl())
            self.method = self.excel.getValue(i,getApiName()) #请求地址的名字
            self.function = self.excel.getValue(i,getFunctionName()) #执行的方法名字
            self.forApidata = self.excel.getValue(i,getThisapi()) #放入本次接口的名字
            self.add1 = self.excel.getValue(i,getAdd1()) #额外参数一
            self.add2 = self.excel.getValue(i,getAdd2()) #额外参数二
            isrun = self.excel.getValue(i,getExecute()).strip()#是否执行
            self.expect = self.excel.getValue(i,getExpect())
            self.other = self.excel.getValue(i,getHeader())
            self.expect =  self.excel.getValue(i,getExpect())
            if isrun == "":
                print("发现第{0}测试用例是否执行栏出现空格现象".format(self.testid))
                break
            else:
                if isrun == "yes" or isrun == "Yes" or isrun == "YES":
                    self.httpContent()

                elif isrun == "no" or isrun == "No" or isrun == "NO":
                    pass
                else:
                    print("{0}出现未知命令".format(self.testid))
        print("通过%d个接口测试,未通过%d个接口测试" % (self.Pass, self.no))

    def httpContent(self):
        selectResult  = self.sql.selecet(self.testid)
        function = hasattr(self.req, self.function)
        if function:
            result = getattr(self.req, self.function)(self.url, method=self.method, thisApi=self.forApidata,add1=self.add1,add2=self.add2)
            expects = eval(self.expect)
            flag = True
            try:
                for i in expects:
                    jsonparse = parse("$..%s" % i)
                    ret = [match.value for match in jsonparse.find(result)][0]

                    if ret == expects[i]:
                        pass
                    else:
                        flag = False
                        Pass = "******"
                        print(Pass)
                        if selectResult ==  None:
                            self.sql.insert(self.testid, self.expect,Pass,json.dumps(result),nowTime)
                        elif selectResult["testid"] == self.testid:
                            self.sql.update(self.testid, self.expect, Pass, json.dumps(result),nowTime)
                        break
                if flag:
                    Pass = "******"
                    if selectResult == None:
                        self.sql.insert(self.testid,self.expect,Pass,json.dumps(result),nowTime)


                    elif selectResult["testid"] == self.testid:
                        print("update",self.testid)
                        self.sql.update(self.testid,self.expect,Pass,json.dumps(result),nowTime)

                    print(Pass)
            except Exception as e:
                print(e)
Beispiel #4
0
class TestCase(object):
    def __init__(self):
        #self.sql = BaseQuery()  # sql查询
        self.excel = Excel()  # excel操作
        self.req = API()  # 接口基本信息

    def startCase(self):  #执行测试用例
        self.caseNum = self.excel.allCase()
        self.Pass = 0
        self.no = 0
        for i in range(1, self.caseNum):
            self.testid = self.excel.getValue(i, getId())
            self.url = self.excel.getValue(i, getUrl())
            self.method = self.excel.getValue(i, getApiName())  #请求地址的名字
            self.function = self.excel.getValue(i, getFunctionName())  #执行的方法名字
            self.forApidata = self.excel.getValue(i, getThisapi())  #放入本次接口的名字
            self.add1 = self.excel.getValue(i, getAdd1())  #额外参数一
            self.add2 = self.excel.getValue(i, getAdd2())  #额外参数二
            isrun = self.excel.getValue(i, getExecute())  #是否执行
            self.expect = self.excel.getValue(i, getExpect())
            self.other = self.excel.getValue(i, getHeader())
            self.key = self.excel.getValue(i, getKeys())  #依赖参数的关键字
            #print(self.testid,self.url,isrun,self.method,self.function,self.forApidata,self.add1,self.add2,self.expect)
            if isrun == "":
                print("发现第{0}测试用例是否执行栏出现空格现象".format(self.testid))
                break
            else:
                if isrun == "yes" or isrun == "Yes" or isrun == "YES":
                    # testid,responseKey,responseSave,url,method,replayid,getvaluemethod,keyValue,forApidata,reqBody,save,key,value
                    self.httpContent()

                elif isrun == "no" or isrun == "No" or isrun == "NO":
                    pass
                    # print("{0}跳过测试用例的执行".format(self.testid))
                else:
                    print("{0}出现未知命令".format(self.testid))
        print("通过%d个接口测试,未通过%d个接口测试" % (self.Pass, self.no))

    def httpContent(self):

        function = hasattr(self.req, self.function)

        if function:
            print(self.function)
            result = getattr(self.req, self.function)(method=self.method,
                                                      thisApi=self.forApidata,
                                                      need=self.other,
                                                      key=self.key,
                                                      add1=self.add1)
            self.expect = eval(self.expect)
            flag = True
            try:
                for i in self.expect:

                    jsonparse = parse("$..%s" % i)
                    ret = [match.value for match in jsonparse.find(result)][0]

                    if ret == self.expect[i]:
                        pass
                    else:
                        flag = False
                        print("接口测试不通过")
                        break
                if flag:
                    print("接口测试通过")
            except Exception as e:
                print(e)