Beispiel #1
0
 def test_add_loan(self, init_req, data_info):
     # 对excel中的固定数据进行替换
     data_info["data"] = replace_data(yaml_key="add_loan", data_info=data_info["data"])
     # 对excel中上下文关联的数据进行替换
     data_info["data"] = sub_data(init_req, data_info["data"])
     # 发送请求1,获取创建项目前项目的总数
     before_loan_num = Context(init_req).get_loan_num()
     # 发送创建项目请求
     res = init_req.run_main(method=data_info["method"], url=host + data_info["url"],
                             data=json.loads(data_info["data"]))
     # 获取状态码
     code = res.json()["code"]
     if code == '10001':
         # 发送请求2,获取创建项目成功后项目的总数
         after_loan_num = Context(init_req).get_loan_num()
         expected_loan_num = before_loan_num + 1
     # 进行断言
     try:
         # 1.状态码断言
         assert code == str(data_info["expected"])
         if code == "10001":
             # 2.请求返回的项目总数断言
             assert after_loan_num == expected_loan_num
         # 3.数据库中的项目总数断言
         # ....
         # 将断言结果写入excel表中
         excel_handler.write_excel("addloan", data_info["case_id"] + 1, 9, "pass")
         logger.info("pass")
     except AssertionError as e:
         excel_handler.write_excel("addloan", data_info["case_id"] + 1, 9, "fail")
         logger.exception("fail")
         raise e
Beispiel #2
0
def rm_log():
    """
    清除2天前的日志
    :return:
    """
    try:
        # 获取log目录下的log文件
        log_list = os.listdir(PyConfig.log_path)
    except FileNotFoundError as e:
        logger.error("文件路径错误")
        raise e
    length = len(log_list)
    while length > 0:
        for i in log_list:
            # 获取log文件的绝对路径
            path = r"%s\%s" % (PyConfig.log_path, i)
            # 获取log文件最后修改时的时间戳
            create = os.stat(path).st_mtime
            # 获取当前时间的时间戳
            now = time.time()
            # 进行判断是否大于两天
            if (now - create) / 24 / 60 / 60 > 2:
                try:
                    os.remove(path)
                except Exception as e:
                    logger.error("删除日志失败")
                    raise e
            length -= 1
        logger.info("没有2天前的日志了")
Beispiel #3
0
 def test_audit(self, data_info, init_req):
     # 对excel中的数据进行替换
     data_info["data"] = replace_data("audit", data_info["data"])
     # 对excel中的带*号数据进行替换
     if "*loan_id*" in data_info["data"]:
         loan_id = Context(init_req).get_loan_id
         data_info["data"] = data_info["data"].replace("*loan_id*", loan_id)
     # 发送请求
     res = init_req.run_main(method=data_info["method"],
                             url=host + data_info["url"],
                             data=json.loads(data_info["data"]))
     # 获取状态码
     code = res.json()["code"]
     # 进行断言
     try:
         # 1.状态码断言
         assert code == str(data_info["expected"])
         # 2.数据库中项目的status是否修改成功
         # ...
         # 将断言结果写入excel中
         excel_handler.write_excel("audit", data_info["case_id"] + 1, 9,
                                   "pass")
         logger.info("pass")
     except AssertionError as e:
         logger.exception("fail")
         excel_handler.write_excel("audit", data_info["case_id"] + 1, 9,
                                   "fail")
         raise e
Beispiel #4
0
 def test_register(self, init_req, data_info):
     # 对excel中的固定的数据进行替换
     data_info["data"] = replace_data("user_info", data_info["data"])
     # 对excel中的不固定数据进行替换
     if "*phone*" in data_info["data"]:
         phone = get_phone()
         data_info["data"] = data_info["data"].replace("*phone*", phone)
     # 发送请求
     result = init_req.run_main(method=data_info['method'],
                                url=self.host + data_info["url"],
                                data=json.loads(data_info["data"]))
     # 获取状态码
     code = result.json()["code"]
     try:
         # 进行断言
         # 1.状态码断言
         assert code == str(data_info["expected"])
         # 2.查看数据库是否新增对应的新用户
         # ...
         logger.info("PASS")
         excel_handler.write_excel('registe', data_info["case_id"] + 1, 9,
                                   "pass")
     except AssertionError as e:
         logger.error("FAIL")
         excel_handler.write_excel('registe', data_info["case_id"] + 1, 9,
                                   "fail")
         raise e
Beispiel #5
0
 def load_sheet(self, sheet_name):
     try:
         self.sheet = self.wb[sheet_name]
     except Exception as e:
         logger.exception("加载excel表单失败")
         raise e
     logger.info("加载excel表单成功")
     return True
Beispiel #6
0
 def write_yaml(self, data):
     try:
         with open(self.file_path, mode='w', encoding='utf8') as f:
             yaml.dump(stream=f, data=data, allow_unicode=True)
     except Exception as e:
         logger.exception("写入yaml文件失败")
         raise e
     logger.info("写入yaml文件成功 {}".format(data))
     return True
Beispiel #7
0
def get_phone():
    """
    获取一个随机的手机号
    :return:
    """
    mobile: str = '1' + str(random.choice([3, 5, 7]))
    for i in range(9):
        mobile += str(random.randint(0, 9))
    logger.info("生成的手机号为:%s" % mobile)
    return mobile
Beispiel #8
0
 def close(self):
     """
     关闭数据库
     :return:T/F
     """
     if self.conn and self.cur:
         self.cur.close()
         self.conn.close()
         logger.info("关闭数据库成功")
         return True
     else:
         return False
Beispiel #9
0
 def write_excel(file_path, sheet_name, row, column, data):
     """
     写入excel
     """
     try:
         wb = openpyxl.load_workbook(file_path)
         sheet = wb[sheet_name]
     except Exception as e:
         logger.error("打开excel失败")
         raise e
     cell = sheet.cell(row, column)
     cell.value = data
     wb.save(file_path)
     logger.info("写入excel {}成功,数据:{}".format(sheet_name, data))
Beispiel #10
0
 def execute(self, sql):
     """
     执行sql,主要用于写入操作
     :param sql: sql语句
     :return: True/False
     """
     if self.conn and self.cur:
         try:
             self.cur.execute(sql)
         except Exception as e:
             logger.error("执行sql失败{}".format(sql))
             raise e
         self.conn.commit()
         logger.info("执行sql成功{}".format(sql))
         return True
Beispiel #11
0
 def test_withdraw(self, init_req, data_info):
     # 对excel中固定的数据进行替换
     data_info["data"] = replace_data("user_info", data_info["data"])
     # 对excel中不固定的数据进行替换(new_phone)
     if "*mobilephone*" in data_info["data"]:
         new_phone = get_phone()
         data_info["data"] = data_info["data"].replace("*mobilephone*", new_phone)
     data_info["data"] = json.loads(data_info["data"])
     # 发送请求1,获取接口返回的初始金额
     before_amount = Context(init_req).leave_mount
     # 发送取现请求
     res = init_req.run_main(method=data_info["method"], url=self.host + data_info["url"], data=data_info["data"])
     res = res.json()
     # 获取状态码code
     code = res["code"]
     if code == "10001":
         # 获取接口返回的取现后的金额
         after_amount = jsonpath(res, "$..leaveamount")[0]
         # 获取充值金额
         amount = data_info["data"]["amount"]
         # 预期金额
         expected_amount = Decimal(before_amount) - Decimal(amount)
     # 进行断言
     try:
         # 断言1:状态码断言
         assert code == data_info["expected"]
         # 断言2:接口返回的金额断言
         if code == "10001":
             assert float(after_amount) == float(expected_amount)
         # 断言3:数据库断言
         # ...
         # 将断言结果写入excel表中
         excel_handler.write_excel("withdraw", data_info["case_id"] + 1, 9, "pass")
         logger.info("pass")
     except AssertionError as e:
         logger.exception("fail")
         # 将断言结果写入excel表中
         excel_handler.write_excel("withdraw", data_info["case_id"] + 1, 9, "fail")
         raise e
Beispiel #12
0
 def test_recharge(self, data_info, init_req):
     # 对excel中固定的数据进行替换
     data_info["data"] = replace_data("user_info", data_info["data"])
     # 对excel中异常的数据进行替换
     if "*mobilephone*" in data_info["data"]:
         phone = get_phone()
         data_info["data"] = data_info["data"].replace(
             "*mobilephone*", phone)
     data = json.loads(data_info["data"])
     # 发送请求1获取获取请求前的金额
     before_amount = Context(init_req).leave_mount
     # 发送请求2
     res = init_req.run_main(data_info["method"],
                             url=self.host + data_info["url"],
                             data=data)
     res = res.json()
     # 获取状态码和充值后返回数据中的金额
     code = res["code"]
     if code == "10001":
         after_amount = jsonpath(res, "$..leaveamount")[0]
         expected_amount = Decimal(before_amount) + Decimal(data["amount"])
     # 进行断言
     try:
         # 状态码断言
         assert code == str(data_info["expected"])
         # 返回数据中的金额断言
         if code == "10001":
             assert float(after_amount) == float(expected_amount)
         # 数据库断言
         # ....
         # 将断言结果写入excel
         excel_handler.write_excel("recharge", data_info["case_id"] + 1, 9,
                                   'pass')
         logger.info('pass')
     except AssertionError as e:
         logger.exception("fail")
         excel_handler.write_excel("recharge", data_info["case_id"] + 1, 9,
                                   'fail')
         raise e
Beispiel #13
0
 def test_login(self, init_req, data_info):
     # 对excel中固定的数据进行替换
     data_info["data"] = replace_data("user_info", data_info["data"])
     # 对excel中的异常数据进行替换
     if "*mobilephone*" in data_info["data"]:
         phone = get_phone()
         data_info["data"] = data_info["data"].replace("*mobilephone*", phone)
     # 发送请求
     res = init_req.run_main(method=data_info["method"], url=self.host + data_info["url"],
                             data=json.loads(data_info["data"]))
     # 获取code
     code = res.json()["code"]
     # 进行断言
     try:
         # 断言状态码
         assert code == str(data_info["expected"])
         # 断言结果写入excel
         excel_handler.write_excel('login', data_info["case_id"] + 1, 9, "PASS")
         logger.info("PASS")
     except AssertionError as e:
         logger.exception("FAIL")
         excel_handler.write_excel('login', data_info["case_id"] + 1, 9, "FAIL")
         raise e
Beispiel #14
0
 def test_invest(self, init_req, data_info):
     # 对excel中的固定数据进行替换
     data_info["data"] = replace_data("user_info", data_info["data"])
     # 对excel中的上下关联数据进行替换
     data_info["data"] = sub_data(init_req, data_info["data"])
     # 将数据由json格式转换为字典形式
     data_info["data"] = json.loads(data_info["data"])
     # 发送请求1,获取投资前用户的可用余额
     before_amount = Context(init_req).leave_mount
     # 发送投资请求
     res = BaseRequest().run_main(method=data_info["method"], url=self.host + data_info["url"],
                                  data=data_info["data"])
     # 获取code
     res = res.json()
     code = res["code"]
     # 发送请求2,获取投资后用户的可用余额
     if code == '10001':
         after_amount = float(Context(init_req).leave_mount)
         amount = data_info["data"]["amount"]
         expected_amount = float(Decimal(before_amount) - Decimal(amount))
     # 进行断言
     try:
         # 断言1:状态码code断言
         assert code == json.loads(data_info["expected"])
         # 断言2:接口返回的金额断言
         if code == '10001':
             assert after_amount == expected_amount
         # 断言3:数据库中的用户金额断言
         # 断言4:invest 表是否新增一条投资记录
         # 断言5:是否新增一条流水记录保存到 financeLog 表
         # 将断言结果写入excel表中
         logger.info("pass")
         excel_handler.write_excel('invest', data_info["case_id"] + 1, 9, "pass")
     except AssertionError as e:
         logger.exception("fail")
         excel_handler.write_excel('invest', data_info["case_id"] + 1, 9, "fail")
         raise e
Beispiel #15
0
 def select(self, sql, args=[], one=True):
     """
     查询数据库
     :param one:
     :param sql: sql语句
     :param args: sql参数
     :return: 执行结果
     """
     if self.conn and self.cur:
         try:
             self.cur.execute(sql, args)
         except Exception as e:
             logger.exception("执行sql失败 {}{}".format(sql, args))
             raise e
         if one:
             res = self.cur.fetchone()
             self.conn.commit()
         else:
             res = self.cur.fetchall()
             self.conn.commit()
         logger.info("执行sql成功 {}".format(sql, args))
         return res
     else:
         return "未连接数据库"
Beispiel #16
0
def init_data_clean():
    logger.info("******开始执行测试用例******")
    rm_log()
    yield
    logger.info("******测试用例执行完毕******")