def test_AppLogin(self): '''app登录''' case_name = 'app登录' log.info("执行测试用例:%s" % case_name) try: login = HttpRequest() cfg = ConfigParse() url = 'http://' + cfg.getUrl('url', 'baseUrl', url='/user-api/v1/login') print(url) data = {"mobile": "13668277212", "login_type": 1, "user_pass": "******"} headers = { 'appid': 'sdwe12e12e2e1212', 'phonetype': 'iPhone 6s', 'ostype': '2', 'refersource': 'AppStore', 'os': 'ios 12.1', 'devicecode': '008F504E-A670-41B4-ADD3-403EC6981E26', 'versionname': '3.0.0', 'versioncode': '20', 'User-Agent': '%E6%95%99%E8%82%B2%E5%AE%B6%E7%A4%BE%E5%8C%BA/20 CFNetwork/975.0.3 Darwin/18.2.0', 'Cookie': 'PHPSESSID=2buijfqibnocr73ael86tom4h4', 'Content-Type': 'application/json' } response = login.post_json(url, data, headers) print(response) test = self.assertEqual(response.status, 200) if test: log.info("测试通过") except BaseException as e: log.error("测试用例执行出错: %s,错误信息为:%s" % (case_name, e), exc_info=1) raise
def fetchall(self, sql, params=None): res = '' if self.connectDB(): try: self.cursor.execute(sql, params) res = self.cursor.fetchall() except pymysql.Error as e: log.error("查询数据库失败! sql: %s,params: %s" % (sql, params)) log.error(e) self.close() return res
def update(self, sql, params): flag = False if self.connectDB(): try: self.cursor.execute(sql, params) self.conn.commit() flag = True except pymysql.Error as e: flag = False log.error("更新数据库失败! sql: %s,params: %s" % (sql, params)) self.close() return flag
def connectDB(self): try: self.conn = pymysql.connect(host=self.host, port=int(self.port), user=self.user, password=self.password, database=self.dbName) except pymysql.Error as e: log.error(e) return False sys.exit() self.cursor = self.conn.cursor() return True
def post_json(self, url, data, headers): '''封装post方法,并用json格式传值,return响应码和响应内容''' try: data = json.dumps(data).encode('utf-8') # python数据类型转化为json数据类型 response = requests.post(url=url, data=data, headers=headers) log.info("请求的内容:%s" % data) status_code = response.status_code # 获取返回的状态码 log.info("获取返回的状态码:%d" % status_code) response = response.json() # 响应内容,json类型转化成python数据类型 log.info("响应内容:%s" % response) return status_code, response # 返回响应码,响应内容 except BaseException as e: log.error("请求失败!", exc_info=1)
def execute(self, sql, params=None): # 连接数据库 self.connectDB() try: if self.conn and self.cursor: # 正常逻辑,执行sql,提交操作 self.cursor.execute(sql, params) self.conn.commit() except pymysql.Error as e: log.error("插入数据库失败! sql: %s,params: %s" % (sql, params)) log.error(e) self.close() return False return True
def get(self, url, params=None, headers=None, files=None): '''封装get方法,return响应码和响应内容''' try: response = requests.get(url, params=params, headers=headers, files=files) log.info("请求的内容:%s" % params) status_code = response.status_code # 获取返回的状态码 log.info("获取返回的状态码:%d" % status_code) response_json = response.json() # 响应内容,json类型转化成python数据类型 log.info("响应内容:%s" % response_json) return status_code, response_json # 返回响应码,响应内容 except BaseException as e: log.error("请求失败!", exc_info=1)
def post(self, url, data, headers): '''封装post请求,return响应码和响应内容''' try: response = requests.post(url=url, data=data, headers=headers, files=None) log.info("请求的url:%s" % url) log.info("请求的内容:%s" % data) status_code = response.status_code # 获取返回的状态码 log.info("获取返回的状态码:%d" % status_code) response_json = response.json() # 响应内容,json类型转化成python数据类型 log.info("响应内容:%s" % response_json) return status_code, response_json # 返回响应码,响应内容 except BaseException as e: log.error("请求失败!", exc_info=1) # exc_info=1错误信息等级
def mail(): try: # 创建一个带附件的实例 message = MIMEMultipart() message['From'] = formataddr([username, email]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号 log.info('发件人姓名:%s' % username) log.info('发件人邮箱:%s' % email) message['To'] = ';'.join(recUserName) # 括号里的对应收件人邮箱昵称、收件人邮箱账号 log.info('收件人邮箱:' + recieveMail) message[ 'Subject'] = nowDate + "教育家社区APP接口自动化测试报告.html" # 邮件的主题,也可以说是标题 # 邮件正文内容 message.attach(MIMEText('附件为教育家社区APP接口自动化测试报告.html', 'plain', 'utf-8')) # 构造附件1,传送当前目录下的 test.txt 文件 att1 = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8') log.info('读取附件') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1.add_header("Content-Disposition", "attachment", filename=("gbk", "", "教育家社区APP接口自动化测试报告.html")) # 附件名称非中文时的写法 # att["Content-Disposition"] = 'attachment; filename="test.html")' message.attach(att1) log.info('添加附件') server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是25 log.info('连接QQ邮箱smtp服务') server.login(email, password) # 括号中对应的是发件人邮箱账号、授权验证码 log.info('连接成功') server.sendmail(email, recieveMail, message.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件 server.quit() # 关闭连接 log.info("邮件发送成功") except Exception: # 如果 try 中的语句没有执行,则会执行下面的 ret=False log.error("邮件发送失败", exc_info=1)