Esempio n. 1
0
    def test_add(self, data_namedtuple):
        case_id = data_namedtuple.case_id + 1
        title = data_namedtuple.title
        new_data = HandleParameter.add_parameter(data_namedtuple.data)

        response = TestAdd.do_request(method=data_namedtuple.method,
                                      url=do_config("api", "prefix_url") +
                                      data_namedtuple.url,
                                      data=new_data)

        run_success_msg = do_config("msg", "success_result")
        run_fail_msg = do_config("msg", "fail_result")

        try:
            self.assertEqual(data_namedtuple.expected,
                             response.text,
                             msg="测试【{}】失败".format(title))
        except AssertionError as e:
            do_log.error("{},测试结果为:{}\n,具体异常信息为{}".format(
                title, run_fail_msg, e))
            self.parser_excel.write_result_to_excel(row=case_id,
                                                    actual=response.text,
                                                    result=run_fail_msg)
            raise e
        else:
            do_log.info("{},测试结果为:{}".format(title, run_success_msg))
            self.parser_excel.write_result_to_excel(row=case_id,
                                                    actual=response.text,
                                                    result=run_success_msg)
Esempio n. 2
0
    def __call__(self, method, url, data=None, is_json=False, **kwargs):
        method = method.lower()
        if isinstance(data, str):
            try:
                data = json.loads(data)
            except Exception as e:
                do_log.error("将json格式数据转换为python中字典类型时,出现异常:{}".format(e))
                data = eval(data)

        if method == "get":
            resp = self.one_session.request(method=method, url=url, params=data, **kwargs)
        elif method == "post":
            if is_json:
                resp = self.one_session.request(method=method, url=url, json=data, **kwargs)
            else:
                resp = self.one_session.request(method=method, url=url, data=data, **kwargs)
        else:
            do_log.error("不支持【{}】请求方法".format(method))
            resp = None
        return resp
    def test_recharge(self, data_namedtuple):
        run_success_msg = do_config("msg", "success_result")
        run_fail_msg = do_config("msg", "fail_result")
        case_id = data_namedtuple.case_id+1
        title = data_namedtuple.title
        new_data = json.loads(HandleParameter.recharge_parameter(data_namedtuple.data))
        check_sql = data_namedtuple.check_sql
        if check_sql:
            leaveAmount_before_recharge = TestRecharge.do_mysql(check_sql, args=(new_data["mobilephone"], ))

        response = TestRecharge.do_request(method=data_namedtuple.method,
                                           url=do_config("api", "prefix_url")+data_namedtuple.url,
                                           data=new_data)
        actual = json.loads(response.text)
        actual["data"] = None

        try:
            self.assertEqual(200, response.status_code, msg="测试【{}】时,请求失败,状态码为【{}】".
                             format(title, response.status_code))
        except AssertionError as e:
            do_log.error("具体异常信息为:".format(e))
            raise e

        try:
            self.assertEqual(json.loads(data_namedtuple.expected),
                             actual,
                             msg="测试【{}】失败".format(title))
            if check_sql:
                leaveAmount_after_recharge = TestRecharge.do_mysql(check_sql, args=(new_data["mobilephone"],))
                actual_amount = leaveAmount_after_recharge["LeaveAmount"] - leaveAmount_before_recharge["LeaveAmount"]
                self.assertEqual(float(new_data["amount"]),
                                 round(float(actual_amount)),
                                 msg="充值金额有误")

        except AssertionError as e:
            do_log.error("{},测试结果为:{}\n,具体异常信息为{}".format(title, run_fail_msg, e))
            self.parser_excel.write_result_to_excel(row=case_id, actual=response.text, result=run_fail_msg)
            raise e
        else:
            do_log.info("{},测试结果为:{}".format(title, run_success_msg))
            self.parser_excel.write_result_to_excel(row=case_id, actual=response.text, result=run_success_msg)
Esempio n. 4
0
    def test_invest(self, data_namedtuple):
        case_id = data_namedtuple.case_id + 1
        title = data_namedtuple.title
        new_data = HandleParameter.invest_parameter(data_namedtuple.data)

        response = TestInvest.do_request(method=data_namedtuple.method,
                                         url=do_config("api", "prefix_url") +
                                         data_namedtuple.url,
                                         data=new_data)

        run_success_msg = do_config("msg", "success_result")
        run_fail_msg = do_config("msg", "fail_result")
        """反射"""
        if response.json().get("msg") == "加标成功":
            check_sql = data_namedtuple.check_sql
            if check_sql:
                check_sql = HandleParameter.invest_parameter(check_sql)
                sql_data = TestInvest.do_mysql(sql=check_sql)
                HandleParameter.loan_id = sql_data.get("Id")
                # setattr(HandleParameter, "loan_id", loan_id)

        try:
            self.assertEqual(data_namedtuple.expected,
                             response.text,
                             msg="测试【{}】失败".format(title))
        except AssertionError as e:
            do_log.error("{},测试结果为:{}\n,具体异常信息为{}".format(
                title, run_fail_msg, e))
            self.parser_excel.write_result_to_excel(row=case_id,
                                                    actual=response.text,
                                                    result=run_fail_msg)
            raise e
        else:
            do_log.info("{},测试结果为:{}".format(title, run_success_msg))
            print("{}: {}".format(type(response.text), response.text))
            self.parser_excel.write_result_to_excel(row=case_id,
                                                    actual=response.text,
                                                    result=run_success_msg)
Esempio n. 5
0
 def __call__(self, sql, args=None, is_more=False):
     """
     如果sql为查询,返回一条记录或多条记录,如果sql为非查询,则返回受影响的行数
     :param sql:查询sql或非查询sql
     :param args:sql语句的参数,为序列类型,默认为None
     :param is_more:默认为False, 当sql为查询时,默认返回一条记录
     :return:返回一条记录或多条记录或受影响的行数
     """
     # 3.执行sql
     try:
         self.cursor.execute(sql, args)
         if sql.split()[0].lower() == 'select':
             if is_more:
                 result = self.cursor.fetchall()
             else:
                 result = self.cursor.fetchone()
             return result
         # 4.事务提交
         else:
             self.conn.commit()
             return self.cursor.rowcount
     except Exception as e:
         do_log.error(e)
         self.conn.rollback()