Пример #1
0
class RechargeTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, "recharge")  # 引入这个excel文件
    cases = excel.get_cases()  # 获取excel的值

    @classmethod
    def setUpClass(cls):
        cls.http_request = HttpRequest2()  # 实例化sessio
        cls.mysql = DoMysql()

    @data(*cases)
    def test_recharge(self, case):
        print(case.title)
        #请求之前判断是否执行sql
        #如果执行的sql不是一条,可以用字典,或者列表
        if case.sql is not None:
            sql = eval(case.sql)['sql1']
            member = self.mysql.fetch_one(sql)  #执行sql
            print(member['LeaveAmount'])
            before = member['LeaveAmount']  #取sql里面的值,存到before这个变量里面
        resp = self.http_request.request(case.method, case.url, case.data)
        actual_code = resp.json()['code']
        try:
            self.assertEqual(str(case.expected), actual_code)
            self.excel.write_result(case.case_id + 1, resp.text, "PASS")
            #成功之后,判断是否执行SQL
            if case.sql is not None:
                sql = eval(case.sql)['sql1']
                member = self.mysql.fetch_one(sql)
                print(member['LeaveAmount'])
                after = member['LeaveAmount']
                recharge_amount = int(eval(case.data)['amount'])  #充值金额
                self.assertEqual(before + recharge_amount, after)
        except AssertionError as e:
            self.excel.write_result(case.case_id + 1, resp.text, "FILE")
            raise e
Пример #2
0
class RegisterTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, "register")
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        cls.http_request = HttpRequest2()
        cls.mysql = do_mysql.DoMysql()  #为了避免资源浪费,所有的请求都在一个链接里面完成

    @data(*cases)
    def test_register(self, case):
        if case.data.find(
                'register_mobile') > -1:  #如果find没找到这个'register_mobile',就返回-1
            sql = 'select min(mobilephone) as phone from future.member'
            min_phone = self.mysql.fetch_one(sql)[
                'phone']  #查询最大/最小手机号,fetch-one返回的是一个元祖
            #最大手机号码+1,从字符串转成int+1
            max_phone = int(min_phone) + 45
            case.data = case.data.replace('register_mobile',
                                          str(max_phone))  #替换参数值,字符串是不可变数组,
            # 会生成一个新的值,不能再原有的上面替换,所以要给一个case.data值接收
            print(case.data)
        resp = self.http_request.request(case.method, case.url, case.data)
        try:
            self.assertEqual(case.expected, resp.text)
            self.excel.write_result(case.case_id + 1, resp.text, "PASS")
            if case.sql is not None:
                sql = case.sql
                member = self.mysql.fetch_one(sql)
                print(member['MobilePhone'])
                after_phone = member['MobilePhone']
                self.assertEqual(int(min_phone) + 45, int(after_phone))
        except AssertionError as e:
            self.excel.write_result(case.case_id + 1, resp.text, "FAIL")
            raise e
Пример #3
0
class RechargeTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, "add")  # 引入这个excel文件
    cases = excel.get_cases()  # 获取excel的值

    @classmethod
    def setUpClass(cls):
        cls.http_request = HttpRequest2()  # 实例化sessio

    @data(*cases)
    def test_add(self, case):
        # 使用字典的方式替换
        # case.data = eval(case.data)
        # if case.data.__contains__('mobilephone') and case.data['mobilephone'] == 'normal_user':
        #     case.data['mobilephone'] = config.get('case','normal_user')  # 拿到配置文件里面的的值,赋值给case.data
        # if case.data.__contains__('pwd') and case.data['pwd'] == 'normal_pwd':
        #     case.data['pwd'] = config.get('case','normal_pwd')  # 拿到配置文件里面的值,赋值给case.data
        # if case.data.__contains__('memberId') and case.data['memberId'] == 'loan_member_id':
        #     case.data['memberId'] = config.get('case','loan_member_id')  # 拿到配置文件里面的值,赋值给case.data
        # print(case.data)
        # print(case.title)
        case.data = context.replace(case.data)  #使用context的replace函数做替换
        resp = self.http_request.request(case.method, case.url, case.data)

        try:
            self.assertEqual(str(case.expected), resp.json()['code'])
            self.excel.write_result(case.case_id + 1, resp.text, "PASS")
        except AssertionError as e:
            self.excel.write_result(case.case_id + 1, resp.text, "FILE")
            raise e
Пример #4
0
class LoginTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, "login")
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.info("准备测试前置")
        cls.http_request = HttpRequest2()

    @data(*cases)
    def test_login(self, case):
        logger.info("测试的title:{0}".format(case.title))
        resp = self.http_request.request(case.method, case.url, case.data)
        try:
            self.assertEqual(case.expected, resp.text)
            self.excel.write_result(case.case_id + 1, resp.text, "PASS")
        except AssertionError as e:
            self.excel.write_result(case.case_id + 1, resp.text, "Fail")
            logger.error("测试报错了:{0}".format(e))
            raise e
        logger.info('结束测试:{0}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试结束后置处理')
        cls.http_request.close()
Пример #5
0
class BidloanTeat(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, "bidLoan")
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        cls.http_request = HttpRequest2()
        cls.mysql = do_mysql.DoMysql()  # 为了避免资源浪费,所有的请求都在一个链接里面完成

    @data(*cases)
    def test_bidloan(self, case):
        print("开始执行测试", case.title)
        print(case.url)
        print(case.data)
        case.data = context.replace(case.data)
        resp = self.http_request.request(case.method, case.url, case.data)
        print(resp.text)
        try:
            self.assertEqual(str(case.expected), resp.json()['code'])
            self.excel.write_result(case.case_id + 1, resp.text, "PASS")
            #增加数据库的操作,判断加标成功后,取到loan_id
            if resp.json()["msg"] == "加标成功":
                sql = 'select * from future.loan where MemberID=88538 order by id desc limit 1'
                loan_id = self.mysql.fetch_one(sql)['Id']
                print("加标之后的标id", loan_id)
                #保存到类属性里面
                setattr(Context, "loan_id", str(loan_id))
        except Exception as e:
            self.excel.write_result(case.case_id + 1, resp.text, "FAIL")
            raise e

    @classmethod
    def tearDownClass(cls):
        cls.http_request.close()
        cls.mysql.close()


#!!!!注意写sql语句的时候,一定要带上表名,不然就会报 pymysql.err.InternalError: (1046, 'No database selected')