def connect_db(self): self.getdb1 = Getyaml(yamlparam=self.getdb, interface='interface').get_data() db1 = pymysql.connect(self.getdb1[0], self.getdb1[1], self.getdb1[2], self.getdb1[3], charset='utf8') return db1
def connect_db(self): """ 连接数据库 :return:连接信息 """ data = Getyaml().get_data(self.case_num, self.case_name) self.database = operate_excel.get_palce(self.case_num, self.case_name)['database'] db1 = pymysql.connect(data[0], data[1], data[2],self.database ,charset='utf8') Log().info("连接数据库成功") return db1
class Operate_db(): def __init__(self, getdb, interface_url): # 传参数据库信息的key和请求的url self.getdb = getdb self.interface_url = interface_url # 连接数据库 def connect_db(self): self.getdb1 = Getyaml(yamlparam=self.getdb, interface='interface').get_data() # 通过传参的数据库信息key获取数据库信息 db1 = pymysql.connect(self.getdb1[0], self.getdb1[1], self.getdb1[2], self.getdb1[3], charset='utf8') # 连接远程数据库 return db1 # 获取sql语句 def get_sql(self): self.sql = Getyaml(yamlparam="interface_sql", interface='interface').get_data() for key, value in self.sql.items(): if key == self.interface_url: return value # 执行sql语句 def Perform(self): # 使用cursor()方法获取操作游标 self.db = self.connect_db() self.cursor = self.db.cursor() sql = self.get_sql() version = self.db.server_version Log().info('成功登录数据库:%s,版本为:%s,执行SQL:%s' % (self.getdb, version, sql)) if "SELECT" in sql or "select" in sql: # 执行查询语句 try: self.cursor.execute(sql) results = self.cursor.fetchall() # 获取查询结果 Log().info('查询结果:%s' % results[0][0]) return results[0][0] except: Log().info("Error: unable to fetch data") raise elif "UPDATE" in sql or "update" in sql: # 执行update语句 try: self.cursor.execute(sql) # 执行sql语句 self.db.commit() Log().info("更新成功") except: self.db.rollback() # 数据更新失败,进行回滚 Log().info("Error:Has been rolled back") raise self.db.close()
def connect_db(self): self.getdb1 = Getyaml(yamlparam=self.getdb, interface='interface').get_data() # 通过传参的数据库信息key获取数据库信息 db1 = pymysql.connect(self.getdb1[0], self.getdb1[1], self.getdb1[2], self.getdb1[3], charset='utf8') # 连接远程数据库 return db1
def get_param(self): if self.place != 'database': return self.param else: #获取数据库名 self.database = Getyaml(yamlparam="interface_db", interface=self.url).port_db() Log().info('当前接口涉及数据库:%s' % self.database) #执行数据库操作 post_data = Operate_db(self.database, self.url).Perform() Log().info('数据格式为:%s' % post_data) return post_data
def get_param(self): ''' :return: 返回请求参数 ''' if self.param_place != 'database': # 非database传参直接返回excel文件中的参数中的 return self.connent else: # 获取数据库名 self.database = Getyaml(yamlparam="interface_db", interface=self.url).port_db() # 通过excel文件中的url匹配yaml文件中key为"interface_db"对应的value来匹配对应获得database名称 Log().info('当前接口涉及数据库:%s' % self.database) # 执行数据库操作 post_data = Operate_db(self.database, self.url).Perform() # 传参数据库名和url Log().info('数据格式为:%s' % post_data) return post_data
def connect(self): con_info = Getyaml().get_redis(env=self.env) try: r = redis.Redis(host=con_info[0], port=con_info[1], password=con_info[2], db=0) data1 = str(r.hget(name='showjoy_login_captcha_cache:{0}'.format(self.name), key='15868147451'), encoding = "utf8") data2 = data1.replace("false", "0") data3 = json.loads(data2.replace("true", "0")) data = data3['code'] Log().info('查询结果:{0}'.format(data)) return data except: Log().info('查询redis失败') raise # Operate_redis().connect() # sql2 = "select sum(collect_num),sum(like_num),sum(share_num) from interaction_info where note_id in (select note_id from note_info_shadow where user_id = '9140391') or note_id in (select id from note_info where user_id = '9140391')" # Operate_db(24,sql2).more_perform()
def get_sql(self): self.sql = Getyaml(yamlparam="interface_sql", interface='interface').get_data() for key, value in self.sql.items(): if key == self.interface_url: return value
from config.readyaml import Getyaml test_dir = "./testcase" discover = unittest.defaultTestLoader.discover(test_dir, pattern="case_shop*.py") if __name__ == "__main__": now = time.strftime("%Y-%m-%d-%H-%M-%S") filename = globalparam.report_path + "\\" + "result.html" fp = open(filename, 'wb') runner = HTMLTestReportCN.HTMLTestRunner( stream=fp, title=u'接口自动化测试报告', # description='详细测试用例结果', #不传默认为空 tester=u"QA" # 测试人员名字,不传默认为QA ) results = runner.run(discover) cases = "总用例数:{0}".format(results.testsRun) case_fail = "失败用例数:{0}".format(len(results.failures) + len(results.errors)) case_results = [] if len(results.failures) == 0 and len(results.errors) == 0: case_results.append("无") else: for case, reason in results.failures + results.errors: case_title = Getyaml().get_datas(str(case)) case_results.append(case_title) send_ding.Send_dingding(cases, case_fail, case_results) fp.close() shutil.copy(globalparam.report_path + "\\" + "result.html", globalparam.report_path + "\\" + now + "result.html")