Ejemplo n.º 1
0
    def test_login(self, case):
        # 第一步:准备用例数据
        url = conf.get("env", "url") + case["url"]
        method = case["method"]

        data = eval(case["data"])
        headers = eval(conf.get("env", "headers"))
        expected = eval(case["expected"])
        row = case["case_id"] + 1
        # 第二步:发送请求,获取结果
        response = self.request.send(url=url,
                                     method=method,
                                     json=data,
                                     headers=headers)
        res = response.json()
        # 第三步:断言(比对预期结果和实际结果)
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertEqual(expected["msg"], res["msg"])
        except AssertionError as e:
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例:{},执行未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.info("用例:{},执行未通过".format(case["title"]))
Ejemplo n.º 2
0
    def test_add(self, case):
        # 第一步:准备数据
        url = conf.get("env", "url") + case["url"]
        headers = eval(conf.get("env", "headers"))
        headers["Authorization"] = getattr(CaseDate, "admin_token_value")
        data = eval(replace_data(case["data"]))
        expected = eval(case["expected"])
        method = case["method"]
        row = case["case_id"] + 1
        # 第二步:发请求获取实际结果
        response = self.request.send(url=url,
                                     method=method,
                                     json=data,
                                     headers=headers)
        res = response.json()

        # 第三步:断言(比对预期结果和实际结果)
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertEqual(expected["msg"], res["msg"])
            # 数据库校验

        except AssertionError as e:
            print("预期结果:", expected)
            print("实际结果:", res)
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例:{},执行未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.info("用例:{},执行未通过".format(case["title"]))
Ejemplo n.º 3
0
 def __init__(self):
     # 创建一个连接对象
     self.conn = pymysql.connect(host=conf.get("db", "host"),
                                 port=conf.getint("db", "port"),
                                 user=conf.get("db", "user"),
                                 password=conf.get("db", "pwd"),
                                 charset=conf.get("db", "charset"),
                                 cursorclass=pymysql.cursors.DictCursor)
     # 创建一个游标
     self.cur = self.conn.cursor()
Ejemplo n.º 4
0
    def test_recharge(self, case):
        # 第一步:准备用例数据
        url = conf.get("env", "url") + case["url"]
        method = case["method"]
        # 替换参数中的用户id
        case["data"] = replace_data(case["data"])
        data = eval(case["data"])
        headers = eval(conf.get("env", "headers"))
        headers["Authorization"] = getattr(CaseDate,"token_value")
        # 在请求头中加入setupclass中提取出来的token
        expected = eval(case["expected"])
        row = case["case_id"] + 1
        # 第二步:发送请求,获取结果
        # 发送请求之前,获取用余额
        if case["check_sql"]:
            sql = "SELECT leave_amount FROM futureloan.member WHERE mobile_phone={}".format(
                conf.get("test_data", "phone"))
            # 查询当前用户的余额
            start_money = self.db.find_one(sql)["leave_amount"]

        response = self.request.send(url=url, method=method, json=data, headers=headers)
        res = response.json()
        # 发送请求之后,获取用余额
        if case["check_sql"]:
            sql = "SELECT leave_amount FROM futureloan.member WHERE mobile_phone={}".format(
                conf.get("test_data", "phone"))
            # 查询当前用户的余额
            end_money = self.db.find_one(sql)["leave_amount"]
        # 第三步:断言(比对预期结果和实际结果)
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertEqual(expected["msg"], res["msg"])
            # 判断示范需要进行sql校验
            if case["check_sql"]:
                self.assertEqual(end_money - start_money, Decimal(str(data["amount"])))
        except AssertionError as e:
            print("预期结果:", expected)
            print("实际结果:", res)
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例:{},执行未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.info("用例:{},执行未通过".format(case["title"]))
Ejemplo n.º 5
0
    def test_login(self, case):
        # 第一步:准备用例数据
        url = conf.get("env", "url") + case["url"]
        method = case["method"]
        case["data"] = replace_data(case["data"])
        data = eval(case["data"])
        headers = eval(conf.get("env", "headers"))
        # 判断是否是登录接口,不是登录接口则需要添加token
        if case["interface"] != "login":
            headers["Authorization"] = getattr(CaseDate, "token_value")

        expected = eval(case["expected"])
        row = case["case_id"] + 1
        # 第二步:发送请求,获取结果
        response = self.request.send(url=url,
                                     method=method,
                                     json=data,
                                     headers=headers)
        res = response.json()
        # 发送请求后,判断是否是登陆接口
        if case["interface"].lower() == "login":
            # 提取用户id保存为类属性
            CaseDate.member_id = str(jsonpath.jsonpath(res, "$..id")[0])
            token = jsonpath.jsonpath(res, "$..token")[0]
            token_type = jsonpath.jsonpath(res, "$..token_type")[0]
            # 提取token,保存为类属性
            CaseDate.token_value = token_type + " " + token
        # 判断是否是加标的用例,如果是的则请求标id
        if case["interface"] == "add":
            CaseDate.loan_id = str(jsonpath.jsonpath(res, "$..id")[0])
        # 第三步:断言(比对预期结果和实际结果)
        try:
            self.assertEqual(expected["code"], res["code"])
            # self.assertEqual(expected["msg"], res["msg"])
            self.assertIn(expected["msg"], res["msg"])

        except AssertionError as e:
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例:{},执行未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.info("用例:{},执行未通过".format(case["title"]))
Ejemplo n.º 6
0
 def setUpClass(cls):
     """管理员账户登录"""
     url = conf.get("env", "url") + "/member/login"
     data = {
         "mobile_phone": conf.get("test_data", "admin_phone"),
         "pwd": conf.get("test_data", "admin_pwd")
     }
     headers = eval(conf.get("env", "headers"))
     response = cls.request.send(url=url,
                                 method="post",
                                 json=data,
                                 headers=headers)
     res = response.json()
     token = jsonpath.jsonpath(res, "$..token")[0]
     token_type = jsonpath.jsonpath(res, "$..token_type")[0]
     member_id = str(jsonpath.jsonpath(res, "$..id")[0])
     # 将提取的数据保存到CaseData的属性中
     CaseDate.admin_token_value = token_type + " " + token
     CaseDate.admin_member_id = member_id
Ejemplo n.º 7
0
 def setUpClass(cls):
     # 1、准备登录的数据
     url = conf.get("env", "url") + "/member/login"
     data = {
         "mobile_phone": conf.get("test_data", "phone"),
         "pwd": conf.get("test_data", "pwd")
     }
     headers = eval(conf.get("env", "headers"))
     # 3、发送请求,进行登录
     response = cls.request.send(url=url, method="post", json=data, headers=headers)
     # 获取返回的数据
     res = response.json()
     # 3、提取token,保存为类属性
     token = jsonpath.jsonpath(res, "$..token")[0]
     token_type = jsonpath.jsonpath(res, "$..token_type")[0]
     # 将提取到的token设为CaseData类属性
     CaseDate.token_value = token_type + " " + token
     # 提取用户的id,设为CaseData类属性
     CaseDate.member_id = str(jsonpath.jsonpath(res, "$..id")[0])
Ejemplo n.º 8
0
    def create_logger():
        # 创建收集器,设置收集器的等级
        mylog = logging.getLogger(conf.get('log', 'name'))
        mylog.setLevel(conf.get("log", "level"))
        # 创建输出到控制台的渠道,设置等级
        sh = logging.StreamHandler()
        sh.setLevel(conf.get('log', 'sh_level'))
        mylog.addHandler(sh)
        # 创建输出到文件的渠道,设置等级
        fh = logging.FileHandler(filename=os.path.join(LOGDIR, "log.log"),
                                 encoding="utf8")
        fh.setLevel(conf.get("log", 'fh_level'))
        mylog.addHandler(fh)
        # 设置日志输出格式
        formater = '%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s: %(message)s'
        fm = logging.Formatter(formater)
        sh.setFormatter(fm)
        fh.setFormatter(fm)

        return mylog
Ejemplo n.º 9
0
    def test_audit(self, case):
        # 第一步:准备用例数据
        url = conf.get("env", "url") + case["url"]
        method = case["method"]
        case["data"] = replace_data(case["data"])
        data = eval(case["data"])
        headers = eval(conf.get("env", "headers"))
        headers["Authorization"] = getattr(CaseDate, "admin_token_value")
        expected = eval(case["expected"])
        row = case["case_id"] + 1
        # 第二步:发送请求,获取结果
        response = self.request.send(url=url,
                                     method=method,
                                     json=data,
                                     headers=headers)
        res = response.json()
        # 判断是否是审核通过的这条用例,并且审核通过
        if res["code"] == 0 and case["title"] == "审核通过":
            CaseDate.pass_loan_id = str(data["loan_id"])

        # 第三步:断言(比对预期结果和实际结果)
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertEqual(expected["msg"], res["msg"])
            # 判断是否需要进行sql校验
            if case["check_sql"]:
                sql = replace_data(case["check_sql"])
                status = self.db.find_one(sql)["status"]
                # 断言数据库中的标状态字段是否和预期一致。
                self.assertEqual(expected["status"], status)
        except AssertionError as e:
            print("预期结果", expected)
            print("实际结果", res)
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例:{},执行未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.info("用例:{},执行未通过".format(case["title"]))
Ejemplo n.º 10
0
 def setUp(self) -> None:
     """进行加标"""
     # 1、准备加标的数据
     url = conf.get("env", "url") + "/loan/add"
     headers = eval(conf.get("env", "headers"))
     headers["Authorization"] = getattr(CaseDate, "admin_token_value")
     data = {
         "member_id": getattr(CaseDate, "admin_member_id"),
         "title": "借钱实现财富自由",
         "amount": 2000,
         "loan_rate": 12.0,
         "loan_term": 3,
         "loan_date_type": 1,
         "bidding_days": 5
     }
     # 2、发送请求,添加项目
     response = self.request.send(url=url,
                                  method="post",
                                  json=data,
                                  headers=headers)
     res = response.json()
     # 3、提取审核需要用到的项目id
     CaseDate.loan_id = str(jsonpath.jsonpath(res, "$..id")[0])
Ejemplo n.º 11
0
    def test_register(self, case):
        # 第一步:准备用例数据
        url = conf.get("env", "url") + case["url"]
        method = case["method"]
        # 生成一个手机号码
        phone = self.random_phone()
        # 替换用例数据中的手机号码
        case["data"] = case["data"].replace("#phone#", phone)
        data = eval(case["data"])
        headers = eval(conf.get("env", "headers"))
        expected = eval(case["expected"])
        row = case["case_id"] + 1

        # 第二步:发送请求,获取结果
        response = self.request.send(url=url,
                                     method=method,
                                     json=data,
                                     headers=headers)
        res = response.json()

        # 第三步:断言(比对预期结果和实际结果)
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertEqual(expected["msg"], res["msg"])
            if case["check_sql"]:
                sql = "SELECT * FROM futureloan.member WHERE mobile_phone={}".format(
                    data["mobile_phone"])
                count = self.db.find_count(sql)
                self.assertEqual(1, count)
        except AssertionError as e:
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例:{},执行未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.info("用例:{},执行未通过".format(case["title"]))
Ejemplo n.º 12
0
def replace_data(s):
    r1 = r"#(.+?)#"
    # 根据是否匹配到要替换的数据,来决定要不要进入循环
    while re.search(r1, s):
        # 匹配一个需要替换的内容
        res = re.search(r1, s)
        # 获取待替换的内容
        data = res.group()
        # 获取需要替换的字段
        key = res.group(1)
        try:
            # 根据要替换的字典,去配置文件中找到对应的数据,进行替换
            s = s.replace(data, conf.get("test_data", key))
        except Exception:
            # 如果配置文件中找不到,报错了,则去CaseData的属性中找对应的值进行替换
            s = s.replace(data, getattr(CaseDate, key))
    return s
Ejemplo n.º 13
0
 def test_withdraw(self, case):
     # 第一步:准备用例数据
     url = conf.get("env", "url") + case["url"]
     case["data"] = case["data"].replace("#phone#", conf.get("test_data", "phone"))
     case["data"] = case["data"].replace("#pwd#", conf.get("test_data", "pwd"))
     headers = eval(conf.get("env", "headers"))
     # 判断是否是取现接口,取现接口则加上请求头
     if case["interface"].lower() == "withdraw":
         headers["Authorization"] = self.token_value
         case["data"] = case["data"].replace("#member_id#", str(self.member_id))
     data = eval(case["data"])
     expected = eval(case["expected"])
     method = case["method"]
     row = case["case_id"] + 1
     # 判断是否需要进行sql校验
     if case["check_sql"]:
         sql = case["check_sql"].format(conf.get("test_data","phone"))
         start_money = self.db.find_one(sql)["leave_amount"]
     # 第二步:调用接口,获取实际结果
     response = self.request.send(url=url, method=method, json=data, headers=headers)
     res = response.json()
     # 判断是否是登录接口
     if case["interface"].lower() == "login":
         # 提取用户id保存为类属性
         TestWithdraw.member_id = jsonpath.jsonpath(res, "$..id")[0]
         token = jsonpath.jsonpath(res, "$..token")[0]
         token_type = jsonpath.jsonpath(res, "$..token_type")[0]
         # 提取token,保存为类属性
         TestWithdraw.token_value = token_type + " " + token
     # 第三步:断言(比对预期结果和实际结果)
     try:
         self.assertEqual(expected["code"], res["code"])
         self.assertEqual(expected["msg"], res["msg"])
         if case["check_sql"]:
             sql = case["check_sql"].format(conf.get("test_data","phone"))
             end_money = self.db.find_one(sql)["leave_amount"]
             # 比对取现金额是否正确
             self.assertEqual(Decimal(str(data["amount"])),start_money-end_money)
     except AssertionError as e:
         print("预期结果:", expected)
         print("实际结果:", res)
         self.excel.write_data(row=row, column=8, value="未通过")
         log.error("用例:{},执行未通过".format(case["title"]))
         log.exception(e)
         raise e
     else:
         self.excel.write_data(row=row, column=8, value="通过")
         log.info("用例:{},执行未通过".format(case["title"]))