def get_main(self, host, lujing, data=None, headers=None): try: url = host + lujing if headers != None: # print("ok") if type(headers) == dict: headers = headers else: headers = eval( headers) # 如果data 不是dict 类型 就必须先是str 然后转成dict res = requests.get(url=url, data=data, headers=headers, verify=False) # print(res) else: res = requests.get(url=url, data=data, verify=False) if res.status_code != 200: # 判断响应状态码是否不为200 Log().info(res.json()) response = res.json() # 将返回的数据转换为json格式的 字典 return response except Exception as e: Log().info('post 请求错误 错误原因为%s' % e) return ('post 请求错误 错误原因为%s' % e)
def post_main(self, host, lujing, data, headers=None): #host 为基础url ,lujing为路径 try: # print(type(data)) if type(data) == dict: #判断是否为dict 类型 # print("ok") data = data else: # print("nooo") data = eval(data) #如果data 不是dict 类型 就必须先是str 然后转成dict url = host + lujing # print(url) if headers != None: if type(headers) == dict: headers = headers else: headers = eval( headers) #如果data 不是dict 类型 就必须先是str 然后转成dict res = requests.post(url=url, json=data, headers=headers, verify=False) else: res = requests.post(url=url, json=data, verify=False) if res.status_code != 200: #判断响应状态码是否不为200 Log().info(res.json()) response = res.json() # 将返回的数据转换为json格式的 字典 return response except Exception as e: Log().info('post 请求错误 错误原因为%s' % e) return ('post 请求错误 错误原因为%s' % e)
def setUpClass(cls): Log().info("正在执行:登陆测试") # token=get_yaml_variable("X-SessionToken-With") # print("登陆后的token为:%s"%token) user_Session() cls.app_host = cls.app_host cls.User_session = { "X-SessionToken-With": get_yaml_variable("X-SessionToken-With") } cls.app_headers.update(cls.User_session) print(cls.app_headers)
def __init__(self, excelPath, sheet_id): if not os.path.exists(excelPath): Log().error('测试用例文件不存在!') sys.exit() self.excelPath = excelPath self.data = xlrd.open_workbook(excelPath) self.table = self.data.sheets()[sheet_id] # 获取第一行作为key值 self.keys = self.table.row_values(0) # 获取总行数 self.rowNum = self.table.nrows # 获取总列数 self.colNum = self.table.ncols
def test_check_order(self): """ 检查订单状态 :return: """ Log().debug("正在执行:检查订单状态") lujing = "/loan/order/check/" headers = self.app_headers res = RunMethod().run_main("get", self.app_host, lujing, headers=headers) self.assertEqual(res["code"], 200) print(res)
def send_email(file_path): # ----------1.跟发件相关的参数------ # smtpserver # 发件服务器 smtpserver = "smtp.qq.com" port = 465 # 端口 sender = "*****@*****.**" # 账号 shouquan = "rpybibdelqojbgfc" # 授权码 qq邮箱 receiver = ["*****@*****.**"] # 接收人 # ----------2.编辑邮件的内容------ with open (file_path,'rb') as fb: mail_body=fb.read() # body='<p>这个是发送的个人自动化邮件</p>' subject="主题:66ifuel接口测试报告" msg = MIMEMultipart() msg['from'] = sender msg['to'] = ";".join(receiver) #发送多个接收人 msg['subject'] =subject #正文 # 定义邮件正文为 html 格式 body = MIMEText(mail_body, "html", "utf-8") msg.attach(body) # # #附件 att=MIMEText(mail_body,'base64','utf-8') att["Content-Type"] = "application/octet-stream" att["Content-Disposition"] = 'attachment; filename="test_report.html"' #重命名的邮件 msg.attach(att) # # # # ----------3.发送邮件163------ # smtp = smtplib.SMTP() # smtp.connect(smtpserver) # 连服务器 try: # # ----------4.发送邮件QQ------ smtp = smtplib.SMTP_SSL(smtpserver, port) smtp.login(sender, shouquan) # 登录 smtp.sendmail(sender, receiver, msg.as_string()) # 发送 smtp.quit() # 关闭 except Exception as e: # print(e) Log().warning('邮件发送失败:%s'%e)
def dict_data(self): """ 读取excel数据 :return: """ Log().info('开始读取excel数据') if self.rowNum <= 1: print("总行数小于1") else: r = [] j = 1 for i in list(range(self.rowNum - 1)): #循环行 s = {} # 从第二行取对应values值 s['rowNum'] = i + 2 values = self.table.row_values(j) # print(values) for x in list(range(self.colNum)): # 在行的循环下进行 循环列 s[self.keys[x]] = values[x] # # print(self.keys[x]) # print( values[x]) r.append(s) j += 1 return r
def setUpClass(cls): Log().info("正在执行:GPS管理模块测试") cls.cms_host = cls.cms_host cls.session = cms_cookies() # 将获取的cookie设置为session变量全局使用
def send_requests(session, testdata): Log().info("开始发送请求") "封装requests请求" url = testdata["url"] method = testdata["method"] # 请求头部headers try: headers = eval(testdata["headers"]) except: headers = None # post请求body类型 body_type = testdata["type"] # 请求的datas参数 # 判断传data数据还是json datas = None #定义一个datas 作为返判断返回值 if body_type == 'from': try: datas = eval(testdata["datas"]) #取出来的str 需转换为dict except: datas = None elif body_type == 'json': try: datas = json.dumps(testdata["datas"]) #取出来的str 转变成json类型字典 except: datas = None #执行的id test_nub = testdata['id'] print("*******正在执行用例:----- %s ----**********" % test_nub) print("请求方式:%s, 请求url:%s" % (method, url)) # print(type(datas)) print("请求datas:%s" % datas) response = {} # 接受返回数据 后面写入数据 res = None #定义一个res 作为返回值 is_run = testdata['must'] # print(is_run) if is_run == 'yes': #判断是否执行 try: if method == 'get': res = session.get(url=url, headers=headers, params=datas, verify=False) response["Times"] = str(res.elapsed.total_seconds()) print("页面返回信息:%s" % res.content.decode("utf-8")) elif method == 'post': res = session.post(url=url, headers=headers, data=datas, verify=False) response["Times"] = str(res.elapsed.total_seconds()) # print(response["Times"]) print("页面返回信息:%s" % res.content.decode("utf-8")) else: print('方法有误') return res # return是用来终止代码运行的 在返回结果后 response['rowNum'] = testdata['rowNum'] response["text"] = res.content.decode("utf-8") str_response = json.loads(res.text) # print(str_response["code"]) if str_response["code"] != 200: #如果状态码不为200时写入 Response response["Response"] = response["text"] else: response["Response"] = "" #如果=200则写入空值 response["Msg"] = "" #提前定义Msg 默认为空 if testdata["CheckPoint"] in response["text"]: response["Result"] = "pass" else: response["Result"] = "fail" print("用例测试结果: %s---->%s" % (test_nub, response["Result"])) return response except Exception as msg: response["Msg"] = str(msg) # print(response) return response else: response = "" #如果不执行response 赋值为空 后面调用写入方法好判断 print("用例跳过") return response
def setUpClass(cls): Log().info("正在执行:登陆测试") # token=get_yaml_variable("X-SessionToken-With") # print("登陆后的token为:%s"%token) cls.cms_host = cls.cms_host cls.session = cms_cookies() # 将获取的cookie设置为session变量全局使用