class join(unittest.TestCase): header = {} header['Content-Type'] = read_config().get('header', 'type') header['Auth-Token'] = read_config().get('header', 'token') case_data = [{ "title": "社区搜索", "method": "post", "url": "https://test-api.wefunapp.cn/v1/servers/search", "data": { "server_name": "乐园社区" }, "expected": "0" }, { "title": "社区加入", "method": "post", "url": "https://test-api.wefunapp.cn/v1/servers/join", "data": { "server_id": getattr(Context, 'server_id') }, "expected": "0" }, { "title": "离开社区", "method": "post", "url": "https://test-api.wefunapp.cn/v1/servers/exit", "data": { "server_id": getattr(Context, 'server_id') }, "expected": "0" }] def setUp(self): warnings.simplefilter('ignore', ResourceWarning) @data(*case_data) 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)
class remarks(unittest.TestCase): header = {} header['Content-Type'] = read_config().get('header', 'type') header['Auth-Token'] = read_config().get('header', 'token') url = "https://test-api.wefunapp.cn/v1/friend/set-remark" data = {"friend_id": "", "remark": ""} def test_001(self): re = requests.post(url=url, data=json.dumps(data), headers=self.header)
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
class set(unittest.TestCase): header = {} header['Content-Type'] = read_config().get('header', 'type') header['Auth-Token'] = read_config().get('header', 'token') def setUp(self): warnings.simplefilter('ignore', ResourceWarning) # 每次调修改名字前,都会先去数据库,将此用户设置为 现在就"修改名字",不用等时间,因为wefun修改昵称需要等待60天间隔时间 sql = "update `t_vip` set update_nickname_time = '1577808000' where telephone = '18888888888'" self.mysql = MysqlUtil() self.mysql.fetch_one(sql) self.mysql.commit() # 测试数据 picture_list = [ "http://img2.woyaogexing.com/2020/08/26/39a147f5f6bc4e2c94470e4628f7fe5e!400x400.jpeg", "http://img2.woyaogexing.com/2020/08/24/21269c6f21054deebe64cf5dda71f14b!400x400.jpeg", "http://img2.woyaogexing.com/2020/08/03/5439185ffbcf47b6b7b2d43a4b0646d4!400x400.jpeg", "http://img2.woyaogexing.com/2020/08/02/f2c64c6d67334289abab21ab5208621e!400x400.jpeg", "http://img2.woyaogexing.com/2020/08/26/8076a025d99b484a8d5192a224a8d517!400x400.jpeg", "http://img2.woyaogexing.com/2020/08/09/d13d5fbbc2834801bd307149235a15f3!400x400.jpeg", "http://img2.woyaogexing.com/2020/08/21/8bdf8bd0e6704efd825cf56f96512354!400x400.jpeg", "http://img2.woyaogexing.com/2020/08/13/b4a5b74dc68f43bb8df35bee497c2aa5!400x400.jpeg", "http://img2.woyaogexing.com/2020/08/07/4548195c03f64752aaf7e5326b430830!400x400.jpeg", "http://img2.woyaogexing.com/2020/08/01/4e8ecc9a65c44d16aede57ea9389d831!400x400.jpeg" ] data = { "title": "设置资料", "method": "post", "url": "https://test-api.wefunapp.cn/v1/vip/update-profile", "data": { "nickname": Random().create_name(), "avatar": random.choice(picture_list), "sex": "1" } } 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"]) def tearDown(self): # 关闭数据库连接 self.mysql.close()
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
class hh(unittest.TestCase): global _id _id = None header = {} header['Content-Type'] = read_config().get('header', 'type') header['Auth-Token'] = read_config().get('header', 'token') def setUp(self): warnings.simplefilter('ignore', ResourceWarning) print( "-------------------------------------------------开始-------------------------------------------------" ) 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_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 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_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 tearDown(self): print( "-------------------------------------------------结束-------------------------------------------------" )
class send_message(unittest.TestCase): global channel_id channel_id = None # 请求头 header = {} header[ 'Content-Type'] = "application/x-www-form-urlencoded" # 这个创建社区频道/分类的接口,header中的type2 = application/x-www-form-urlencoded,只能用这个先处理下 header['Auth-Token'] = read_config().get('header', 'token') case_list = [{ "title": "发送文本内容消息", "method": "post", "url": "https://test-api.wefunapp.cn/v1/channels/message-post", "data": { "channel_id": None, "content": "测试频道消息正常发布" }, "expected": "0" }, { "title": "发送表情内容消息", "method": "post", "url": "https://test-api.wefunapp.cn/v1/channels/message-post", "data": { "channel_id": None, "content": "[斜眼笑]" }, "expected": "0" }, { "title": "发送图片内容消息", "method": "post", "url": "https://test-api.wefunapp.cn/v1/channels/message-post", "data": { "channel_id": None, "content": "http://g.hiphotos.baidu.com/zhidao/pic/item/c83d70cf3bc79f3d6e7bf85db8a1cd11738b29c0.jpg" }, "expected": "0" }] @classmethod 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 # 2-->测试频道中发布各种消息 @data(*case_list) 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) # 3-->删除这个频道(包括发布的消息) @classmethod 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
class new_code(unittest.TestCase): # 请求头 header = {} header['Content-Type'] = read_config().get('header', 'type') header['Auth-Token'] = read_config().get('header', 'token') # 请求数据 case = [{ "title": "生成社区邀请码", "method": "post", "url": "https://test-api.wefunapp.cn/v1/servers/invite-code", "data": { "server_id": 1249029898480123904 }, "expected": "0" }] case_2 = [{ "title": "根据邀请码加入", "method": "post", "url": "https://test-api.wefunapp.cn/v1/servers/join-with-invite", "data": { "invite_code": "" }, "expected": "0" }] def setUp(self): warnings.simplefilter("ignore", ResourceWarning) # 生成社区邀请码 @data(*case) 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 # 根据社区邀请码加入社区 @data(*case_2) 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 # 数据清除,通过邀请码,加入社区成功后,做一个离开社区的操作 @classmethod 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