Exemplo n.º 1
0
class BaseTestCase(unittest.TestCase):
    def __init__(self):
        # 读取表格
        self.excelUtil = OperationExcel()
        # sheet_0
        self.sheet_0 = self.excelUtil.get_exceldata()
        # 黄色标题列清除
        for i in range(self.excelUtil.get_lines() - 1):
            # row = self.sheet_0.row_values(i + 1)
            self.excelUtil.write_cell_value(i + 1, 12, "")
            self.excelUtil.write_cell_value(i + 1, 13, "")
            self.excelUtil.write_cell_value(i + 1, 14, "")
            self.excelUtil.write_cell_value(i + 1, 15, "")
Exemplo n.º 2
0
class TestMethod():
    Login_token = "MTk3NHwxNTc5MjQzODU1MzgzfPx0BM7Xqb_pXEoajcKRH9BQ2QIIsyuIym9d2XqeO-fL"

    def __init__(self):
        # 读取表格
        self.excelUtil = OperationExcel()
        # sheet_0
        self.sheet_0 = self.excelUtil.get_exceldata()
        # 黄色标题列清除
        for i in range(self.excelUtil.get_lines() - 1):
            # row = self.sheet_0.row_values(i + 1)
            self.excelUtil.write_cell_value(i + 1, 12, "")
            self.excelUtil.write_cell_value(i + 1, 13, "")
            self.excelUtil.write_cell_value(i + 1, 14, "")
            self.excelUtil.write_cell_value(i + 1, 15, "")

    def test01(self):
        urllib3.disable_warnings()
        # 场景
        envTemp = ""
        # 用例
        example = ""

        genKeyValue = ""
        for i in range(self.excelUtil.get_lines() - 1):
            row = self.sheet_0.row_values(i + 1)
            if row[1] != "":
                envTemp = row[1]

            if row[2] != "":
                example = row[2]

            print("开始 场景:" + envTemp + ",用例:" + example)

            # 处理是否运行
            if "Y" != row[4]:
                print("结束 场景:" + envTemp + ",用例:" + example + ",原因: 是否运行:N")
                continue

            # 1 调用发起前 参数搜集/检查
            # 是否需要token
            body = row[8]
            isToken = row[5]
            if "Y" == isToken:
                # TODO 如果需要TOKEN
                global Login_token
                Login_token = "MTk3NHwxNTc5MjQzODU1MzgzfPx0BM7Xqb_pXEoajcKRH9BQ2QIIsyuIym9d2XqeO-fL"
                if "" == Login_token:
                    print("Login_token 为空!")
                else:
                    print("Login_token : " + Login_token)
                    body = body.replace("#{token}", Login_token)
                    pass

            # 2 发起调用 需要什么参数  url method header body
            url = row[3]
            method = row[6]
            header = row[7]
            # 引用其他接口返回结果
            refKey = row[10]

            print('body类型为:', type(body))
            if "" != refKey:
                # TODO 获取其他参数返回结果
                for refKeyValue in refKey.split(","):
                    kvs = refKeyValue.split(".")
                    rowId = kvs[0]
                    genKey = kvs[1]
                    tempRow = self.getRow(rowId)
                    print('tempRow:', tempRow)
                    genKeyValues = tempRow[12]
                    print('genKeyValues:', genKeyValues)
                    for genKeyValue in genKeyValues.split(","):
                        if genKeyValue != "" and ":" in genKeyValue:
                            genkvs = genKeyValue.split(":")
                            genkvs_key = genkvs[0]
                            genkvs_value = genkvs[1]
                            print("genkvs_key:", genkvs_key)
                            print('genkvs_value:', genkvs_value)
                            if genKey == genkvs_key:
                                body = body.replace("#{" + genKey + "}",
                                                    genkvs_value)

            print('请求参数body为:', body)

            # TODO body中的占位符替换

            result = ""
            if "post" == method:
                result = requests.post(url=url,
                                       json=json.loads(body),
                                       headers=json.loads(header),
                                       verify=False)
            elif "get" == method:
                result = requests.get(url=url,
                                      params=json.loads(body),
                                      headers=json.loads(header),
                                      verify=False)
            else:
                print("unknown method!")
                continue

            resultJson = result.json()
            print("返回结果:", resultJson)
            # 实际结果写入表格
            self.excelUtil.write_cell_value(i + 1, 13, str(resultJson))

            # 3 结果处理
            exceptResult = row[9]
            # 3.1 结果是否通过
            if result.status_code == 200:
                # 如果预期结果不止一个code TODO 支持多个参数结果检查
                # if resultJson["status"] != "":
                #     print('运行失败:'+resultJson)
                if resultJson["status"] == int(exceptResult.split(":")[1]):
                    self.excelUtil.write_cell_value(i + 1, 14, "Y")
                    # 3.2 返回信息 参数化
                    genKey = row[11]
                    if genKey != "":
                        # TODO 考虑多参数/参不在data层 结果->参数
                        genKeyValue = genKey + ":" + resultJson[genKey]
                        print('参数化的genkeyValue为;', genKeyValue)
                        self.excelUtil.write_cell_value(i + 1, 12, genKeyValue)
                    print("结束 场景:" + envTemp + ",用例:" + example + ',参数为:' +
                          genKeyValue)
                else:
                    self.excelUtil.write_cell_value(i + 1, 14, "N")
            elif resultJson["status"] == "":
                print('运行失败:' + str(resultJson))

            # 3.2 返回信息 参数化
            # if row[14] == "Y":
            #     genKey = row[11]
            #     if genKey != "" :
            #         # TODO 考虑多参数/参不在data层 结果->参数
            #         genKeyValue = genKey + ":" + resultJson["data"][genKey]
            #         print('参数化的genkeyValue为;',genKeyValue)
            #         self.excelUtil.write_cell_value(i + 1, 12, genKeyValue)
            #     print("结束 场景:" + envTemp + ",用例:" + example+',参数为:'+ genKeyValue)
            # elif row[14] == 'N':
            #     pass

        pass

    def getRow(self, rowId):
        sheet0 = self.excelUtil.get_exceldata()
        for i in range(self.excelUtil.get_lines() - 1):
            row = sheet0.row_values(i + 1)
            if row[0] == rowId:
                return row
Exemplo n.º 3
0
class RunMain():
    def __init__(self):
        self.operaExcel = OperationExcel()
        self.sheet = self.operaExcel.get_exceldata()
        for i in range(self.operaExcel.get_lines() - 1):
            self.operaExcel.write_cell_value(i + 1, 12, "")
            self.operaExcel.write_cell_value(i + 1, 13, "")
            self.operaExcel.write_cell_value(i + 1, 14, "")
            self.operaExcel.write_cell_value(i + 1, 15, "")

    def run(self):
        #场景
        exampleCode = ""
        #用例
        example = ""

        # 循环表数据,执行请求
        for i in range(self.operaExcel.get_lines() - 1):
            # 根据行号获取该行的内容
            row_data = self.sheet.row_values(i + 1)
            if row_data[1] != "":
                exampleCode = row_data[1]

            if row_data[2] != "":
                example = row_data[2]
            print('测试模块为:' + exampleCode + ",用例名为:" + example)

            #是否携带token
            if row_data[5] == 'N':
                pass

            if row_data[8] != "":
                request_data = row_data[8]

            if row_data[3] != "":
                url = row_data[3]

            if row_data[7] != "":
                isHeader = row_data[7]

            if row_data[6] != "":
                method = row_data[6]

            if row_data[4] != "":
                isRun = row_data[4]

            if row_data[11] != "":
                responseKey = row_data[11]

            #引用的key
            needKey = row_data[10]

            if needKey != "":
                for needKeyValue in needKey.split(","):
                    # 获取值
                    needKey_Values = needKeyValue.split(".")
                    getExampleId = needKey_Values[0]
                    getResponseKey = needKey_Values[1]
                    # 根据exampleId 获取整行数据
                    rowData = self.operaExcel.get_Row(getExampleId)
                    # 拿到该行提供的key value值
                    responseKeyValues = rowData[12]

                    for responseKeyValue in responseKeyValues.split(','):
                        if responseKeyValue != "" and ":" in responseKeyValue:
                            genkvs = responseKeyValue.split(":")
                            genkvs_key = genkvs[0]
                            genkvs_value = genkvs[1]
                            print('返回的key:' + genkvs_key + ",返回key对应的值为:" +
                                  genkvs_value)
                            # 判断需要引用的key和提供的key值是否相同
                            if genkvs_key == getResponseKey:
                                # 把提供的value值替换params里的值
                                request_data = request_data.replace(
                                    "#{" + getResponseKey + "}", genkvs_value)

            result = ""  # 实际结果
            print("-------------")
            print(type(request_data))
            print("请求参数为:" + str(request_data))

            if isRun == 'Y':  #执行
                if method == 'post':
                    result = requests.post(url=url,
                                           json=json.loads(request_data),
                                           headers=json.loads(isHeader))
                elif method == 'get':
                    result = requests.get(url=url,
                                          params=json.loads(request_data),
                                          headers=json.loads(isHeader))

                else:
                    print("请求方法为:", method)

                resultJson = result.json()

                # 把结果写到表格里
                self.operaExcel.write_cell_value(i + 1, 13, str(resultJson))

                if row_data[14] != "":
                    isPass = row_data[14]

                expectResult = ""  # 预期结果
                if row_data[9] != "":
                    expectResult = row_data[9]

                #预期 实际结果的判断
                if result.status_code == 200:
                    if int(expectResult.split(":")[1]) == resultJson["status"]:
                        self.operaExcel.write_cell_value(i + 1, 14, "Y")
                    else:
                        self.operaExcel.write_cell_value(i + 1, 14, "N")

            #返回信息  参数化
                if row_data[14] == "Y":
                    getResponseKey = row_data[11]
                    if getResponseKey != "":
                        genkvs_value = getResponseKey + ":" + resultJson[
                            "data"][getResponseKey]
                        print("参数化值为:" + genkvs_value)
                        self.operaExcel.write_cell_value(
                            i + 1, 12, genkvs_value)
                    print("结束 场景:" + exampleCode + ",用例:" + example + ',参数为:' +
                          genkvs_value)
Exemplo n.º 4
0
class my_test:

    def __init__(self):
        # 读取表格
        self.excelUtil = OperationExcel()
        # sheet_0
        self.sheet_0 = self.excelUtil.get_exceldata()
        # 黄色标题列清除
        for i in range(self.excelUtil.get_lines() - 1):
            # row = self.sheet_0.row_values(i + 1)
            self.excelUtil.write_cell_value(i + 1, 12, "")
            self.excelUtil.write_cell_value(i + 1, 13, "")
            self.excelUtil.write_cell_value(i + 1, 14, "")
            self.excelUtil.write_cell_value(i + 1, 15, "")


    def run(self):
        #场景
        envTemp = ""
        #用例
        example = ""

        for i in range(self.excelUtil.get_lines()-1):
            row = self.sheet_0.row_values(i + 1)
            if row[1] != "":
                envTemp = row[1]

            if row[2] != "":
                example = row[2]

            print("开始 场景:"+envTemp+",用例:"+example)

            #处理是否运行
            if "Y" != row[4]:
                print('结束场景:' + envTemp+",用例:"+example+",原因为:"+row[4])
                continue

            #1.调用发起前  参数搜集/检查
            #是否需要token
            isToken = row[5]
            if "Y" == isToken:
                pass

            #2.发起调用 需要的参数
            url = row[3]
            method = row[6]
            header = row[7]

            #引用其他接口返回结果
            refKey = row[10]

            body = row[8]
            print("请求参数为:"+body)

            if "" != refKey:
                for refKeyValue in refKey.split(","):
                    kvs = refKeyValue.split(".")
                    rowId = kvs[0]
                    genKey = kvs[1]
                    tempRow = self.getRow(rowId)
                    genKeyValues = tempRow[12]
                    for genKeyValue in genKeyValues.split(","):
                        if genKeyValue != "" and genKeyValue.contains(":"):
                            genkvs = genKeyValue.split(":")
                            genkvs_key = genkvs[0]
                            genkvs_value = genkvs[1]
                            if genKey == genkvs_value:
                                body = body.replace("#{"+genKey+"}",genkvs_value)
            print("请求参数body为:"+body)

            result = ""
            if "post" == method:
                result = requests.post(url=url,json=json.loads(body),headers = json.loads(header))
            elif "get" == method:
                result = requests.get(url = url,json=json.loads(body),headers=json.loads(header))
            else:
                print("unknown method")
                continue

            resultJson = result.json()
            print("返回的结果为:"+str(resultJson))
            #实际结果写入表格
            self.excelUtil.write_cell_value(i+1,13,str(resultJson))

            #3.结果处理
            excpetResult = row[9]
            #3.1 结果是否通过
            if result.status_code != 200:
                pass

            #如果预期结果不止一个code
            if resultJson["status"] != "":
                print("运行失败:"+str(resultJson))
            elif resultJson["code"] == int(excpetResult.split(":")[1]):
                self.excelUtil.write_cell_value(i+1,14,"Y")
            else:
                self.excelUtil.write_cell_value(i+1,14,"N")

            #3.2返回信息 参数化
            genKey = row[11]
            if genKey != "":
                #TODO 考虑多参数/参数不在data层 结果——》参数
                genKeyValue = genKey+":"+resultJson["data"][genKey]
                print("参数化的genKeyValue为:"+genKeyValue)
                self.excelUtil.write_cell_value(i+1,12,genKeyValue)
            print("结束场景:"+envTemp+",用例:"+example+",参数为:"+genKeyValue)




    def getRow(self,rowId):
        sheet0 = self.excelUtil.get_exceldata()
        for i in range(self.excelUtil.get_lines() - 1):
            row = sheet0.row_values(i + 1)
            if row[0] == rowId:
                return row