def test_add_sign_to_member(self, token1, tagid, userlist, errcode, errmsg, add_sign_to_member): log.info("--------开始测试冒烟") res = self.sign.add_sign_to_member(token1, tagid, userlist) log.info("--------开始结束测试") assert errcode == res["errcode"] assert errmsg in res["errmsg"]
def add_member(self, token, userid, name, mobile): """ 增加联系人,这里代码没有封装,看起来很乱 :param token: token值 :param userid: 请求参数的值 :param name: 请求参数的值 :param mobile: 请求参数的值 :return: 返回响应体 """ # Template模板二次修改的值,p_data p_data = { "token": token, "name": name, "userid": userid, "mobile": mobile } # 合并yml_api的文件路径 data_path = os.path.join(self.BASE_PATH, "data/member/add_member.yml") # 通过模板技术获取更改后的请求数据 data = self.template(data_path, p_data) # log日志打印请求体 log.info(data) # 发送封装requests的请求,获取响应值 res = self.send_api(data) return res
def test_edit_calendar_smoke(self, schedule_token): log.info("--------开始编辑日历冒烟测试") res = self.calendar.edit_calendar(schedule_token, 0, None, "gaigai", "1000FF", None) log.info("--------结束测试") assert 0 == res["errcode"] assert "ok" in res["errmsg"]
def test_edit_metting_room_smoke(self, meeting_smoke, token): log.info("--------开始编辑会议室的冒烟测试") res = self.meeting.edit_meeting_room(token, 2, "edit", None, None, None, None, None) log.info("--------开始结束测试") assert 0 == res["errcode"] assert "updated" in res["errmsg"]
def test_add_calendar_smoke(self, schedule_token): log.info("--------增加日历开始冒烟测试") res = self.calendar.add_calendar(schedule_token, "calendar", 1, 0, "test1", "0001FF", "hehedada") log.info("--------结束测试") assert 0 == res["errcode"] assert "ok" in res["errmsg"]
def test_edit_calendar_smoke(self, schedule_token): log.info("--------开始编辑日程冒烟测试") res = self.schedule.edit_schedule(schedule_token, "schedule", 1, "2020-10-01 00:00:00", "2020-10-02 00:00:00", "calendar", "abc", None, None) log.info("--------结束测试") assert 0 == res["errcode"] assert "ok" in res["errmsg"]
def test1_add_member(self): userid = "tong1234" name = "tong1234" mobile = "13172771165" res = self.member.add_member(userid, name, mobile) log.info(res) assert res["errcode"] == 0
def send_api_data(self, path, p_data, sub): path = os.path.join(self.base_path, path) data = self.template(path, p_data, sub) log.info(f"请求为:{data}") res = self.send_api(data) log.info((f"响应为:{res}")) return res
def test_add_depart(self, token, parentid, name, errcode, errmsg, add_depart): log.info("-------------开始增加部门测试---------") res = self.depart.add_depart(token, parentid, name) log.info("-------------测试结束---------") assert res["errcode"] == errcode assert errmsg in res["errmsg"]
def delete_member(self, token, userid): p_data = {"token": token, "userid": userid} data_path = os.path.join(self.BASE_PATH, "data/member/delete_member.yml") data = self.template(data_path, p_data) log.info(f"请求的参数:{data}") res = self.send_api(data) return res
def test(self, token1, tagid, userlist, errcode, errmsg, delete_sign_to_member, invalidlist): log.info("--------开始删除用户的标签测试") res = self.sign.delete_sign_to_member(token1, tagid, userlist) log.info("--------开始结束测试") assert errcode == res["errcode"] assert errmsg in res["errmsg"] if self.sign.jsonpath(res, "$.invalidlist"): assert invalidlist in self.sign.jsonpath(res, "$.invalidlist")
def send_api_data(self, p_data, path): # 打印传入template要改变的$变量值 log.info(f"template传入的p_data:{p_data}") # 读取yml文件的路径 data_path = os.path.join(self.BASE_PATH, path) # 改变yml文件的$变量值 data = self.template(data_path, p_data) log.info(f"请求的所有参数:{data}") res = self.send_api(data) return res
def get_depart_member_explicit(self, token, department_id, fetch_child): p_data = { "token": token, "department_id": department_id, "fetch_child": fetch_child } log.info(p_data) data_path = os.path.join(self.BASE_PATH, "data/member/get_depart_member_explicit.yml") data = self.template(data_path, p_data) log.info(data) res = self.send_api(data) return res
def add_member(self, token, userid, name, mobile): p_data = { "token": token, "name": name, "userid": userid, "mobile": mobile } data_path = os.path.join(self.BASE_PATH, "data/member/add_member.yml") data = self.template(data_path, p_data) log.info(data) res = self.send_api(data) return res
def send_api_data(self, p_data, path): """ 封装从yml拿数据,并改变替换yml原本的$变量的数据 :param p_data: Template二次转化的变量数据 :param path: yml_api的路径 :return: """ # 打印传入template要改变的$变量值 log.info(f"template传入的p_data:{p_data}") # 读取yml文件的路径 data_path = os.path.join(self.BASE_PATH, path) # 改变yml文件的$变量值 data = self.template(data_path, p_data) log.info(f"请求的所有参数:{data}") # 获取响应体 res = self.send_api(data) return res
def delete(self, query): """ 运行mysql的delete语句 :param query: delete语句 """ log.info(f"delete语句为:{query}") try: # 定义游标,并通过execute执行delete语句 cur = self.conn.cursor() cur.execute(query) # delete执行成功后commit提交数据 cur.execute("commit") log.info("数据删除成功") except Exception as e: log.error(f"delete语句失败,原因:{e}") # delete失败后rollback回滚数据 cur.execute("rollback")
def delete_calendar(self, token, index): """ 删除日历 :param token: 日历的token值 :param index: 在数据库获取第几个id值 :return: 返回响应体 """ # 从数据库获取cal_id_list cal_id = self.get_cal_id_list()[index] p_data = {"ip": self.ip, "token": token, "cal_id": cal_id} res = self.send_api_data(self.yml_api_path, p_data, "delete") # 当删除api成功时,同步从数据库中删除cal_id if res["errcode"] == 0: sql.delete(f"delete from cal_id where cal_id='{cal_id}'") else: log.info("日历删除请求有误,数据库没有删除cal_id") return res
def insert(self, query): """ 运行mysql的select语句 :param query: insert语句 """ log.info(f"insert语句为:{query}") try: # 定义游标,并通过execute执行insert语句 cur = self.conn.cursor() cur.execute(query) # insert执行成功后commit提交数据 cur.execute("commit") log.info(f"数据插入成功") except Exception as e: log.error(f"insert 语句错误,原因是{e}") # insert失败后rollback回滚数据 cur.execute("rollback")
def select(self, query): """ 运行mysql的select语句 :param query: select语句 :return: select_data:返回全部的select语句的数据 """ log.info(f"select语句为:{query}") try: # 定义游标,并通过execute执行sql语句 cur = self.conn.cursor() cur.execute(query) # fetchall读取游标中的所有select数据 select_data = cur.fetchall() log.info("数据查询成功") # 返回select数据 return select_data except Exception as e: log.error(f"select语句错误,错误原因是:{e}")
def delete_schedule(self, token, index): """ 删除日程 :param token: 日历的token值 :param index: 在数据库获取第几个id值 :return: 返回响应体 """ # 从数据库获取schedule_id schedule_id = self.get_schedule_id_list()[index] p_data = {"ip": self.ip, "token": token, "schedule_id": schedule_id} res = self.send_api_data(self.data_path, p_data, "delete") # 当删除api成功时,同步从数据库中删除schedule_id if res["errcode"] == 0: sql.delete( f"delete from schedule_id where schedule_id='{schedule_id}'") else: log.info("删除请求失败,无法删除schedule_id") return res
def test04_delete_member(self, userid, errcode, errmsg, delete): log.info("-------开始测试获取成员-------") res = self.member.delete_member(self.token, userid) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"]
def test02_get_member(self, userid, errcode, errmsg, get): log.info("-------开始测试获取成员-------") res = self.member.get_member_info(self.token, userid) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"]
def test01_add_member(self, userid, name, mobile, errcode, errmsg, add): log.info(f"-------开始测试增加成员-------") res = self.member.add_member(self.token, userid, name, mobile) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"]
def test_get_invite_qr(self, size, errcode, errmsg): log.info("-------开始获取企业微信二维码-------") res = self.member.get_invite_qr(self.token, size) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"]
def test_active_stat(self, date, errcode, errmsg): log.info("-------开始查看企业微信活跃度-------") res = self.member.get_active_stat(self.token, date) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"]
def test03_edit_member(self, userid, name, mobile, errcode, errmsg, edit): log.info("-------开始修改获取成员-------") res = self.member.edit_member(self.token, userid, name, mobile) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"]
def send_api_data(self, path, p_data, sub): """ 1.进一步优化封装请求,是本框架的第二个核心技术要点 2.解决了如何非必填字段的问题,接口测试的非必填字段,只需要传入None值就好 2.1 本方法就是解决如何传入None的问题 :param path: 存放yml的api数据的相对路径 :param p_data: Template模板里面,二次转化的数据 :param sub: yml的二级数据目录,区别同一个api类中不同的api方法,比如add,delete :return: 返回请求体的字典类型 """ # 链接根路径和yml文件的相对路径,简化文件路径 path = os.path.join(self.Base_Path, path) # 获取请求数据 data: dict = self.template(path, p_data, sub) log.info(f"api模板改变的参数为:{p_data}") # 由于Template转化的数据都是字符串,None也会变成'None',通过下面的方法解决这个问题 # 防止有些请求没有请求体的,不然就报错了 try: for i in data['json'].keys(): if data['json'][i] == 'None': data['json'][i] = None except: pass log.info(f"修改后的请求为:{data}") res = self.send_api(data) log.info((f"响应为:{res}")) return res
def test_all_smoke_member(self, test_all_pre_data): add_res = self.member.add_member(self.token, "tong1234", "tong1234", "13172771165") edit_res = self.member.edit_member(self.token, "tong1234", "tong1234", "13172771165") del_res = self.member.delete_member(self.token, "tong1234") multi_del_res = self.member.multi_delete_member( self.token, ["tongtong1", "tongtong2", "tongtong3"]) qr_res = self.member.get_invite_qr(self.token, 1) active_res = self.member.get_active_stat(self.token, "2020-10-10") depart_res = self.member.get_depart_member(self.token, "1", "1") depart_res_e = self.member.get_depart_member_explicit( self.token, "1", "1") assert add_res["errcode"] == 0 assert edit_res["errcode"] == 0 assert del_res["errcode"] == 0 assert multi_del_res["errcode"] == 0 assert qr_res["errcode"] == 0 assert active_res["errcode"] == 40058 assert depart_res["errcode"] == 0 assert depart_res_e["errcode"] == 0 log.info("finish")
def test05_multi_delete_member(self, userid_list, errcode, errmsg, multi_delete): log.info("-------开始批量删除获取成员-------") res = self.member.multi_delete_member(self.token, userid_list) log.info(f"打印响应结果:{res}") log.info("-------测试结束-------") assert res["errcode"] == errcode assert errmsg in res["errmsg"]
def test_add_metting_room_smoke(self, meeting_smoke, token): log.info(f"token:{token}") log.info("--------开始增加会议室的冒烟测试") res = self.meeting.add_meeting_room(token, "smoke1", 30, None, None, None, None) log.info("--------开始结束测试") assert 0 == res["errcode"] assert "ok" in res["errmsg"]