Exemple #1
0
    def test_01(self, item):
        # 准备数据
        method = item['method']
        url = d1.get('url', 'base_url') + item['url']

        # 动态变更参数,字符串替换参数
        params = eval(replace_data(item['data'], TestAdd))

        expected = eval(item['expected'])

        # 获取实际结果
        res = requests.request(method=method,
                               url=url,
                               json=params,
                               headers=self.headers)
        print('预期结果:', expected)
        print('实际结果:', res.json())

        # 断言
        try:
            self.assertEqual(expected['code'], res.json()['code'])
            self.assertEqual(expected['msg'], res.json()['msg'])

        except AssertionError as e:
            my_log.error(f'用例执行--{item["title"]}--失败')
            raise e
        else:
            my_log.info(f'用例执行--{item["title"]}--成功')
Exemple #2
0
class TestLogin(unittest.TestCase):
    # 读取excel测试数据
    word = HandleExcel(DATAS_PATH + '/data.xlsx', 'login')
    cases = word.read_excel()
    # 配置文件获取基本地址
    base_url = d1.get('url', 'base_url')
    headers = eval(d1.get('headers', 'headers'))

    @list_data(cases)
    def test_01(self, item):
        # 准备数据(方法、url、参数、请求头、预期结果、title)
        method = item['method']
        url = self.base_url + item['url']
        params = eval(item['data'])
        headers = self.headers
        expected = eval(item['expected'])
        title = item['title']
        # row = item['case_id']+1

        # 获取实际结果
        response = requests.request(method=method,
                                    url=url,
                                    json=params,
                                    headers=headers)
        print(response.json())
        # 断言
        try:
            self.assertEqual(expected['code'], response.json()['code'])
            self.assertEqual(expected['msg'], response.json()['msg'])
        except AssertionError as e:
            # 数据回写(会很影响速度,如果没有要求,不写)
            # write = HandleExcel(workname=DATAS_PATH+'/data.xlsx', sheetname='login')
            # write.write_excel(row=row, column=8, value='不通过')
            # 日志
            my_log.error(f'{title}-用例执行失败')
            # my_log.exception(e)
            raise e
        else:
            # # 数据回写(会很影响速度,如果没有要求,不写)
            # write = HandleExcel(workname=DATAS_PATH+'/data.xlsx', sheetname='login')
            # write.write_excel(row=row, column=8, value='通过')
            # 日志
            my_log.info(f'{title}-用例执行通过')
    def test_01(self, item):
        # 准备数据
        method = item['method']
        url = d1.get('url', 'base_url') + item['url']

        # 动态变更参数,字符串替换参数
        # params = eval(item['data'].replace('#member_id#', self.member_id))
        params = eval(replace_data(item['data'], TestCharge))

        headers = self.headers
        expected = eval(item['expected'])

        # ------------------数据库获取充值前的金额---------------------------
        sql = f'select leave_amount from futureloan.member where mobile_phone={d1.get("login", "mobile_phone")};'
        exc_sql = self.db.find_one(sql)[0]

        # 获取实际结果
        res = requests.request(method=method,
                               url=url,
                               json=params,
                               headers=headers)
        print('预期结果:', expected)
        print('实际结果:', res.json())
        # ------------------数据库获取充值后的金额---------------------------
        res_sql = self.db.find_one(sql)[0]
        print('充值前金额:', exc_sql)
        print('充值后金额:', res_sql)

        # 断言
        try:
            self.assertEqual(expected['code'], res.json()['code'])
            self.assertEqual(expected['msg'], res.json()['msg'])
            # ------------------断言数据库充值金额------------------
            if item['check_sql'] == 1:
                self.assertEqual(float(params['amount']),
                                 float(res_sql) - float(exc_sql))

        except AssertionError as e:
            my_log.error(f'用例执行--{item["title"]}--失败')
            raise e
        else:
            my_log.info(f'用例执行--{item["title"]}--成功')
Exemple #4
0
def replace_data(data, cls):
    """
    批量替换测试数据中的数据
    :param data:需替换的数据(str)
    :param cls:测试类
    :return:替换后的数据
    """
    while re.search('#(.+?)#', data):
        # search:匹配并返回第一个符合规则的匹配对象
        res = re.search('#(.+?)#', data)
        # 获取需要替换的数据
        old = res.group()
        # 获取新数据属性名
        attr = res.group(1)
        try:
            # 根据属性名获取类属性
            new = str(getattr(cls, attr))
        except AttributeError:
            # 如果类属性没有,就去配置文件找
            new = d1.get('login', attr)
        # 替换
        data = data.replace(old, new)
    return data
class TestRegister(unittest.TestCase):
    # 读取excel测试数据
    word = HandleExcel(DATAS_PATH + '/data.xlsx', 'register')
    cases = word.read_excel()
    # 配置文件获取基本地址
    base_url = d1.get('url', 'base_url')
    # 获取headers(配置文件中获取的是str,需要eval()转换一下)
    headers = eval(d1.get('headers', 'headers'))
    # 实例化操作数据库的方法
    db = HandleDB()

    @list_data(cases)
    def test_01(self, item):
        # 准备数据(方法、url、参数、请求头、预期结果、title)
        method = item['method']
        url = self.base_url + item['url']

        # 动态处理参数
        params = eval(item['data'].replace('#mobile_phone#', self.randow_phone()))

        headers = self.headers
        expected = eval(item['expected'])
        title = item['title']
        row = item['case_id'] + 1

        # 获取实际结果
        response = requests.request(method=method, url=url, json=params, headers=headers)
        print('预期结果:', expected)
        print('实际结果:', response)

        # 断言
        try:
            self.assertEqual(expected['code'], response.json()['code'])
            self.assertEqual(expected['msg'], response.json()['msg'])
        except AssertionError as e:
            # 数据回写(会很影响速度,如果没有要求,不写)
            write = HandleExcel(workname=DATAS_PATH + '/data.xlsx', sheetname='register')
            write.write_excel(row=row, column=8, value='不通过')
            # 日志
            my_log.error(f'{title}-用例执行失败')
            print(expected)
            print(response.json())
            raise e
        else:
            # 数据回写(会很影响速度,如果没有要求,不写)
            write = HandleExcel(workname=DATAS_PATH + '/data.xlsx', sheetname='register')
            write.write_excel(row=row, column=8, value='通过')
            # 日志
            my_log.info(f'{title}-用例执行通过')

    def randow_phone(self):
        """封装随机生成手机号的方法,数据库检查是否注册过"""
        # while True:
        #     b = (176, 175, 174, 170, 171, 172, 173, 177)
        #     sql = 'select mobile_phone from futureloan.member;'
        #     db_phone = self.db.find_all(sql)
        #     print(db_phone)
        #     random_phone = random.randint(17600000000, 17699999999)
        #     print(random_phone)
        #
        #     if random_phone in db_phone:
        #         continue
        #     else:
        #         break
        # return random_phone
        return str(random.randint(17600000000, 17699999999))