Beispiel #1
0
    def test_recharge(self, case):
        # 第一步:入参和用例数据
        url = case.url + case.interface
        # 获取充值之前的余额
        if case.check_sql:
            start_money = self.db.find_one(case.check_sql)[0]
            print("充值之前用户的余额是:{}".format(start_money))

        # 第二步:发送接口请求
        res = self.http.request(case.method, url, data=eval(case.data)).json()
        # 第三步:校验结果
        try:
            self.assertEqual(str(case.excepted_code), res["code"])
            # 获取充值之后的余额
            if case.check_sql:
                end_money = self.db.find_one(case.check_sql)[0]
                print("充值之后用户的余额是:{}".format(end_money))
                self.assertEqual(float(eval(case.data)["amount"]), float(end_money-start_money))
        except AssertionError as e:
            # 用例执行未通过
            self.excel.write_data(case.case_id+1, 10, "fail")
            logger.info("测试数据为:{}".format(case.data))
            logger.info("期望结果是:{}".format(case.excepted_code))
            logger.info("实际结果是:{}".format(res))
            logger.exception(e)
            raise e
        else:
            self.excel.write_data(case.case_id + 1, 10, "pass")
            logger.info("测试数据为:{}".format(case.data))
            logger.info("期望结果是:{}".format(case.excepted_code))
            logger.info("实际结果是:{}".format(res))
Beispiel #2
0
    def test_login(self, case):
        # 第一步:准备测试数据和入参
        excepted = case.excepted
        # url = case.url + case.interface
        url = my_conf.get('url', 'url') + case.url + case.interface
        case.data = data_replace(case.data)
        if "*phone*" in case.data:
            random_phone = self.random_phone()
            case.data = case.data.replace("*phone*", random_phone)
        request_data = eval(case.data)
        # 第二步:请求接口
        res = HTTPRequest().request(case.method, url, request_data)

        # 第三步:预期对比
        try:
            self.assertEqual(json.loads(excepted), res.json())
        except AssertionError as e:
            self.do_excel.write_data(case.case_id + 1, 8, "Fail")
            logger.info("测试数据为:{}".format(request_data))
            logger.info("期望结果是:{}".format(excepted))
            logger.info("实际结果是:{}".format(res.json()))
            logger.exception(e)
            raise e
        else:
            self.do_excel.write_data(case.case_id + 1, 8, "Pass")
            logger.info("测试数据为:{}".format(request_data))
            logger.info("期望结果是:{}".format(excepted))
            logger.info("实际结果是:{}".format(res.json()))
Beispiel #3
0
    def test_api(self, args):

        global COOKIES  #声明全局变量

        logger.info('正在执行第{0}条用例'.format(args['case_id']))
        logger.info('测试参数:{0}'.format(args))
        url = uri + args['apiName']
        #发起HTTP请求
        res = HttpRequests().http_request(url, args['method'],
                                          eval(args['param']), COOKIES)
        logger.info('返回结果:{0}'.format(res.json()))
        logger.info(type(args['case_id']))
        logger.info(COOKIES)
        #每一个登录请求之后都会产生一个cookies
        if res.cookies != {}:
            COOKIES = res.cookies  #如果cookies不为空,就对全局变量进行修改

        #断言,比对结果
        try:
            self.assertEqual(eval(res.json()['code']), args['expected'])
            test_result = 'pass'
        except Exception as e:
            logger.exception('断言出错啦,期望结果为:{0},实际结果为:{1}'.format(
                args['expected'],
                res.json()['code']))
            test_result = 'fail'
            raise e
        finally:
            excel_obj.write_back(args['case_id'] + 1, 8, res.text)
            excel_obj.write_back(args['case_id'] + 1, 9, test_result)
Beispiel #4
0
class TestAdd(unittest.TestCase):
    """加标接口"""
    data_file_path = os.path.join(DATA_DIR, "case_data.xlsx")
    excel = ReadExcel(data_file_path, 'add')
    cases = excel.read_data_obj()
    http = HTTPSession()
    db = ReadMysql(my_conf.get('mysql', 'host'), my_conf.get('mysql', 'user'),
                   my_conf.get('mysql', 'password'),
                   my_conf.getint('mysql', 'port'),
                   my_conf.get('mysql', 'database'))

    @classmethod
    def setUpClass(cls):
        logger.debug("-------开始加标接口测试{}-------")

    @classmethod
    def tearDownClass(cls):
        logger.debug("-------结束加标接口测试{}-------")

    @data(*cases)
    def test_add(self, case):
        # 第一步:入参和用例数据
        url = my_conf.get('url', 'url') + case.url + case.interface
        case.data = data_replace(case.data)
        if "*memberId*" in case.data:
            max_id = self.db.find_one("select max(id) from member")[0]
            memberId = max_id + 1
            case.data = case.data.replace("*memberId*", str(memberId))

        # 判断是否需要sql校验
        if case.check_sql:
            case.check_sql = data_replace(case.check_sql)
            # 获取当前用户加标签的数量
            start_count = self.db.find_count(case.check_sql)

        # 第二步:发送接口请求
        res = self.http.request(case.method, url, data=eval(case.data)).json()
        print(res)
        res_code = res['code']
        # 第三步:校验结果
        try:
            self.assertEqual(str(case.excepted), res_code)
            if case.check_sql:
                case.check_sql = data_replace(case.check_sql)
                # 获取当前用户加标签的数量
                end_count = self.db.find_count(case.check_sql)
                self.assertEqual(1, end_count - start_count)
        except AssertionError as e:
            # 用例执行未通过
            self.excel.write_data(case.case_id + 1, 8, "fail")
            logger.info("测试数据为:{}".format(case.data))
            logger.info("期望结果是:{}".format(case.excepted_code))
            logger.info("实际结果是:{}".format(res))
            logger.exception(e)
            raise e
        else:
            self.excel.write_data(case.case_id + 1, 8, "pass")
            logger.info("测试数据为:{}".format(case.data))
            logger.info("期望结果是:{}".format(case.excepted))
            logger.info("实际结果是:{}".format(res))
Beispiel #5
0
    def test_register(self, case):
        # 第一步:准备测试数据和入参
        excepted = case.excepted
        url = my_conf.get('url', 'url') + case.url + case.interface
        # 替换动态化参数
        random_phone = self.random_phone()
        case.data = case.data.replace("*phone*", random_phone)
        request_data = eval(case.data)

        # 第二步:发送接口请求
        res = HTTPRequest().request(case.method, url, request_data)

        # 第三步:比对结果
        try:
            self.assertEqual(json.loads(excepted), res.json())
            try:
                if case.check_sql:
                    logger.debug("此条用例走了if语句")
                    count = self.db.find_count(
                        case.check_sql.replace("*phone*", random_phone))
                    self.assertEqual(1, count)
                    logger.info("测试数据为:1")
                    logger.info("期望结果是:{}".format(count))
                else:
                    logger.debug("此条用例没有走if语句")
            except AssertionError as e:
                logger.exception(e)
                raise e
        except AssertionError as e:
            self.do_excel.write_data(case.case_id + 1, 8, "Fail")
            logger.info("测试数据为:{}".format(request_data))
            logger.info("期望结果是:{}".format(excepted))
            logger.info("实际结果是:{}".format(res.json()))
            logger.exception(e)
            raise e
        else:
            self.do_excel.write_data(case.case_id + 1, 8, "Pass")
            logger.info("测试数据为:{}".format(request_data))
            logger.info("期望结果是:{}".format(excepted))
            logger.info("实际结果是:{}".format(res.json()))
Beispiel #6
0
 def test_api(self, args):
     logger.info('正在执行第{0}条用例'.format(args['case_id']))
     logger.info('测试参数:{0}'.format(args))
     url = uri + args['apiName']
     #发起HTTP请求
     res = HttpRequests().http_request(url, args['method'],
                                       eval(args['param']))
     logger.info('返回结果:{0}'.format(res.json()))
     logger.info(type(args['case_id']))
     #断言,比对结果
     try:
         self.assertEqual(eval(res.json()['code']), args['expected'])
         test_result = 'pass'
     except Exception as e:
         logger.exception('断言出错啦,期望结果为:{0},实际结果为:{1}'.format(
             args['expected'],
             res.json()['code']))
         test_result = 'fail'
         raise e
     finally:
         excel_obj.write_back(args['case_id'] + 1, 8, res.text)
         excel_obj.write_back(args['case_id'] + 1, 9, test_result)
Beispiel #7
0
 def http_request(self, url, method, args):
     if method.lower() == 'get':
         try:
             logger.info('发起get请求')
             result = requests.get(url, args)
         except Exception as e:
             logger.exception('发起get请求出错啦,错误为:%s', e)
             raise e
     elif method.lower() == 'post':
         try:
             logger.info('发起post请求')
             result = requests.post(url, args)
         except Exception as e:
             logger.exception('发起post请求出错啦,错误为:%s', e)
             raise e
     else:
         logger.exception('请求类型错误,发起请求失败')
     return result
Beispiel #8
0
        print(res)
        res_code = res['code']

        # 判断是否是执行的加标用例
        if case.interface == "audit":
            # 提取标id
            loan_id = self.db.find_one("select Id from loan where MemberId='{}' order by id desc limit 1".format(my_conf.getint('data','memberId')))
            # 将添加的标id保存为临时变量
            setattr(ConText, "loan_id", str(loan_id[0]))

        # 第三步:校验结果
        try:
            self.assertEqual(str(case.excepted), res_code)
            if case.check_sql:
                case.check_sql = data_replace(case.check_sql)
                # 获取当前审核状态
                status = self.db.find_one(case.check_sql)[0]
                self.assertEqual(eval(case.data)["status"], status)
        except AssertionError as e:
            # 用例执行未通过
            self.excel.write_data(case.case_id+1, 8, "fail")
            logger.info("测试数据为:{}".format(case.data))
            logger.info("期望结果是:{}".format(case.excepted_code))
            logger.info("实际结果是:{}".format(res))
            logger.exception(e)
            raise e
        else:
            self.excel.write_data(case.case_id + 1, 8, "pass")
            logger.info("测试数据为:{}".format(case.data))
            logger.info("期望结果是:{}".format(case.excepted))
            logger.info("实际结果是:{}".format(res))