def test_01(self, case): data = json.loads(case.data) header = json.loads(ConfigLoader().get("header", "quanzi_value")) # 请求参数补全 if data['uuid'] == '': # 如果case中读取出来的uuid为空,就从数据库中查出最新的uuid赋上去 data['uuid'] = alter_ago['uuid'] else: data['uuid'] = data['uuid'] data['background_img'] = ConfigLoader().get('picture', 'img_url_one') data['images'] = ConfigLoader().get('picture', 'img_url_two') try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求报错: %s" % e) raise e result = res.get_json() print("请求结果: %s" % result) self.assertEqual(case.expected, result['success']) # 接口断言 sql_2 = "select * from message.message_circle_vote_questions where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' and activity_type = 1 order by id desc limit 1;" if result['success'] == 'True' and result['message'] == '成功': result_img = mysql.fetch_one(sql_2)['images'] self.assertNotEqual(alter_ago['images'], result_img) # 修改前的图片,跟调接口修改后的接口,不等则PASS
def test_001(self, case): # 从excel配置文件读出来的数据处理 data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) # 发起接口请求 print(("用例id:{0},用例标题:{1}".format(case.case_id, case.title))) try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求时报错:%s" % e) raise e print("请求结果:{}".format(res.get_json())) # 接口断言 result = res.get_json() if res.get_status_code() == 200: self.assertEqual(result['message'], case.expected) # 数据库校验 if result['success'] == True or result['message'] == '成功': if data['type'] == '1': #如果用例中,type标识获取圈子详情下,刷屏 self.assertEqual( result['data']['counts'], len(spam_type)) # 接口响应counts响应数据条数,与sql查询的结果条数一致,PASS elif data['type'] == 2: pass
def test_01(self, case): # 接口需要的参数处理 data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) # 发起接口请求 print(("用例id:{0},用例标题:{1}".format(case.case_id, case.title))) try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求时报错:%s" % e) raise e print("请求结果:{}".format(res.get_json())) # 接口断言 result = res.get_json() self.assertEqual(result['success'], case.expected) if result['success'] == True and result['message'] == '成功': # 接口请求成功后,查出"开心就好"在第四个圈子的状态 mysql_expected = mysql.fetch_one( "select * from message.message_circle_link where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' and message_uuid = '9db154e2be2e450488bb8dd14c767f4b';" ) # 第一条用例调用后status是1;第二条用例调用后然后变成0 self.assertNotEqual( article_status, mysql_expected['status']) # 接口调用前的状态,跟接口调用后的状态,bu一致PASS
def test_01(self, case): # excel和config 读出来的数据,转换成dict data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) # 接口参数传递 data['uuid'] = newest_id['uuid'] # 这里避免出错,把uuid的值,直接从数据库中取 if newest_id['status'] == 2: #这里检查下 数据库中最新问答或圈子的状态,因为新增的问答投票,默认是禁用状态 data['status'] = '1' # 取出来的状态是禁用,那么调用接口就传 1,启用 elif newest_id['status'] == 1: # 这里检查下 数据库中问答投票的状态 data['status'] = '2' # 取出来的状态是启用,那么调用接口就传 2,禁用 # 发起忌口请求 try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求时报错:%s" % e) raise e result = res.get_json() print("请求结果:%s" % result) # 断言(接口断言/数据库校验) self.assertEqual(case.expected, result['success']) if result['success'] == 'True' and result['message'] == '成功': sql_2 = "select * from message.message_circle_vote_questions where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' order by id desc limit 1;" self.assertNotEqual(newest_id['status'], mysql.fetch_one(sql_2)['status'])
def test_01(self, case): # 必要数据的处理 header = json.loads(ConfigLoader().get('header', 'header_value')) data = json.loads(case.data) # 接口参数的值随机选 data['title'] = data['title'] + str(random.randint( 0, 550)) # 圈子名字拼接,降低名字重复的几率 data['base_join_num'] = str(random.randint(100, 1500)) # 圈子加入人数初始值,取个随机数 print(("用例id:{0},用例标题:{1}".format(case.case_id, case.title))) try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求时报错:%s" % e) raise e # 断言 result = res.get_json() print("请求结果:%s" % result) if result['success'] == 'True': #如果接口返回True # 接口成功,对应的数据库圈子表中id要+1 expected = max_uuid + 1 # 期望结果 self.assertEqual( expected, mysql.fetch_one( "select * from message.message_circle where id != '' order by id desc limit 1; " )['id'])
def test_01(self, case): header = json.loads(ConfigLoader().get("header", "quanzi_value")) data = json.loads(case.data) # 参数补全 if data['circle_uuid'] == '': # 圈子加入领域-正常加入,参数补齐 data['circle_uuid'] = enabled_circle['uuid'] data['field_uuid'] = enabled_domain['uuid'] # 发起请求 try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("接口请求报错:%s" % e) raise e # 断言(接口断言/数据库校验) result = res.get_json() self.assertEqual(case.expected, result['success']) if result['message'] == '成功': # 圈子成功加入领域,那么圈子领域circle_field_link 表中数据会增加 self.assertEqual( max_circle_field_link + 1, mysql.fetch_one( "select * from message.message_circle_field_link where id != '' order by id desc;" )['id'])
def test_01(self, case): # 从excel配置文件读出来的数据处理 data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) # 发起接口请求 print(("用例id:{0},用例标题:{1}".format(case.case_id, case.title))) try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求时报错:%s" % e) raise e print("请求结果:{}".format(res.get_json())) # 接口请求的断言 result = res.get_json() self.assertEqual(result['success'], case.expected) # 接口请求结果,与数据库中结果校验 if result['success'] and result['message']: if data['circle_uuid'] == '555c54bb67a546569969e8c5d8d0c538': # 如果用例中uuid = 555c54bb67a546569969e8c5d8d0c538 self.assertEqual(result['data']['counts'], len(four_max)) # 比对接口获取的值,跟数据库中的条数,一致则PASS elif data['circle_uuid'] == 'd41b03780174469ba858a3db5ae0f05d': self.assertEqual(result['data']['counts'], len(three_max)) else: pass
def test_01(self,case): # 参数json化 header = json.loads(ConfigLoader().get("header","header_value")) data = json.loads(case.data) # 参数补全,因为excel中为空 ,防止id写死就需要最新的field_uuid if data['field_uuid'] == '': data['field_uuid'] = newest['uuid'] else: data['field_uuid'] = data['field_uuid'] # 发起请求 try: res = Http_Request(case.method,case.url,data,headers=header) except Exception as e : print("请求报错:%s"%e) raise e result = res.get_json() print("请求结果:%s"%result) # 修改领域状态,用例参数补全 data['status'] = '2' # 断言 self.assertEqual(case.expected,result['success']) if result['success'] == 'True' and case.url =="/message/circle/sys/updMessageCircleFieldStatus": # 如果测试跑的是,修改领域状态,则接口请求成功,做数据库校验 self.assertNotEqual(newest['status'],mysql.fetch_one("SELECT * FROM message.`message_circle_field` where id != '' order by id desc limit 1;")['status'])
def test_001(self, case): # 从excel配置文件读出来的数据处理 data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) # 发起接口请求 print(("用例id:{0},用例标题:{1}".format(case.case_id, case.title))) try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求时报错:%s" % e) raise e print("请求结果:{}".format(res.get_json())) # 接口断言 result = res.get_json() self.assertEqual(result['success'], case.expected) if result['success'] == True and result['message'] == '成功': # 接口请求成功后,查出"开心就好"在第四个圈子的状态 mysql_expected = mysql.fetch_one( "select * from message.message_circle_link_account where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' and account_uuid = 'eab0303b2db04247add02ad2f0d13682'" ) # 第一条用例调用后status是1;第二条用例调用后然后变成0 self.assertNotEqual( join_status, mysql_expected['status']) # 接口调用前的状态,跟接口调用后的状态,bu一致PASS
def __init__(self, method, url, data=None, cookies=None, headers=None): config = ConfigLoader().get('api', 'test_api') url = config + url if method == 'get': self.resp = requests.get(url=url, params=data, cookies=cookies, headers=headers) # get请求的方式 以params定义参数名 elif method == 'post': self.resp = requests.post(url=url, data=data, cookies=cookies, headers=headers) elif method == 'delete': self.resp = requests.delete(url=url, data=data, cookies=cookies, headers=headers) elif method == 'put': self.resp = requests.put(url=url, params=data, cookies=cookies, headers=headers)
def test_001(self, case): # 接口需要的参数处理 data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) data['base_join_num'] = str(random.randint(25, 85)) # 修改圈子加入基数,随机生成 if data['uuid'] == '': # 如果用例中,uuid为 '' 控制用那条用例来请求接口 data['uuid'] = max_uuid['uuid'] # 就从数据库中查出,最新圈子uuid作为修改圈子的uuid data['title'] = max_uuid['title'] # 发起请求 res = Http_Request(case.method, case.url, data, headers=header) result = res.get_json() self.assertEqual(result['data'], case.expected) # 修改成功,要做数据库校验 if result['data'] == '1' and result[ 'message'] == '成功': #如果接口请求成功,说明图片修改成功 alter_image = mysql.fetch_one( "select * from message.message_circle where id != '' order by id desc limit 1; " ) self.assertNotEqual(max_uuid['image'], alter_image) #执行请求前的图,跟请求后的图,不相等PASS else: data['uuid'] = data['uuid'] res = Http_Request(case.method, case.url, data, headers=header) result = res.get_json() self.assertNotEqual(result['status'], case.expected)
def test_01(self, case): # excel 配置文件读出来的数据,做处理 data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) print(("用例id:{0},用例标题:{1}".format(case.case_id, case.title))) res = Http_Request(case.method, case.url, data, headers=header) result = res.get_json() print("请求结果:%s" % result) self.assertEqual(case.expected, result['success'])
def test_001(self, case): # 参数json化 请求的参数 和请求的头 都必须是 json格式 header = json.loads(ConfigLoader().get("header", "quanzi_value")) data = json.loads(case.data) try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求报错:%s" % e) raise e result = res.get_json() # print(result) self.assertEqual(case.expected, result['success']) if data['keyword'] == None and result['message'] == '成功': self.assertEqual(len(max_circle), result['data']['counts'])
def test_01(self,case): # 接口需要的参数处理 data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) # 发起接口请求 print(("用例id:{0},用例标题:{1}".format(case.case_id, case.title))) try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求时报错:%s" % e) raise e print("请求结果:{}".format(res.get_json())) # 接口断言 result = res.get_json() self.assertEqual(result['success'], case.expected)
def test_01(self, case): data = json.loads(case.data) header = json.loads(ConfigLoader().get("header", "quanzi_value")) # 用例中包含"问答"和"投票"的用例,实际调用时uuid 参数要区分下 if data['uuid'] == '1': # 用例中uuid 写的1,表示用例为"投票的用例" data['uuid'] = newest_vote['uuid'] # 就把最新投票的uuid给到接口参数需要的uuid elif data['uuid'] == '2': # 用例中uuid 写的2,表示用例为"问答的用例" data['uuid'] = newest_answers['uuid'] # 就把最新问答的uuid给到接口参数需要的uuid else: data['uuid'] = data['uuid'] # 调用接口 try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求时报错: %s" % e) raise e result = res.get_json() self.assertEqual(case.expected, result['success']) # 接口断言 # 分别对投票/问答,是否加入刷屏,做接口请求后的数据库校验 if result['success'] == 'True' and data['uuid'] == newest_vote[ 'uuid']: # 投票 # 查出接口调用后,加入刷屏的状态,作为实际结果 vote_expected = mysql.fetch_one( "select * from message.message_circle_vote_questions " "where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' " "and activity_type = 1 " "and status = 1 order by id desc limit 1;")['is_join_sp'] # 调用接口前,与调用接口后,状态不等PASS self.assertNotEqual(newest_vote['is_join_sp'], vote_expected) elif result['success'] == 'True' and data['uuid'] == newest_answers[ 'uuid']: # 问答 # 查出接口调用后,加入刷屏的状态,作为实际结果 ans_expected = mysql.fetch_one( "select * from message.message_circle_vote_questions " "where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' " "and activity_type = 2 " "and status = 1 order by id desc limit 1;")['is_join_sp'] # 调用接口前,与调用接口后,状态不等PASS self.assertNotEqual(newest_answers['is_join_sp'], ans_expected)
def test_001(self,case): header = json.loads(ConfigLoader().get('header', 'header_value')) data = json.loads(case.data) data['title'] = data['title'] + str(random.randint(1,521)) # 从用例中取出来名字,做个拼接 try: res = Http_Request(case.method,case.url,data,headers=header) except Exception as e: raise e result = res.get_json() self.assertEqual(case.expected,result['success']) if result['success'] == 'True': expected = max_id + 1 # 请求接口成功后,id 会+1 self.assertEqual(expected,mysql.fetch_one("select * from message.message_circle_vote_questions where id != '' order by id desc limit 1;"))
def test_01(self, case): header = json.loads(ConfigLoader().get("header", "header_value")) data = json.loads(case.data) if data['field_uuid'] == '': data['field_uuid'] = domain['uuid'] data['fieldName'] = PhoneNOGenerator().stochastict_name() res = Http_Request(case.method, case.url, data, headers=header) result = res.get_json() self.assertEqual(case.expected, result['success']) if result['success'] == 'True': self.assertNotEqual( domain, mysql.fetch_one( "SELECT * FROM message.`message_circle_field` where id != '' order by id desc limit 1;" ))
def test_01(self,case): # 必要数据的处理 header = json.loads(ConfigLoader().get('header', 'header_value')) # 从配置文件读取请求头 data = json.loads(case.data) # excel中读取出来的字典,最开始是str类型,请求接口时需要dict,所以要吧str转成dict # 发起接口请求 try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("接口请求时报错:%s"%e) raise e # 对接口请求进行断言 result = res.get_json() print("请求结果%s"%result) self.assertEqual(case.expected,result['success']) # 期望结果=excel中的True,实际结果=请求结果中success的值;期望结果 = 实际结果,接口断言通过 # 数据库断言(接口查询出来有个counts) if result['success'] and result['message']: self.assertEqual(len(vote_max),result['data']['counts']) # 数据库中查出的数据条数 = 接口响应的条数,PASS
def test_01(self, case): header = json.loads(ConfigLoader().get("header", "quanzi_value")) # 请求头 data = json.loads( case.data) # excel 中读取的字典,是str类型,需要json化,因为接口只接收json格式参数 try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求报错:%s" % e) raise e result = res.get_json() self.assertEqual(case.expected, result['success']) print(result['data']['counts']) if result[ 'message'] == '成功' and case.url == '/message/circle/sys/queryMessageCircleField': self.assertEqual( result['data']['counts'], len(max_counts)) #获取领域列表的接口,返回的值counts与数据库中查出的数据条数一致,PASS
def __init__(self): config = ConfigLoader() host = config.get('mysql', 'host') port = config.getint('mysql', 'port') # port 是一个int数据 user = config.get('mysql', 'usr') password = config.get('mysql', 'pwd') try: self.mysql = pymysql.connect(host=host, user=user, password=password, database=None, port=port, cursorclass=pymysql.cursors.DictCursor) except Exception as e : print("数据库连接错误:{}".format(e)) raise e
def test_01(self, case): data = json.loads(case.data) header = json.loads(ConfigLoader().get("header", "quanzi_value")) # 此接口以type判断是问答还是投票,要做用例参数分开传递 if data['type'] == '1': # 用例中type是1表问答,vote_questions_uuid需要传入问答的uuid sql_wenda = "select * from message.message_circle_vote_questions " \ "where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' " \ "and activity_type = 2 " \ "and status = 1 order by id desc limit 1;" data['vote_questions_uuid'] = mysql.fetch_one(sql_wenda)['uuid'] elif data[ 'type'] == '2': # 用例中type是2表投票,vote_questions_uuid需要传入投票的uuid sql_vote = "select * from message.message_circle_vote_questions " \ "where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' " \ "and activity_type = 1 " \ "and status = 1 order by id desc limit 1;" data['vote_questions_uuid'] = mysql.fetch_one(sql_vote)['uuid'] # 调接口 try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求接口报错:%s" % e) raise e result = res.get_json() print("请求结果:%s" % result) self.assertEqual(case.expected, result['success']) if result['success'] == 'True' and result['message'] == '成功': new_id = mysql.fetch_one( "select * from message.message_circle_vote_questions_link where id != '' order by id desc limit 1;" ) self.assertEqual(max_id['id'] + 1, new_id['id'])
class quanzi_issue(unittest.TestCase): quanzi_case = DoExcel(QuanZi_case_dir).get_case('模拟用户发布内容到圈子') def setUp(self): global mysql mysql = MysqlUtil() global max_id sql = "select * from message.message_circle_link where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' order by id desc limit 1;" max_id = mysql.fetch_one(sql)['id'] # 事先查出"第四个圈子"下最新的发布id,因为后面调用发布接口,成功后数据库id会加1 @data(*quanzi_case) def test_01(self,case): # 接口需要的参数处理 data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) print(("用例id:{0},用例标题:{1}".format(case.case_id, case.title))) # 发起接口请求 try: res = Http_Request(case.method, case.url, data, headers=header) print(res.get_url()) except Exception as e : print("请求报错%s"%e) raise e print("请求结果:{}".format(res.get_json())) # 接口断言 result = res.get_json() self.assertEqual(result['success'], case.expected) # 调用发布接口成功,数据库校验 if result['success'] == True and result['message'] == '成功': expected_id = mysql.fetch_one("select * from message.message_circle_link where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' order by id desc limit 1;") self.assertEqual(max_id+1,expected_id['id'])
def test_01(self, case): data = json.loads(case.data) header = json.loads(ConfigLoader().get('header', 'header_value')) if data['title'] != None: # 对测试用例中的问答名字,做一个拼接处理 data['title'] = data['title'] + str(random.randint(1, 876)) else: data['title'] = data['title'] # 如果测试用例中,title参数值为None 就保持不变 try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求时报错:%s" % e) raise e # 断言 result = res.get_json() print("请求结果:%s" % result) self.assertEqual(case.expected, result['success']) # 如果接口请求成功,数据库中数据产生变化,做数据库校验 if result['message'] == '成功': expected = max_id + 1 actual_id = mysql.fetch_one( "select * from message.message_circle_vote_questions where id != '' order by id desc limit 1;" )['id'] self.assertEqual(expected, actual_id) # 如果接口请求false,数据库中数据不变化,做数据库校验 else: self.assertEqual( max_id, mysql.fetch_one( "select * from message.message_circle_vote_questions where id != '' order by id desc limit 1;" )['id'])
def test_01(self, case): data = json.loads(case.data) header = json.loads(ConfigLoader().get( 'header', 'quanzi_value')) # 这里取出来的token,是接口文档给得 # 发起接口请求 print(("用例id:{0},用例标题:{1}".format(case.case_id, case.title))) try: res = Http_Request(case.method, case.url, data, headers=header) except Exception as e: print("请求接口报错:%s" % e) raise e print("请求结果:{}".format(res.get_json())) # 接口断言 result = res.get_json() self.assertEqual(result['success'], case.expected) if result['success'] == True and result['message'] == '成功': mysql_expected = mysql.fetch_one( "select * from message.message_circle_link_account " "where circle_uuid = '555c54bb67a546569969e8c5d8d0c538' " "and account_uuid = '7cd855be8df149bc86d816add22ec3d3' ;") self.assertNotEqual(super_status, mysql_expected) # 不相等则PASS