class WeiXinSendMessage: def __init__(self, excel_name, sheet_name): self.excel = DoExcel(excel_name, sheet_name) self.row_num = self.excel.get_row_num(0, "weixin-002") self.url = self.excel.read_cell(self.row_num, 2) self.new_request = ConfigHttp("HTTP2", "host", "port", "timeout") def send_message(self): row_num = self.excel.get_row_num(0, "weixin-001") access_token = self.excel.read_cell(row_num, 6) params = {"access_token": access_token} if access_token: self.new_request.set_url(self.url) self.new_request.set_data(data=data) self.new_request.set_params(params) r = self.new_request.post() errmsg = r.json()["errmsg"] if errmsg == "send job submission success" or errmsg == "clientmsgid exist": self.excel.write_excel(0, self.row_num, 10, errmsg) else: print("send job submission fail") else: print("access_token is null")
class Baidu: def __init__(self, excel_name, sheet_name): ''' 初始化DoExcel和ConfigHttp ''' self.excel = DoExcel(excel_name, sheet_name) self.row_num = self.excel.get_row_num(0, "bd-001") self.excel.write_excel(0, self.row_num, 10, None) self.new_request = ConfigHttp("HTTP", "host", "port", "timeout") self.url = self.excel.read_cell(self.row_num, 2) def baidu_get(self): self.new_request.set_url(url=self.url) self.new_request.set_headers(headers) self.new_request.set_params(params=params) r = self.new_request.get() print(r.status_code) self.excel.write_excel(0, self.row_num, 10, r.status_code) return r.status_code
class WeiXinTest(unittest.TestCase): '''微信公众号测试''' def setUp(self): self.excel = DoExcel("case.xls", "weixin") self.weixin = WeiXin(self.excel) self.logger = Logger("FOX", cmd_log_level=logging.INFO, file_log_level=logging.INFO) def test_get_token(self): access_token = self.weixin.get_token().json()["access_token"] print(access_token) row_num = self.excel.get_row_num(0, "weixin-001") self.excel.write_excel(0, row_num, 6, access_token) def test_post_message(self): self.weixin.send_message() row_num = self.excel.get_row_num(0, "weixin-002") result = self.excel.read_cell(row_num, 10) self.assertEqual(result, "pass") def tearDown(self): pass