Beispiel #1
0
 def __init__(self):
     config = ConfigLoader()
     host = config.get('mysql', 'host')
     port = config.getint('mysql', 'port')  # port 是一个数值
     user = config.get('mysql', 'usr')
     password = config.get('mysql', 'pwd')
     # 异常处理
     self.mysql = pymysql.connect(host=host,
                                  user=user,
                                  password=password,
                                  port=port,
                                  cursorclass=pymysql.cursors.DictCursor)
Beispiel #2
0
 def __init__(self):
     config = ConfigLoader()
     host = config.get('mysql', 'host')
     port = config.getint('mysql', 'port')
     user = config.get('mysql', 'usr')
     password = config.get('mysql', 'pwd')
     #自行添加异常处理
     try:
         self.mysql = pymysql.connect(
             host=host,
             user=user,
             password=password,
             port=port,
             cursorclass=pymysql.cursors.DictCursor)  #建立连接
         print('数据库连接成功!')
     except AssertionError as e:
         print('数据库连接失败!', e)
         raise e
Beispiel #3
0
 def __init__(self,method,url,data=None,cookies=None,headers=None):
     config = ConfigLoader()
     url_pre = config.get('api','url_pre')
     try:
         if method == 'get':
             self.resp = requests.get(url=url_pre+url, params=data, cookies=cookies, headers=headers)
         elif method == 'post':
             self.resp = requests.post(url=url_pre+url, data=data, cookies=cookies, headers=headers)
         elif method == 'delete':
             self.resp = requests.delete(url=url_pre+url, data=data, cookies=cookies, headers=headers)
     except Exception as e:
         raise e
Beispiel #4
0
    sheet_names = do_excel.get_sheet_names()  # 获取Excel文件中全部sheet名称,返回为一个列表
    print("sheet名称列表:",sheet_names)
    cases_list = ['login', 'register'] # 定义一个执行测试用例的列表

    #  根据sheet_names 分别取到全部用例来执行
    for sheet_name in sheet_names:
        # 获取login的所有测试用例,返回的是一个cases列表,列表里面是多个Case类实例,每个实例的属性对应Excel表中每列的值
        if sheet_name in cases_list: #如果当前的这个sheet_name不在可执行的case_list里面,就不执行
            cases = do_excel.get_cases(sheet_name)
            print(sheet_name+' 测试用例个数:', len(cases))
            for case in cases:  # 遍历测试用例列表,每进for一次,就取一个case实例
                print("case信息:", case.__dict__)  # 打印case信息  对象的内置的私有方法
                # data = eval(case.data)  # Excel里面取到data是一个字符串,使用eval函数将字符串转换成字典
                data = json.loads(case.data)
                config = ConfigLoader()
                url_pre = config.get('api','url_pre')
                url = url_pre + case.url
                resp = Request(method=case.method, url=url, data=data)  # 通过封装的Request类来完成接口的调用
                print('status_code:', resp.get_status_code())  # 打印响应码
                resp_dict = resp.get_json()  # 获取请求响应,字典
                # 通过json.dumps函数将字典转换成格式化后的字符串
                resp_text = json.dumps(resp_dict, ensure_ascii=False, indent=4)
                print('response: ', resp_text)  # 打印响应

                if case.expected == resp.get_text():
                    print("result : PASS")
                    do_excel.write_result_by_case_id(sheet_name=sheet_name,case_id=case.case_id,
                                                     actual=resp.get_text(),result='PASS')
                else:
                    print("result : FAIL")
                    do_excel.write_result_by_case_id(sheet_name=sheet_name,case_id=case.case_id,