def send_email(report_name): cf = Config() logger = Log.get_logger() report_file = os.path.join(pro_path, cf.get_runtime("report_dir"),report_name) with open(report_file, "rb") as f: body = f.read() # 格式化email正文 msg = MIMEText(body, "html", "utf-8") # 配置email头 msg["Subject"] = cf.get_email("subject") msg["From"] = cf.get_email("user") msg["To"] = cf.get_email("receiver") # 连接smtp服务器,发送邮件 smtp = smtplib.SMTP() smtp.connect(cf.get_email("server")) smtp.login(cf.get_email("user"),cf.get_email("pwd")) smtp.sendmail(cf.get_email("user"), cf.get_email("receiver"), msg.as_string()) print("邮件发送成功")
def __init__(self): self.logger = Log.get_logger() self.cf = Config()
import os from common.config import root_path from common.log import Log log = Log.get_logger(name="common_func") class CommonFunction: def write_access_token_to_txt(self, filename, content): file_path = os.path.join(root_path, "conf", filename) with open(file_path, 'w') as f: f.write(content) log.info("access_token写入txt文件成功!") def red_access_token_txt(self, file_path): with open(file_path, 'r') as f: return f.readline() if __name__ == '__main__': func = CommonFunction() file_path = "../conf/contacts_access_token.txt" print(func.red_access_token_txt(file_path))
import os import allure import json from apis.base_api import BaseApi from common.config import root_path from common.log import Log log = Log.get_logger("manger_department") class ManageDepartment(BaseApi): def __init__(self): BaseApi.__init__(self) application_name = "contacts" # 判断当前token是否过期 token_path = os.path.join(root_path, "conf", "contacts_access_token.txt") # token_path = "../../conf/contacts_access_token.txt" self.judge_access_token_is_valid(application_name, token_path) self.access_token = self.func.red_access_token_txt(token_path) # print("<<access_token>>:", self.access_token) def create_department(self, url, payload): """创建部门""" with allure.step("获取创建新部门请求参数"): params = { "access_token": self.access_token } log.info("url: " + str(url)) log.info("payload: {}".format(payload))
import requests from common.config import root_path, Config from common.log import Log from common.common_func import CommonFunction log = Log.get_logger(name="base_api") class BaseApi: def __init__(self): self.cf = Config() self.func = CommonFunction() def send_post_json(self, url, json_obj, params=None): if params: self.res = requests.post(url, json=json_obj, params=params) else: self.res = requests.post(url, json=json_obj) def send_get(self, url, params): self.res = requests.get(url, params=params) def get_response(self): return self.res.json() def get_access_token(self, application_name): """获取access_token""" # 请求参数获取 token_url = self.cf.get_access("token_url")