def test_002(self): header = {} header[ 'Content-Type'] = "application/x-www-form-urlencoded" # 创建社区下面的 分类 频道 需要用到另一个Content-Type header['Auth-Token'] = read_config().get('header', 'token') case = { "title": "创建分类", "method": "post", "url": "https://test-api.wefunapp.cn/v1/channels/create", #---->创建分类 "data": { "server_id": getattr(Context, 'new_sercer'), "channel_type": "1", "channel_name": "test" }, "expected": "0" } try: re = Request(case['method'], case['url'], json.dumps(case["data"]), header=header) result = re.get_json() if result["code"] == 0: print("创建社区中的分类-->成功 %s" % result) else: print("创建社区中的分类-->失败 %s" % result) # 失败的写到日志里面去 My_Logger().info("创建社区中的分类-->失败 %s" % result) self.assertEqual(int(case['expected']), result['code']) except Exception as e: My_Logger().error("错误接口: %s%s" % case['title'] % e) raise e
def test_001(self): url = "https://test-api.wefunapp.cn/v1/friend/index" data = {"online_status": "", "page": "", "row": ""} try: re = requests.post(url=url, data=data, headers=self.header) My_Logger().info("获取好友:%s" % re.json()) except Exception as e: My_Logger().error("报错:获取好友接口-> %s" % e) raise e
def test_003(self): url = "https://test-api.wefunapp.cn/v1/friend/update-crowd-info" data = { "channel_id": getattr(Context, 'id'), "channel_name": "s", "icon": "4e8ecc9a65c44d16aede57ea9389d831.jpeg" } try: re = requests.post(url=url, data=json.dumps(data), headers=self.header) My_Logger().info("修改群资料:%s" % re.json()) except Exception as e: My_Logger().error("错误接口:修改群资料--> %s" % e) raise e
def test_002(self, case): try: re = Request(method=case["method"], url=case['url'], data=json.dumps(case['data']), header=self.header) result = re.get_json() if result['code'] == 0: print("根据邀请码加入社区-->成功 %s" % result) else: print("根据邀请码加入社区-->失败 %s" % result) My_Logger().info("根据邀请码加入社区-->失败 %s" % result) except Exception as e: My_Logger().error("错误接口:根据邀请码加入社区-->%s" % e) raise e
def test_004(self): url = "https://test-api.wefunapp.cn/v1/friend/delete-member" data = { "server_id": getattr(Context, 'id'), "member_id": [ "1212124358370131968", "1190659892201914368", "1186674854070321152" ] } try: re = requests.post(url=url, data=json.dumps(data), headers=self.header) My_Logger().info("踢出群聊:%s" % re.json()) except Exception as e: My_Logger().error("错误接口:踢出群聊--> %s" % e) raise e
def test_001(self, case): try: re = Request(method=case["method"], url=case['url'], data=json.dumps(case['data']), header=self.header) result = re.get_json() if result['code'] == 0: print("生成社区邀请码-->成功 %s" % result) else: print("生成社区邀请码-->失败 %s" % result) # 失败的写道日志里去 My_Logger().info("生成社区邀请码-->失败 %s" % result) # 生成的邀请码存起来 if re.get_json()['code'] == 0: self.case_2[0]["data"]["invite_code"] = re.get_json( )['invite_code'] except Exception as e: My_Logger().error("错误接口:生成社区邀请码-->%s" % e) raise e
def test_002(self): url = "https://test-api.wefunapp.cn/v1/friend/create-crowd" data = { "members": [ "1212124358370131968", "1190659892201914368", "1186674854070321152" ] } try: re = requests.post(url=url, data=json.dumps(data), headers=self.header) result = re.json() # 如果创建成功,存入id以供后面接口使用 if result['server_id']: self._id = result['server_id'] setattr(Context, 'id', result['server_id']) My_Logger().info("创建群聊:%s" % re.json()) except Exception as e: My_Logger().error("错误接口:创建群聊--> %s" % e) raise e
def setUpClass(cls): warnings.simplefilter("ignore", ResourceWarning) # 去掉ResourceWarning # 1-->新增文本频道 data = { "title": "新增文本频道", "method": "post", "url": "https://test-api.wefunapp.cn/v1/channels/create", "data": { "server_id": "1277254596447698944", "channel_name": Random().create_name(), "channel_desc": "新增文本频道", "channel_type": "1", "parent_id": "", "channel_pwd": "" } } try: re = Request(method=data['method'], url=data['url'], data=data['data'], header=cls.header) except Exception as e: My_Logger().error("错误接口: %s%s" % data['title'] % e) raise e result = re.get_json() if result['code'] == 0: print(data['title'] + "-->成功 %s" % result) else: print(data['title'] + "-->失败 %s" % result) My_Logger().info(data['title'] + "-->失败 %s" % result) # 处理数据依赖 if result['code'] == 0: cls.channel_id = result['channel_id'] else: pass
def test_serch(self, case): try: re = Request(case['method'], case['url'], json.dumps(case['data']), header=self.header) result = re.get_json() print(case['title'], result) if result['code'] == 0: print(case['title'] + "-->成功 %s" % result) else: print(case['title'] + "-->失败 %s" % result) # 失败的写到日志去 My_Logger().info(case['title'] + "-->失败 %s" % result) except Exception as e: My_Logger().error("错误接口: %s%s" % case['title'] % e) raise e # 断言 try: self.assertEqual(int(case['expected']), result['code']) except Exception as e: My_Logger().error(case['title'] + "断言失败: %s" % e)
def test_001(self, case): if case['data']['channel_id'] == None: case['data']['channel_id'] = self.channel_id try: re = Request(method=case['method'], url=case['url'], data=case['data'], header=self.header) except Exception as e: My_Logger().error("错误接口: %s%s" % case['title'] % e) raise e # 接口结果写入报告 result = re.get_json() if result['code'] == 0: print(case['title'] + "-->成功 %s" % result) else: print(case['title'] + "-->失败 %s" % result) My_Logger().info(case['title'] + "-->失败 %s" % result) # 接口断言 try: self.assertEqual(int(case['expected']), result['code']) except AssertionError as e: My_Logger().info(case['title'] + "断言错误: %s" % e)
def test_001(self, case): try: res = Request(method=case["method"], url=case['url'], data=json.dumps(case['data']), header=self.header) result = res.get_json() if result['code'] == 0: print("创建社区-->成功 %s" % result) else: print("创建社区-->失败") # 如果创建社区成功,就把创建成功得社区id存起来 if result['server_id']: setattr(Context, 'new_sercer', result['server_id']) except Exception as e: My_Logger().error("错误接口: %s%s" % case['title'] % e) raise e
def tearDownClass(cls): cls.header data = { "case_id": "离开社区", "method": "post", "url": "https://test-api.wefunapp.cn/v1/servers/exit", "data": { "server_id": 1249029898480123904 }, "expected": "0" } try: re = Request(method=data["method"], url=data['url'], data=json.dumps(data['data']), header=cls.header) except Exception as e: My_Logger().error("错误接口:离开社区-->%s" % e) raise e
def tearDownClass(cls): data = { "case_title": "删除新增得频道", "method": "post", "url": "https://test-api.wefunapp.cn/v1/channels/delete", "data": { "channel_id": cls.channel_id }, "expected": "0" } try: res = Request( method=data['method'], url=data['url'], data=data['data'], header=cls.header, ) print('删除频道接口: %s' % res.get_json()) except Exception as e: My_Logger().error("删除失败,失败原因: %s" % e) raise e
def test_001(self): try: re = Request(method=self.data["method"], url=self.data['url'], data=json.dumps(self.data['data']), header=self.header) result = re.get_json() print("设置资料: %s" % result) except Exception as e: My_Logger().error("错误接口: %s%s" % self.data['title'] % e) raise e # 断言--->如果传入的名字/图片,跟接口调用之后,数据库中的名字一致,测试PASS if result['code'] == 0: self.assertEqual( data['data']['nickname'], self.mysql.fetch_one( "select * from `t_vip` where telephone = '18888888888'" )[0]["nickname"]) self.assertEqual( data['data']['avatar'], self.mysql.fetch_one( "select * from `t_vip` where telephone = '18888888888'" )[0]["avatar"])
class new(unittest.TestCase): global server_id server_id = None new_case = [{ "title": "社区创建", "method": "post", "url": "https://test-api.wefunapp.cn/v1/servers/create", "data": { "server_name": "测试", "server_icon": "" }, "expected": "0" }] #-------->新增社区得测试用例 header = {} header['Content-Type'] = read_config().get('header', 'type') header['Auth-Token'] = read_config().get('header', 'token') # header['Cookie'] = open(r'D:\WeFun Smoke\configs\cookie.txt','r+').read() def setUp(self): # 去掉ResourceWarning warnings.simplefilter("ignore", ResourceWarning) # 新增社区 @data(*new_case) def test_001(self, case): try: res = Request(method=case["method"], url=case['url'], data=json.dumps(case['data']), header=self.header) result = res.get_json() if result['code'] == 0: print("创建社区-->成功 %s" % result) else: print("创建社区-->失败") # 如果创建社区成功,就把创建成功得社区id存起来 if result['server_id']: setattr(Context, 'new_sercer', result['server_id']) except Exception as e: My_Logger().error("错误接口: %s%s" % case['title'] % e) raise e # 社区下面创建分类 def test_002(self): header = {} header[ 'Content-Type'] = "application/x-www-form-urlencoded" # 创建社区下面的 分类 频道 需要用到另一个Content-Type header['Auth-Token'] = read_config().get('header', 'token') case = { "title": "创建分类", "method": "post", "url": "https://test-api.wefunapp.cn/v1/channels/create", #---->创建分类 "data": { "server_id": getattr(Context, 'new_sercer'), "channel_type": "1", "channel_name": "test" }, "expected": "0" } try: re = Request(case['method'], case['url'], json.dumps(case["data"]), header=header) result = re.get_json() if result["code"] == 0: print("创建社区中的分类-->成功 %s" % result) else: print("创建社区中的分类-->失败 %s" % result) # 失败的写到日志里面去 My_Logger().info("创建社区中的分类-->失败 %s" % result) self.assertEqual(int(case['expected']), result['code']) except Exception as e: My_Logger().error("错误接口: %s%s" % case['title'] % e) raise e # 删除社区测试 def test_003(self): case = { "case_id": "2", "method": "post", "url": "https://test-api.wefunapp.cn/v1/servers/delete", #---->删除社区用例 "data": { "server_id": getattr(Context, 'new_sercer') }, "expected": "success" } try: re = Request(method=case["method"], url=case['url'], data=json.dumps(case['data']), header=self.header) result = re.get_json() if result['code'] == 0: print("删除社区-->成功 %s" % result) else: print("删除社区-->失败 %s" % result) except Exception as e: My_Logger().error("错误接口:删除社区 -->%s" % e) raise e