示例#1
0
    def get_xls(self):
        '''
        读取测试用例,
        xls_name为Excel表格名称
        sheet_name为sheet的名称
        '''
        # config.ini文件存放路径
        config_path = GetPath().get_conf_path()
        # 读取配置文件config.ini
        config = configparser.ConfigParser()
        config.read(config_path)
        # 获取[excel]中指定的file_name
        file_name = config.get('excel', 'file_name')
        # 获取[excel]中指定的sheet_name
        sheet_name = config.get('excel', 'sheet_name')

        # 测试用例文件路径:../TestCase
        case_path = os.path.join(GetPath().get_case_dir(), file_name)
        log = Log(__name__).getlog()
        # 判断用例文件是否存在
        if not os.path.isfile(case_path):
            # 记录到日志并退出python程序
            log.error("测试用例文件不存在!")
            os._exit(1)

        file = open_workbook(case_path)  # 打开EXCEL
        sheets = sheet_name.split('|')
        cls = []  # 用于存储用例
        for s in sheets:
            try:
                sheet = file.sheet_by_name(s)  # 打开指定sheet
            except XLRDError as e:
                log.error("config.ini文件中sheet_name编辑错误")
                raise
            nrows = sheet.nrows  # sheet的行数
            for i in range(nrows):
                if sheet.row_values(i)[0] != 'Num':  # 过滤顶栏目录
                    if sheet.row_values(i)[9] != 'no':  # 过滤未激活的用例
                        cls.append(sheet.row_values(i))
        return cls
示例#2
0
    def testrequest(self, URL, URI, Param, RequestForm, File, CheckPoint,
                    Headers, num, CaseName):
        Current_class = os.path.basename(__file__)
        print(Current_class)
        log = Log(Current_class)
        # 可选
        '''
        Headers = {'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'}
        Headers = {'Content-Type':'application/json;charset=utf-8'}
        '''
        # 生成请求类的对象
        req = Interface_Request()
        # req_get = req.req_get(url,params = params,Headers = Headers)
        # 请求前缀和接口地址的拼接
        full_url = URL + URI

        # 判断请求类型
        if (RequestForm == 'GET'):
            # 调用请求类的函数,得到返回结果
            self.req_test = req.req_get(full_url, Param, Headers)

        # elif (RequestForm == 'POST' and DataForm == 'Form'):
        #     self.req_test = req.post_kv(full_url, Param, Headers)
        # elif (RequestForm == 'POST' and DataForm == 'Json'):
        #     Headers = {'Content-Type': 'application/json;charset=utf-8'}
        #     self.req_test = req.post_json(full_url, Param, Headers)
        # elif (RequestForm == 'POST' and DataForm == 'File'):
        #     Headers = {'Content-Type': 'multipart/form-data;boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA'}
        #     self.req_test = req.post_json(full_url, Param, Headers)

        elif (RequestForm == 'POST'
              and re.search("application/x-www-form-urlencoded", Headers)):
            self.req_test = req.post_kv(full_url, Param, Headers)
        elif (RequestForm == 'POST'
              and re.search("application/json", Headers)):
            self.req_test = req.post_json(full_url, Param, Headers)
        elif (RequestForm == 'POST'
              and re.search("multipart/form-data", Headers)):
            Headers = {'Content-Type': 'multipart/form-data'}
            self.req_test = req.post_json(full_url, File, Headers)
        else:
            print("请求不通过,请检查case用例配置:{0}-{1}".format(num, CaseName))
            log.error("请求不通过,请检查case用例配置:{0}-{1}".format(num, CaseName))
            return "请求不通过,请检查case用例配置:{0}-{1}".format(num, CaseName), ''

        # 检查点与响应数据做对比
        if (re.search(CheckPoint, str(self.req_test))):
            log.info("用例编号" + str(num) + " " + CaseName + "接口执行成功")
            return "成功", str(self.req_test)
        else:
            log.error("用例编号" + str(num) + " " + CaseName + "接口执行失败")
            return "失败", str(self.req_test)


# #请求前缀
# url = "http://p.3.cn"
# #接口地址
# uri = "/prices/mgets"
# params = {"skuIds":"100004770249","type":"1"}
# Headers = {}
# #请求类型
# RequestForm = 'GET'
# #数据类型
# dataform = 'json'
# #检查点
# checkpoint = '"id":"J_100004770249"'
#
# it = InterfaceTest()
# it.testrequest(url,uri,params,RequestForm,dataform,checkpoint,Headers)