def test_video(self, case): # datetime_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # print(datetime_str) url = conf.get("evn", "url") + case["url"] print("新增视频的url:", url) headers = eval(conf.get("evn", "headers")) headers["accessToken"] = getattr(CaseData, "accessToken") method = case["method"] data = eval(replace_data(case["data"])) # data = eval(case["data"]) print("新增视频点播的参数:", data) expected = eval(case["expected"]) row = case["case_id"] + 1 response = self.request.send_request(url=url, method=method, json=data, headers=headers) res = response.json() print("新增视频的实际请求:", res) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) except AssertionError as e: self.excel.write_data(row=row, column=8, value="未通过") log.error("用例{}执行未通过".format(case["title"])) log.exception(e) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.info("用例{}执行通过".format(case["title"]))
def test_01login(self, case): # 这里的case是用于接收测试用例处的数据 url = conf.get("evn", "url") + case["url"] # print("请求路径:", url) headers = eval(conf.get("evn", "headers")) method = case["method"] # print(method, type(method)) data = eval(case["data"]) # print(data, type(data)) expected = eval(case["expected"]) # print("预期结果:", expected) row = case["case_id"] + 1 response = self.request.send_request(url=url, headers=headers, method=method, json=data) # 通过对象调用方法 res = response.json() print("实际结果:", res) # 添加断言: try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) except AssertionError as e: self.excel.write_data(row=row, column=8, value="未通过") log.error("用例{}执行未通过".format(case["title"] + 1)) log.exception(e) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.info("用例{}执行通过".format(case["title"]))
def test_addcourse(self, case): url = conf.get("evn", "url") + case["url"] print("新增课件的url:", url) headers = eval(conf.get("evn", "headers")) headers["accessToken"] = getattr(CaseData, "accessToken") method = case["method"] data = eval(replace_data(case["data"])) expected = eval(case["expected"]) row = case["case_id"] + 1 response = self.request.send_request(url=url, method=method, json=data, headers=headers) res = response.json() print("新增课件的实际请求:", res) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) except AssertionError as e: self.excel.write_data(row=row, column=8, value="未通过") log.error("用例{}执行未通过".format(case["title"])) log.exception(e) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.info("用例{}执行通过".format(case["title"]))
def setUpClass(cls): url = conf.get("evn", "url") + "/server//adminLogin/byPassword" headers = eval(conf.get("evn", "headers")) data = {"phone": "18502183690", "password": "******"} response = cls.request.send_request(url=url, headers=headers, json=data, method="post") res = response.json() print("实际结果:", res) accessToken = jsonpath.jsonpath(res, "$..accessToken")[0] CaseData.accessToken = accessToken print("提取的token值:", CaseData.accessToken)
def send_email(filename, title): # 封装成发送邮件的函数 smtp = smtplib.SMTP_SSL(host=conf.get("email", "host"), port=conf.getint( "email", "port")) # 连接邮件, 注意使用getint方法 获取port 端口号是整数类型 smtp.login(user=conf.get("email", "user"), password=conf.get("email", "pwd")) # 创建多组件的邮件 msg = MIMEMultipart() # 通过类创建对象 with open(filename, "rb") as f: # 使用二进制rb方式进行读取,以防止打开报告出现乱码 content = f.read() # 创建文本内容 text_msg = MIMEText(content, _subtype="html", _charset="utf8") # 添加到多组件的邮件中 msg.attach(text_msg) # 创建邮件的附件 report_file = MIMEApplication(content) report_file.add_header("content-disposition", "attachment", filename=filename) # 设置发送邮件的头部信息 # 将邮件添加到多组件的邮件中 msg.attach(report_file) msg["Subject"] = title msg["From"] = conf.get("email", "from_addr") msg["To"] = conf.get("email", "to_addr") smtp.send_message(msg, from_addr=conf.get("email", "from_addr"), to_addrs=conf.get("email", "to_addr"))
def create_logger(): mylog = logging.getLogger(conf.get("log", "name")) # 日志收集器对象 mylog.setLevel(conf.get("log", "level")) # 设置日志等级 sh = logging.StreamHandler() # 日志输出渠道--》控制台 sh.setLevel(conf.get("log", "sh_level")) mylog.addHandler(sh) # fh = logging.FileHandler(filename="log.log", encoding="utf8") # 日志输出渠道-->在当前文件包下生成文件 fh = logging.FileHandler(filename=os.path.join(LOGDIR, "log.log"), encoding="utf8") # 日志输出渠道--》日志文件 fh.setLevel(conf.get("log", "fh_level")) mylog.addHandler(fh) format = "%(asctime)s-[%(filename)s-->line:%(lineno)d]-%(levelname)s-%(message)s" fm = logging.Formatter(format) sh.setFormatter(fm) # 输出格式和渠道进行绑定 fh.setFormatter(fm) return mylog
def setUp(self): url = conf.get("evn", "url") + "/server//file/upload" headers = eval(conf.get("evn", "headers")) headers["accessToken"] = getattr(CaseData, "accessToken") files = { "file": ("test.jpg", open( r"D:\下载\Automation Code study\tax-school-hb\testcases\test.jpg", "rb"), "image/jpeg") } response = self.request.send_request(url=url, files=files, headers=headers, method="post") res = response.json() print("文件上传实际结果:", res) imgkey = jsonpath.jsonpath(res, "$..data")[0] CaseData.imgkey = imgkey print("提取的filekey值:", CaseData.imgkey)
def replace_data(s): r = r"#(.*?)#" while re.search(r, s): res = re.search(r, s) key = res.group(1) # 替换后的value值首先从配置文件中找,配置文件中如无则从封装的空类中找(CaseData类是存储所有提取的变量信息) try: value = conf.get("test_data", key) # 括号前是实值, key是用例中符合规则的变量 except Exception: value = getattr(CaseData, key) finally: s = re.sub(r, value, s, 1) return s