def hander(self,level,msg): """设置日志输出""" self.mylog = logging.getLogger("mylg") # 创建日志收集器 self.mylog.setLevel(self.level) formatters=Readconf(SplitPath().join_confname()).read_logformatter() #方法一,配置文件中不写logging.Formatter,在当前文件创建对象 formatter=logging.Formatter(formatters)#取到配置文件的格式后要创建实例 # 方法二,配置文件中写入logging.Formatter,通过eval转换原类型 # f=eval(formatters) #方法三,不通过配置文件,直接在当前文件写入格式 # formatter = logging.Formatter( # '%(asctime)s-%(levelname)s-%(filename)s-%(name)s-日志信息:%(message)s') output=logging.FileHandler(SplitPath().join_mylogname(),encoding="utf-8") #设置输出等级 output.setLevel(self.level) #设置输出格式 output.setFormatter(formatter) #输出渠道对接 self.mylog.addHandler(output) if level == "DEBUG": self.mylog.debug(msg) elif level == "INFO": self.mylog.info(msg) elif level == "WARNING": self.mylog.warning(msg) elif level == "ERROR": self.mylog.error(msg) else: self.mylog.critical(msg) self.mylog.removeHandler(output) # 清除日志渠道
# -*- utf-8 -*- #@Time :2019/3/1011:55 #@Author :无邪 #@File :request_http.py #@Software:PyCharm import requests from Item.common.read_conf import Readconf from Item.common.path_jion import SplitPath http_method = Readconf( SplitPath().join_confname()).read_value() #获取配置文件中返回的请求方法 from Item.common.wlog import MYLog #----------------------方法1:可以在这直接设置cookie传送------------------------ # url1="http://47.107.168.87:8080/futureloan/mvc/api/member/login" # params1={"mobilephone":"18258148330","pwd":"wx123456"} # res=requests.get(url1,params1) # cookies=res.cookies #-------------------------------------------------------------------------------- class HttpResquest: def __init__(self, urls, params): #定义传递的参数 self.url = urls self.param = params def httprequest(self, cookies): if http_method == "get": #get请求,可以通过配置文件修改 try: res = requests.get(self.url, self.param, cookies=cookies) #发起get请求 except AssertionError as e: print("请求出错{}".format(e))
def __init__(self): self.level=Readconf(SplitPath().join_confname()).read_loglevel()
def setUp(self): #创建写入测试的实例 self.w = Readexcel(SplitPath().join_excelname()) print("-------------开始执行测试用例了------------------")
#@Time :2019/3/1012:08 #@Author :无邪 #@File :test_case.py #@Software:PyCharm import unittest from Item.common.request_http import HttpResquest #引入http请求类 from ddt import ddt, data from Item.common.read_excel import Readexcel #引入excel读取类 from Item.common.path_jion import SplitPath #引入路径切割类 from Item.common.wlog import MYLog #引入日志类 from Item.common.get_data import GetData #引入反射类 from Item.common.work_mysql import MysqlConnect #引入数据库操作类 from Item.common import get_data #导入正则类 #取得用例的值,传递给装饰器data cases = Readexcel(SplitPath().join_excelname()).readcase() # cookiess=None #---------------------------------------------------------------------- @ddt class TestsCases(unittest.TestCase): def setUp(self): #创建写入测试的实例 self.w = Readexcel(SplitPath().join_excelname()) print("-------------开始执行测试用例了------------------") def tearDown(self): print("--------------------测试结束---------------------") @data(*cases)
#@Author :无邪 #@File :runs.py #@Software:PyCharm # -*- utf-8 -*- #@Time :2019/3/917:35 #@Author :无邪 #@File :runs.py #@Software:PyCharm import unittest import sys sys.path.append("./") print(sys.path) import HTMLTestRunnerNew from Item.common import test_case from Item.common.path_jion import SplitPath htmlname = SplitPath().join_myhtml() #报告生成路径 class Testreport: """导入测试用例生成测试报告""" def testresult(self): suit = unittest.TestSuite() #收集测试用例 loader = unittest.TestLoader() # 加载 suit.addTest(loader.loadTestsFromModule(test_case)) #执行用例 with open(htmlname, "wb") as file: runner = HTMLTestRunnerNew.HTMLTestRunner(stream=file, verbosity=2, title="前程贷自动化测试报告", description="我的第一份测试报告", tester="无邪")
def readcase(self): """读取用例""" #获取配置文件中用例执行控制开关button button=Readconf(SplitPath().join_confname()).read_button() if button=="1": mobile=self.get_data()#获取初始化手机号 single_one=[] for i in range(1,self.sheet.max_row):#读取全部用例" single_case={} single_case["Case_id"] = self.sheet.cell(i+1,1).value single_case["module"]=self.sheet.cell(i+1,2).value single_case["url"]=self.sheet.cell(i+1,5).value single_case["Params"] = self.sheet.cell(i + 1, 6).value single_case["sql"] = self.sheet.cell(i + 1, 7).value single_case["ExpectedResult"] = self.sheet.cell(i + 1, 9).value if self.sheet.cell(i+1,6).value.find("tel")!=-1: #如果手机号存在,就替换 newvalue=self.sheet.cell(i+1,6).value.replace('tel',str(mobile)) #-------------------------------------替换参数方法1--------------------------------------------------------- # elif self.sheet.cell(i + 1, 6).value.find("%re_call") != -1: # # 将手机号参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace("%re_call",str(ZhengZ().use_tel())) # elif self.sheet.cell(i + 1, 6).value.find("%re_memberId") != -1: # # 将正常用例的用户id参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace("%re_memberId", str(str(ZhengZ().use_id()))) #----------------------------------------替换参数方法2---------------------------------------------------- # elif self.sheet.cell(i + 1, 6).value.find("call") != -1: # # 将手机号参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace("call", str(getattr(GetData, "call"))) # elif self.sheet.cell(i + 1, 6).value.find("userid") != -1: # # 将正常用例的用户id参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace("userid", str(getattr(GetData, "memberId"))) #--------------------------------------------------------------------------------------------------------- else: # 不替换 newvalue = self.sheet.cell(i + 1, 6).value single_case["Params"] = newvalue single_one.append(single_case) self.update_tel(mobile+1) return single_one else: mobile=self.get_data() single_one = [] for i in eval(button):#选择单个用例 single_case = {} single_case["Case_id"] = self.sheet.cell(i+1,1).value single_case["module"] = self.sheet.cell(i + 1, 2).value single_case["url"] = self.sheet.cell(i+1,5).value single_case["Params"] = self.sheet.cell(i+1,6).value single_case["sql"] = self.sheet.cell(i + 1, 7).value single_case["ExpectedResult"] = self.sheet.cell(i+1,9).value if self.sheet.cell(i+1, 6).value.find("tel") !=-1: # 如果手机号存在,就替换 newvalue =self.sheet.cell(i+1, 6).value.replace("tel",str(mobile)) # -------------------------------------替换参数方法1--------------------------------------------------------- # elif self.sheet.cell(i + 1, 6).value.find("%re_call") != -1: # # 将手机号参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace( # "%re_call", str(ZhengZ().use_tel())) # elif self.sheet.cell(i + 1, 6).value.find("%re_memberId") != -1: # # 将正常用例的用户id参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace( # "%re_memberId", str(str(ZhengZ().use_id()))) # -------------------------------------替换参数方法2--------------------------------------------------------- # elif self.sheet.cell(i + 1, 6).value.find("call")!=-1: # #将手机号参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace("call",str(getattr(GetData,"call"))) # elif self.sheet.cell(i + 1, 6).value.find("userid")!=-1: # #将正常用例的用户id参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace("userid",str(getattr(GetData,"memberId"))) #---------------------------------------------------------------------------------------------------- else: # 不替换 newvalue = self.sheet.cell(i+1, 6).value single_case["Params"]=newvalue single_one.append(single_case) self.update_tel(mobile+ 1) return single_one
newvalue =self.sheet.cell(i+1, 6).value.replace("tel",str(mobile)) # -------------------------------------替换参数方法1--------------------------------------------------------- # elif self.sheet.cell(i + 1, 6).value.find("%re_call") != -1: # # 将手机号参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace( # "%re_call", str(ZhengZ().use_tel())) # elif self.sheet.cell(i + 1, 6).value.find("%re_memberId") != -1: # # 将正常用例的用户id参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace( # "%re_memberId", str(str(ZhengZ().use_id()))) # -------------------------------------替换参数方法2--------------------------------------------------------- # elif self.sheet.cell(i + 1, 6).value.find("call")!=-1: # #将手机号参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace("call",str(getattr(GetData,"call"))) # elif self.sheet.cell(i + 1, 6).value.find("userid")!=-1: # #将正常用例的用户id参数化,从getdata模块中获取 # newvalue = self.sheet.cell(i + 1, 6).value.replace("userid",str(getattr(GetData,"memberId"))) #---------------------------------------------------------------------------------------------------- else: # 不替换 newvalue = self.sheet.cell(i+1, 6).value single_case["Params"]=newvalue single_one.append(single_case) self.update_tel(mobile+ 1) return single_one def writeexcel(self,row,column,value): self.sheet.cell(row,column).value=value self.wb.save(self.filename) if __name__ == '__main__': readexcel=Readexcel(SplitPath().join_excelname()) print(readexcel.readcase())