class ApiRequests: def __init__(self): self.log = MyLogs() def api_request(self, http_method, api_url, api_header, param, file=None): if http_method.upper() == "POST": try: self.log.info("正在进行post请求") res = requests.post(api_url, json=param, headers=api_header, files=file) except BaseException as e: self.log.error("post请求出现异常") raise e elif http_method.upper() == "GET": try: self.log.info("正在进行get请求") res = requests.get(api_url, json=param, headers=api_header) except BaseException as e: self.log.error("get请求出现异常") raise e else: self.log.error("请求方式不正确") return res
def read_config(self, filename, section, option): cf = configparser.ConfigParser() try: cf.read(filename) except Exception as e: MyLogs().error("打开配置文件异常:{0}".format(e)) else: try: res = cf.get(section, option) except Exception as e: MyLogs().error("读取配置文件失败{0}".format(e)) return res
def data_re(self, pattern, string, object): for i in range(20): if re.search(pattern, string): res = re.search(pattern, string) key = res.group(0) value = res.group(1) try: MyLogs().info("正在获取动态参数:{0}获取的参数是:{1}".format( key, str(getattr(object, value)))) string = string.replace(key, str(getattr(object, value))) except Exception as e: MyLogs().error("参数替换失败{0}".format(e)) else: string = string return string
def setUpClass(cls): cls.log = MyLogs() cls.read_data = ReadDatas(DataPaths.DatasPath) cls.re = DataRe() cls.api = ApiRequests()
def __init__(self): self.log = MyLogs()
def output(title, url, header, param, expected): MyLogs().info("测试标题是:{0}".format(title)) MyLogs().info("测试的地址是:{0}".format(url)) MyLogs().info("测试的header是:{0}".format(header)) MyLogs().info("测试的参数是:{0}".format(param)) MyLogs().info("预期结果是:{0}".format(expected))
# @Author :Tester_Liang # @Email :[email protected] # @File :run.py # @software :PyCharm from BusProject.Linebus_Platform_API import HTMLTestRunnerNew from BusProject.Linebus_Platform_API.common import DataPaths from BusProject.Linebus_Platform_API.common.suite_case import SuiteCase from BusProject.Linebus_Platform_API.common.read_config import Read_Config from BusProject.Linebus_Platform_API.common.MyLog import MyLogs with open(DataPaths.ReportPath, 'wb+') as file: button = Read_Config().read_config(DataPaths.ConfigPath, 'CASE', 'switch') caselist = eval(Read_Config().read_config(DataPaths.ConfigPath, 'CASE', 'caselist')) casedir = Read_Config().read_config(DataPaths.ConfigPath, 'DIR', 'switch') if casedir.upper() == "ALL": casepath = DataPaths.CasePath else: casepath = DataPaths.CasePath + '/{0}'.format(casedir) suite = SuiteCase().suitecase(casepath, button, caselist) runner = HTMLTestRunnerNew.HTMLTestRunner(file, verbosity=2, title="Linebus_Platform_API", description="2019/5/28", tester='Tester_yin') try: runner.run(suite) except Exception as e: MyLogs().error("执行用例失败:{0}".format(e))