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())
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)
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)
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()
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
class LoginTest(unittest.TestCase): excel = do_excel.DoExcel(config.config.get('case', '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('开始测试:{0}'.format(case.title)) resp = self.http_request.request(case.method, case.url, case.type, 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()
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