def test_login(): # 3、定义测试数据 conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/authorizations/" # url = "http://211.103.136.242:8064/authorizations/" data = {"username": "******", "password": "******"} # 4、发送POST请求 # r = requests.post(url,json=data) # r = requests_post(url,json=data) request = Request() r = request.post(url, json=data) # 5、输出结果 # print(r.json()) print(r) # 返回状态码 code = r['code'] AssertUtil().assert_code(code, 200) body = r['body'] # print(body) AssertUtil().assert_in_body(body, '"username": "******"') # 1、初始化数据库对象 conn = init_db('db_1') # 2、查询结果 res_db = conn.fetchone("select id from tb_users where username='******'") print("数据库查询结果:", res_db) # 3、验证 user_id = body['user_id'] assert user_id == res_db['id']
def test_create_dictionaries(uri, headers, ID, testcases, dictionaries_name, dictionaries_code, parentDictionary, status_code, expected_result): parentDictionaryKey = get_dictionaries_key(parentDictionary) parentDictionaryCode = get_dictionaries_code(parentDictionary) create_dictionaries_param = { 'name': dictionaries_name, 'code': dictionaries_code, 'properties': { 'reverseName': "" }, 'parentDictionaryKey': parentDictionaryKey, 'parentDictionaryCode': parentDictionaryCode } create_dictionaries_response = requests.post( url=uri + create_dictionaries_url, headers=headers, json=create_dictionaries_param).json() code = create_dictionaries_response['status'] allure.attach("请求响应code", str(create_dictionaries_response['status'])) allure.attach("请求响应结果", str(create_dictionaries_response)) my_log().info(create_dictionaries_response) AssertUtil().assert_code(create_dictionaries_response['status'], status_code) AssertUtil().assert_in_body(create_dictionaries_response['data'], expected_result)
def test_login(): # 3. 定义测试数据 url = ConfigYaml().get_conf_url() + "/authorizations/" # url = "http://211.103.136.242:8064/authorizations/" json_data = {"username": "******", "password": "******"} # 4. 发送post请求 # r = requests.post(url, json=json_data) # 调用封装的post方法 # res = requests_post(url, json=json_data) # 调用Requests重构的post方法 request = Request() res = request.post(url, json=json_data) # 5. 输出结果 # print(r.json()) print(res) # 验证 # 返回状态码 code = res["code"] # assert code == 201 AssertUtil().assert_code(code, 200) # 返回结果内容 # body = json.dumps(res["body"]) body = res["body"] # assert '"user_id": 1, "username": "******"' in body AssertUtil().assert_in_body(body, '"user_id": 1, "username": "******"') # 1.初始化数据库对象 conn = init_db("db_1") # 2.查询结果 res_db = conn.fetchone( "select id, username from tb_users where username = '******'") print("数据库查询结果", res_db) # 3. 验证结果 user_id = body["user_id"] assert user_id == res_db["id"]
def test_category_relationship(uri, headers, ID, testcases, categoryfrom, relationship, categoryto,status_code, expected_result): """ 创建配置类型间关系 :param uri: :param headers: :param ID: :param testcases: :param categoryfrom:源配置项类型 :param relationship:关系 :param categoryto:目的配置项类型 :return: """ category_from = get_categorykey(categoryfrom) category_to = get_categorykey(categoryto) relationshipkey = get_dictionary(relationship) create_categoryRelationship_param = { "_to": category_to, "_from": category_from, "dictionaryItemKey": relationshipkey._key } create_categoryRelationship_response = requests.post(url = uri + category_relationship_url, headers = headers, json= create_categoryRelationship_param).json() allure.attach("请求响应code", str(create_categoryRelationship_response['status'])) allure.attach("请求响应结果", str(create_categoryRelationship_response)) my_log().info(create_categoryRelationship_response) AssertUtil().assert_code(create_categoryRelationship_response['status'], status_code) AssertUtil().assert_in_body(create_categoryRelationship_response['data'], expected_result)
def test_login(): #3、定义测试数据 conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/authorizations/" #url = "http://211.103.136.242:8064/authorizations/" data = {"username": "******", "password": "******"} #4、发送POST请求 #r = requests.post(url,json=data) #r = requests_post(url,json=data) request = Request() r = request.post(url, json=data) #5、输出结果 #print(r.json()) print(r) #验证 #返回状态码 code = r["code"] #assert code == 200 AssertUtil().assert_code(code, 200) #返回结果内容 #body = json.dumps(r["body"]) body = r["body"] #assert '"user_id": 1, "username": "******"' in body AssertUtil().assert_in_body(body, '"user_id": 1, "username": "******"') #1、初始化数据库对象 conn = init_db("db_1") #2、查询结果 res_db = conn.fetchone( "select id,username from tb_users where username='******'") print("数据库查询结果", res_db) #3、验证 user_id = body["user_id"] assert user_id == res_db["id"]
def test_configitem_relationship(uri, headers, ID, testcases, fromcode,configitemfrom, relationship, tocode,configitemto,status_code, expected_result): """ 创建配置项间关系 :param uri: :param headers: :param ID: :param testcases: :param fromcode:源配置项名称code :param configitemfrom:源配置项名称 :param relationship:关系 :param tocode:目的配置项名称code :param configitemto:目的配置项名称 :return: """ relationshipkey = get_dictionary(relationship) configitem_from = get_configitem_key(fromcode,configitemfrom) configitem_to = get_configitem_key(tocode,configitemto) create_configitemRelationship_param = [{ "_to": configitem_to, "_from": configitem_from, "dictionaryItemKey": relationshipkey._key }] create_configitemRelationship_response = requests.post(url = uri + configitem_relationship_url, headers = headers, json =create_configitemRelationship_param).json() allure.attach("请求响应code", str(create_configitemRelationship_response['status'])) allure.attach("请求响应结果", str(create_configitemRelationship_response)) my_log().info(create_configitemRelationship_response) AssertUtil().assert_code(create_configitemRelationship_response['status'], status_code) AssertUtil().assert_in_body(create_configitemRelationship_response['data'], expected_result)
def test_update_dictionaries(uri, headers, ID, testcases, dictionaries_name, update_dictionaries_name, status_code, expected_result): DictionaryKey = get_dictionaries_key(dictionaries_name) DictionaryCode = get_dictionaries_code(dictionaries_name) parentDictionaryKey = get_parent_dictionaries_key(dictionaries_name) parentDictionaryCode = get_parent_dictionaries_code(dictionaries_name) update_dictionaries_param = { 'name': update_dictionaries_name, 'code': DictionaryCode, 'properties': { 'reverseName': "" }, 'parentDictionaryKey': parentDictionaryKey, 'parentDictionaryCode': parentDictionaryCode } update_dictionaries_response = requests.put( url=uri + update_dictionaries_url + str(DictionaryKey), headers=headers, json=update_dictionaries_param).json() allure.attach("请求响应code", str(update_dictionaries_response['status'])) allure.attach("请求响应结果", str(update_dictionaries_response)) my_log().info(update_dictionaries_response) AssertUtil().assert_code(update_dictionaries_response['status'], status_code) AssertUtil().assert_in_body(update_dictionaries_response['data'], expected_result)
def assert_db(db_name, result, db_verify): assert_util = AssertUtil() sql = init_db(db_name) db_res = sql.fetchone(db_verify) log.debug("数据库查询结果:{}".format(str(db_res))) verify_list = dict(db_res).keys() for line in verify_list: res_line = result[line] res_db_line = dict(db_res)[line] assert_util.assert_body(res_line, res_db_line)
def assert_db(db_name, res_body, db_verify): """ 数据库验证 """ assert_util = AssertUtil() sql = init_db(db_name) db_res = sql.fetch_one(db_verify) log.debug(f"数据库查询结果:{str(db_res)}") for db_k, db_v in db_res.items(): res_line = res_body[db_k] assert_util.assert_body(res_line, db_v)
def test_01(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=GetSampleDeviceAndPoint" data = {"Inputs": "{CName:\"\",MCode:0,pageCurrent:1,PageSize:10}"} requests = Request() r = requests.post(url, data=data, headers=None) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body(body, "'Page': {'Current': 1, 'Total': 150}") print(body)
def assert_db(db_name,result,db_verify): assert_util = AssertUtil() sql = init_db(db_name) db_res = sql.fetchone(db_verify) log.debug("数据库查询结果: {}".format(str(db_res))) # 2.)---------数据库的结果与接口返回的结果验证--------- # 获取数据库结果的key verify_list = list(dict(db_res).keys()) # 根据key获取数据库结果,获取接口返回结果 for line in verify_list: res_db_line = dict(db_res)[line] res_line = result[line] # 验证 assert_util.assert_body(res_db_line, res_line)
def test_01(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=DeleteReportInfo" data = {"Inputs": "{ID:1}"} requests = Request() r = requests.post(url, data=data, headers=None) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body( body, "'ErrorCode': 0, 'Success': True, 'ExceptionMsg': '删除成功!'") print(body) conn = init_db("db_1") conn.execute("truncate table TB_IMS_REPORTFORMS")
def test_update_datacenter(uri, headers, ID, testcases, regionname, update_region, description, status_code): """ 编辑数据中心 :param uri: :param headers: :param ID: :param testcases: :param regionname:需要编辑的数据中心名称 :param update_region: 更改的数据中心名称 :param description: 更改后的数据中心描述 :return: """ regionId = str(get_datacenterid(uri, headers, regionname)) update_datacenter_data = { "name": update_region, "description": description } update_datacenter_response = requests.post( url=uri + createDataCenter_url + "/" + regionId, data=json.dumps(update_datacenter_data), headers=headers).json() allure.attach("请求响应code", str(update_datacenter_response['status'])) allure.attach("请求响应结果", str(update_datacenter_response)) my_log().info(update_datacenter_response) AssertUtil().assert_code(update_datacenter_response['status'], status_code) assert update_region in get_datacenter(uri, headers)
def assert_db(db_name, result, db_verify): """ 数据库结果验证 :param db_name: :param result: :param db_verify: :return: """ # 1.初始化数据库 # sql = init_db("db_1") sql = init_db(db_name) # 2.查询sql,excel文件中定义好的 db_res = sql.fetchone(db_verify) # print(db_res) log.debug("数据库查询结果:{}".format(str(db_res))) # 3.数据库的结果与接口返回的结果验证 # 获取数据库结果的key verify_list = list(dict(db_res).keys()) # 根据key获取数据库结果,以及 接口返回的结果 for line in verify_list: # res_line = res["body"][line] res_line = result[line] res_db_line = dict(db_res)[line] # 进行验证 AssertUtil().assert_body(res_line, res_db_line)
def test_good_list(): # url = "http://211.103.136.242:8064/categories/115/skus" #---使用配置文件的url了 #定义测试数据 url_y = ConfigYaml() url_path = url_y.get_conf_url() url = url_path + "/categories/115/skus" data = {"page": "1", "page_size": "10", "ordering": "create_time"} # r = requests.get(url,json=data) r = requeests_get(url, json=data) from utils.RequestsUtil import request request = request() r = request.get(url, json=data) # print(r.json()) print(r) code = r["code"] # print("-----test",code) AssertUtil().assert_code(code, 200) # body = r["body"] # print("-------test------",body) # AssertUtil().assert_in_body(body, 'count:14') #初始化数据对象 conn = init_db("db_1") #查询结果 res_db = conn.fetchone("select * from roles") print("数据库查询结果:%s" % res_db)
def assert_db(db_name, result, db_verify): assert_util = AssertUtil() #sql = init_db("db_1") sql = init_db(db_name) # 2、查询sql,excel定义好的 db_res = sql.fetchone(db_verify) # 3、数据库的结果与接口返回的结果验证 # 获取数据库结果的key verify_list = list(dict(db_res).keys()) # 根据key获取数据库结果,接口结果 for line in verify_list: #res_line = res["body"][line] res_line = result[line] res_db_line = dict(db_res)[line] # 验证 assert_util.assert_body(res_line, res_db_line)
def login(): Con = ConfigYaml() con_url = Con.get_url_yaml() url = con_url + "/authorizations/" data = {"username": "******", "password": "******"} requset = Request() r = requset.post(url, json=data) code = r["code"] print(code) # print(r) AssertUtil().assert_code(code, 200) body = r["body"] # print(body,"-------------------") # AssertUtil().assert_in_body(body, '"user_id": 1, "username": "******"') #初始化数据库对象 mysql = MySql() #调用查询方法 res_db = mysql.fetchone( "select id,username from tb_users where username='******'") # print("数据库查询的结果:{}".format(res_db)) #验证 user_id = body["user_id"] assert user_id == res_db["id"] return r
def test_03(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=GetSampleDeviceAndPoint" data = { "Inputs": "{CName:\"1设备11\",MCode:\"M014\",pageCurrent:1,PageSize:10}" } headers = { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" } requests = Request() r = requests.post(url, data=data, headers=headers) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body(body, "'Page': {'Current': 1, 'Total': 1}") print(body)
def test_08(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=GetDeviceBySysAndName" # data = {"Inputs":"{DateType:0,List:[{TableNum:18,Num:1,RecordNo:null,DeviceId:1}]}"} # data = {"Inputs": "{strMcode:\"M009\",strName:\"\",PageCurrent:1,PageSize:10}"} data = { "Inputs": "{strMcode:0,strName:\"@@@@\",PageCurrent:1,PageSize:10}" } requests = Request() r = requests.post(url, data=data, headers=None) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body( body, "'Page': {'Current': 1, 'Total': 0}, 'list': []") print(body)
def assert_db(db_name, result, db_verify): assert_util = AssertUtil() # sql = init_db("db_1") sql = init_db("db_name") # 查询sql,excel定义好的 db_res = sql.fechone(db_verify) log.debug("数据库查询结果:{}".format(str(db_res))) # 数据库的结果与接口返回的结果验证 # 获取数据库的结果key verify_list = list(dict(db_res).keys()) # 根据key获取数据库的结果,接口的结果 for line in verify_list: # res_line = res["body"][line] res_line = result[line] res_db_line = dict(db_res)[line] # 验证 assert_util.assert_body(res_line, res_db_line)
def test_Login(): # 3、定义测试数据 conf_y = ConfigYaml() #加载config.Conf文件中的ConfigYaml类 url_path = conf_y.get_conf_url() # 读取配置文件中的url url = url_path + "/authorizations/" # 拼接url+登录参数 # url = "http://211.103.136.242:8064/authorizations/" data = {"username": "******", "password": "******"} # 4、发送Post请求 # r = requests_post(url,json = data) r = request.post(url, json=data) # 5、输出结果 # print(json.dumps(r,sort_keys=True,ensure_ascii = False,indent=4,separators=(', ', ': '))) #Json格式打印 #验证返回状态码 code = r["code"] # assert code == 200 #调用assert封装函数 AssertUtil().assert_code(code, 200) #验证返回结果内容 # body = json.dumps(r["body"]) # assert '"user_id": 1, "username": "******"' in body body = r["body"] # print(body) # AssertUtil().assert_in_body(body,'"user_id": 1') AssertUtil().assert_in_body(body, '"username": "******"') print( "***************************************************************************************************" ) #数据库断言 #1、初始化数据库对象 conn = init_db("db_1") #2、查询结果 res_db = conn.fetchone( "select id,username from tb_users where username = '******'") print("数据库查询结果:", res_db) #3、数据库断言 user_id = body["user_id"] assert user_id == res_db["id"] # 6、返回token result_token = r['body']['token'] # print(result_token) return result_token
def test_13(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=GetDevicePDRData" data = {"Inputs": "{alarmId:\"e815175d-3051-4d8e-a52e-c97d498bed7e\"}"} headers = { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" } Cookie = {"userid": "admin"} requests = Request() r = requests.post(url, data=data, headers=None, cookies=Cookie) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body( body, "'Success': False, 'ExceptionMsg': '没有数据', 'ActionName': '获取告警曲线数据'") print(body)
def test_09(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=GetHistoryMonitor" data = { "Inputs": "{particle:1,Unit:1,QueryType:\"avg\",StartTime:\"2020-09-09 00:00:00\",EndTime:\"2020-09-09 10:51:59\"," "DeviceId:1,SampleType:1,NumList:[\"20002000\"]}" } requests = Request() r = requests.post(url, data=data, headers=None) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body( body, "'Items': [{'ItemName': '模拟量点1', 'ItemValue': 10.0, 'Unit': 'V'}]") print(body)
def test_04(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=ExportDeviceHistoryData" data = { "Inputs": "{Unit:1,StartTime:\"2020-09-08 00:00\",EndTime:\"2020-09-08 11:09\",MinutesNum:10," "DeviceList:[{DeviceId:1,DeviceName:\"1设备1:\",NumList:[{SampleType:18,RecordNo:20002000}]}]}" } headers = { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" } requests = Request() r = requests.post(url, data=data, headers=headers) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body(body, "'status': 'success'") print(body)
def test_10(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=ExportSelectedAlarms" data = { "Inputs": "{AlarmIds:['e815175d-3051-4d8e-a52e-c97d498bed7e'],PageSize:10,PageIndex:1,AllAlarm:true,CampusIds:1001}" } headers = { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" } requests = Request() r = requests.post(url, data=data, headers=None) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body( body, "'Success': False, 'ExceptionMsg': '登录超时,请重新登录'") print(body)
def test_07(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?prefix=admin&action=GetDeviceLimit" # data = {"Inputs":"{DateType:0,List:[{TableNum:18,Num:0,RecordNo:null,DeviceId:1}]}"} # data = {"Inputs":"{DateType:0,List:[{TableNum:18,Num:1,RecordNo:null,DeviceId:1}]}"} data = { "Inputs": "{DateType:1,List:[{TableNum:18,Num:null,RecordNo:20002000,DeviceId:1}]}" } requests = Request() r = requests.post(url, data=data, headers=None) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body( body, "'ResLimitList': {'18_20002000': {'UpperLimit': '2', " "'LowerLimit': '1', 'DeadZoneValue': '0'}}") print(body)
def test_11(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=GetAlarms" data = { "Inputs": "{ObjName:\"模拟量1\",IsAlarmPage:\"alarm\",StartTime:\"2020-09-14 00:00\"," "EndTime:\"2020-09-14 17:21\",PageSize:10,CampusIds:1001,PageIndex:1,SrcType:0,Flag:-1}" } headers = { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" } Cookie = {"userid": "admin"} requests = Request() r = requests.post(url, data=data, headers=headers, cookies=Cookie) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body(body, "'total': 1, 'current': 1, 'Count': 2") print(body)
def test_02(): conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/action.ashx?action=GetSampleDeviceAndPoint" data = { "Inputs": "{CName:\"1设备1\",MCode:\"M009\",pageCurrent:1,PageSize:10}" } headers = { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" } requests = Request() r = requests.post(url, data=data, headers=headers) code = r["code"] AssertUtil().assert_code(code, 200) body = r["body"] AssertUtil().assert_in_body( body, "'ActionInfo': {'ErrorCode': 0, 'Success': True, 'ExceptionMsg': '', " "'ExtendContent': None}, 'Page': {'Current': 1, 'Total': 2}") print(body)
def test_delete_datacenter(uri, headers, ID, testcases, regionname, status_code): regionId = str(get_datacenterid(uri, headers, regionname)) delete_datacenter_response = requests.delete( url=uri + createDataCenter_url + "/" + regionId, headers=headers).json() allure.attach("请求响应code", str(delete_datacenter_response['status'])) allure.attach("请求响应结果", str(delete_datacenter_response)) my_log().info(delete_datacenter_response) AssertUtil().assert_code(delete_datacenter_response['status'], status_code) assert regionname not in get_datacenter(uri, headers)
def assert_mysqldb(excepted_db, db_res): """ mysql数据库结果与接口返回的结果验证 :param db_name: :param result: :param db_verify: :return: """ assert_util = AssertUtil() for line in excepted_db.keys(): try: excepted_line = excepted_db[line] res_db_line = dict(db_res)[line] assert_util.assert_body(excepted_line, res_db_line) return True except: my_log("DBUtil").error("数据库断言error, 数据库查询结果 is %s,期望数据库字段 is %s" % (db_res, excepted_db)) allure.attach("数据库断言error, 数据库查询结果 is %s,期望数据库字段 is %s" % (db_res, excepted_db)) raise