Esempio n. 1
0
    def request(self, method, url, paramType, jsons=None, headers=None):
        method = method.upper()  # 将method强制转成全大小
        if type(jsons) == str:
            try:
                jsons = json.loads(jsons)
            except:
                jsons = eval(jsons)
        # 拼接URL
        url = config.get('api', 'pre_url') + url
        logger.debug('请求url:{0}'.format(url))
        logger.debug('请求data:{0}'.format(jsons))

        if paramType == 'param':
            resp = self.session.request(method=method,
                                        url=url,
                                        headers=headers,
                                        params=jsons,
                                        verify=False)
        elif paramType == 'json':
            if json:
                resp = self.session.request(method=method,
                                            url=url,
                                            headers=headers,
                                            json=jsons,
                                            verify=False)
        else:
            resp = None
            logger.error('UN-support method')

        logger.debug('请求返回的值:{0}'.format(resp.text))
        return resp
Esempio n. 2
0
class CustomerTest(unittest.TestCase):
    excel = do_excel.DoExcel(config.get('case', 'case_file'), 'customer')
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.logger.info("开始测试销售管理相关接口")
        cls.http_request = HTTPRequest2()
        cls.mysql = do_mysql.DoMysql()
        params = config.get('data', 'data')
        resp = cls.http_request.request('POST',
                                        '/user/login',
                                        'json',
                                        jsons=params)
        #将登陆的token传入header里面,后面接口调用
        cls.headers = {
            "content-type": "application/json",
            "Connection": "keep-alive",
            'token': resp.json()['data']['token']
        }

    @data(*cases)
    def test_customer(self, case):
        case.data = context.replace(case.data)
        logger.logger.info("开始测试:{0},发送的请求是:{1},请求类型:{2}".format(
            case.title, case.data, type(case.data)))
        # 随机生成用户名,并且反射到Context类的属性中,方便后面参数调用
        random_str = ''.join(random.sample(string.ascii_letters, 6))
        if case.data.find('customer_name') != -1:
            case.data = case.data.replace('customer_name', random_str.lower())
            setattr(context.Context, 'customer_name', random_str)
Esempio n. 3
0
class WxmallTest(unittest.TestCase):
    excel = do_excel.DoExcel(config.get('case', 'case_file'), 'wxMall')
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.logger.info("开始商城管理相关接口")
        cls.http_request = HTTPRequest2()
        cls.mysql = do_mysql.DoMysql()
        params = config.get('data', 'data')
        resp = cls.http_request.request('POST',
                                        '/user/login',
                                        'json',
                                        jsons=params)
        # 将登陆的token传入header里面,后面接口调用
        cls.headers = {
            "content-type": "application/json",
            "Connection": "keep-alive",
            'token': resp.json()['data']['token']
        }

    @data(*cases)
    def test_payment(self, case):
        case.data = context.replace(case.data)
        random_str = ''.join(random.sample(string.ascii_letters, 6))
        if case.data.find('proper_name') != -1:
            case.data = case.data.replace('proper_name', random_str.lower())
            setattr(context.Context, 'proper_name', random_str.lower())
Esempio n. 4
0
 def setUpClass(cls):
     logger.logger.info("开始测试采购管理相关接口")
     cls.http_request = HTTPRequest2()
     cls.mysql = do_mysql.DoMysql()
     params = config.get('data', 'data')
     resp = cls.http_request.request('POST', '/user/login', 'json', jsons=params)
     #将登陆的token传入header里面,后面接口调用
     cls.headers = {"content-type": "application/json", "Connection": "keep-alive", 'token': resp.json()['data']['token']}
Esempio n. 5
0
class SalseTest(unittest.TestCase):
    excel = do_excel.DoExcel(config.get('case', 'case_file'), 'salse')
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.logger.info("开始测试促销管理相关接口")
        cls.http_request = HTTPRequest2()
        cls.mysql = do_mysql.DoMysql()
        params = config.get('data', 'data')
        resp = cls.http_request.request('POST',
                                        '/user/login',
                                        'json',
                                        jsons=params)
        #将登陆的token传入header里面,后面接口调用
        cls.headers = {
            "content-type": "application/json",
            "Connection": "keep-alive",
            'token': resp.json()['data']['token']
        }

    @data(*cases)
    def test_salse(self, case):
        case.data = context.replace(case.data)
        resp = self.http_request.request(case.method,
                                         case.url,
                                         case.type,
                                         headers=self.headers,
                                         jsons=case.data)
        logger.logger.info("开始测试:{0},发送的请求是:{1},请求类型:{2}".format(
            case.title, case.data, type(case.data)))
        logger.logger.info("{0},返回是:{1},返回类型:{2}".format(
            case.title, resp.text, type(resp.text)))
        print("请求的内容:{}".format(case.title))
        if 'message' in json.loads(resp.text).keys():
            #判断返回值的类型,是否含有message提示
            try:
                self.assertEqual(str(case.expected),
                                 json.loads(resp.text)['message'])
                self.excel.write_result(case.case_id + 1,
                                        json.loads(resp.text)['message'],
                                        'PASS')
                logger.logger.info('{0}接口测试通过'.format(case.title))
                if case.case_id == 4:
                    #将新增活动的整个返回活动id传入Context中,为下一请求的传参
                    activity_id = json.loads(resp.text)['data']['id']
                    setattr(context.Context, 'activity_id', activity_id)
                if case.case_id == 9:
                    #将新增礼品中的返回单号传入Context中,为下一请求的传参单号
                    gift_id = json.loads(resp.text)['data']['id']
                    gift_Num = json.loads(resp.text)['data']['giftNum']
                    setattr(context.Context, 'gift_id', gift_id)
                    setattr(context.Context, 'gift_Num', gift_Num)
Esempio n. 6
0
class LeadingOutTest(unittest.TestCase):
    excel = do_excel.DoExcel(config.get('case', 'case_file'), 'leading_out')
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试导出接口')
        cls.http_request = HTTPRequest2()
        params = config.get('data', 'data')
        resp = cls.http_request.request('POST',
                                        '/user/login',
                                        'json',
                                        jsons=params)
        # 将登陆的token传入header里面,后面接口调用
        cls.headers = {
            "content-type": "application/json",
            "Connection": "keep-alive",
            'token': resp.json()['data']['token']
        }

    @data(*cases)
    def test_leading_out(self, case):
        logger.info('开始测试:{0}'.format(case.title))
        resp = self.http_request.request(case.method,
                                         case.url,
                                         case.type,
                                         headers=self.headers,
                                         jsons=case.data)
        print("请求的内容:{}".format(case.title))
        try:
            self.assertEqual(case.expected, json.loads(resp.text)['message'])
            self.excel.write_result(case.case_id + 1,
                                    json.loads(resp.text)['message'], 'PASS')
        except AssertionError as e:
            self.excel.write_result(case.case_id + 1,
                                    json.loads(resp.text)['message'], 'FAIL')
            logger.error("报错了,{0}".format(e))
            raise e
        logger.info('结束测试:{0}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('导出接口测试完毕')
        cls.http_request.close()
Esempio n. 7
0
class PaymentTest(unittest.TestCase):
    excel = do_excel.DoExcel(config.get('case', 'case_file'), 'payment')
    cases = excel.get_cases()
    @classmethod
    def setUpClass(cls):
        logger.logger.info("开始财务管理相关接口")
        cls.http_request = HTTPRequest2()
        cls.mysql = do_mysql.DoMysql()
        params = config.get('data', 'data')
        resp = cls.http_request.request('POST', '/user/login', 'json', jsons=params)
        #将登陆的token传入header里面,后面接口调用
        cls.headers = {"content-type": "application/json", "Connection": "keep-alive", 'token': resp.json()['data']['token']}
    @data(*cases)
    def test_payment(self, case):
        case.data=context.replace(case.data)
        #对取到的参数做解析,参数化,运用返回值传参
        logger.logger.info("开始测试:{0},发送的请求是:{1},请求类型:{2}".format(case.title, case.data, type(case.data)))
        resp = self.http_request.request(case.method, case.url, case.type, headers=self.headers,jsons=case.data)
        logger.logger.info("{0},返回是:{1},返回类型:{2}".format(case.title, resp.text, type(resp.text)))
        print("请求的内容:{}".format(case.title))
        if 'message' in json.loads(resp.text).keys():
            #判断返回值的类型,是否含有message提示
            try:
                self.assertEqual(str(case.expected), json.loads(resp.text)['message'])
                self.excel.write_result(case.case_id + 1, json.loads(resp.text)['message'], 'PASS')
                logger.logger.info('{0}接口测试通过'.format(case.title))
                if case.case_id in (2,5,16,20):
                    body_data = json.loads(resp.text)['data']['rows'][0]
                    setattr(context.Context, 'body_data', body_data)
                    taskId = json.loads(resp.text)['data']['rows'][0]["id"]
                    setattr(context.Context, 'taskId', taskId)
            except AssertionError as e:
                self.excel.write_result(case.case_id + 1, resp.text, 'FAIL')
                logger.logger.error("报错了,{0}".format(e))
                raise e
        else:
            try:
                self.assertIsInstance(json.loads(resp.text),dict)
                self.excel.write_result(case.case_id + 1, resp.text, 'PASS')
                logger.logger.info('{0}接口测试通过'.format(case.title))
            except AssertionError as e:
                self.excel.write_result(case.case_id + 1, resp.text, 'FAIL')
                logger.logger.error("报错了,{0}".format(e))
                raise e
Esempio n. 8
0
def replace(data):
    p = "#(.*?)#"  # 正则表达式
    while re.search(p, data):
        print(type(data))
        g = re.search(p,
                      data).group(1)  # 从任意位置开始找,找第一个就返回Match object, 如果没有找None
        # 拿到参数化的KEY
        try:
            v = config.get('data', g)  # 根据KEY取配置文件里面的值
        except configparser.NoOptionError as e:  # 如果配置文件里面没有的时候,去Context里面取
            if hasattr(Context, g):
                v = getattr(Context, g)
            else:
                print('找不到参数化的值')
                raise e
        print(v)
        # 记得替换后的内容,继续用data接收
        data = re.sub(p, str(v), data, count=1)  # 查找替换,count查找替换的次数

    return data
class SysUpdownstreamTest(unittest.TestCase):
    excel = do_excel.DoExcel(config.get('case', 'case_file'),
                             'sysUpdownstream')
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.logger.info("开始测试上下游管理相关接口")
        cls.http_request = HTTPRequest2()
        cls.mysql = do_mysql.DoMysql()
        params = config.get('data', 'data')
        resp = cls.http_request.request('POST',
                                        '/user/login',
                                        'json',
                                        jsons=params)
        #将登陆的token传入header里面,后面接口调用
        cls.headers = {
            "content-type": "application/json",
            "Connection": "keep-alive",
            'token': resp.json()['data']['token']
        }

    @data(*cases)
    def test_sysUpdownstream(self, case):
        case.data = context.replace(case.data)
        logger.logger.info("开始测试:{0},发送的请求是:{1},请求类型:{2}".format(
            case.title, case.data, type(case.data)))
        resp = self.http_request.request(case.method,
                                         case.url,
                                         case.type,
                                         headers=self.headers,
                                         jsons=case.data)
        logger.logger.info("{0},返回是:{1},返回类型:{2}".format(
            case.title, resp.text, type(resp.text)))
        print("请求的内容:{}".format(case.title))
        if 'message' in json.loads(resp.text).keys():
            #判断返回值的类型,是否含有message提示
            try:
                self.assertEqual(str(case.expected),
                                 json.loads(resp.text)['message'])
                self.excel.write_result(case.case_id + 1,
                                        json.loads(resp.text)['message'],
                                        'PASS')
                logger.logger.info('{0}接口测试通过'.format(case.title))
                if case.case_id in (1, 5):
                    #将新增颜色尺码传入Context中,为下一请求的传参单号
                    color_id = json.loads(resp.text)['data']['id']
                    color_code = json.loads(resp.text)['data']['code']
                    setattr(context.Context, 'color_id', color_id)
                    setattr(context.Context, 'color_code', color_code)
            except AssertionError as e:
                self.excel.write_result(case.case_id + 1, resp.text, 'FAIL')
                logger.logger.error("报错了,{0}".format(e))
                raise e
        else:
            try:
                self.assertIsInstance(json.loads(resp.text), dict)
                self.excel.write_result(case.case_id + 1, resp.text, 'PASS')
                logger.logger.info('{0}接口测试通过'.format(case.title))
            except AssertionError as e:
                self.excel.write_result(case.case_id + 1, resp.text, 'FAIL')
                logger.logger.error("报错了,{0}".format(e))
                raise e