def test_make_testcase(self): har_path = os.path.join(os.path.dirname(__file__), "data", "demo-quickstart.har") har_parser = HarParser(har_path) testcase = har_parser._make_testcase() testcase_config = testcase[0] self.assertIn("config", testcase_config)
def test_make_testcase_v2(self): har_path = os.path.join(os.path.dirname(__file__), "data", "demo-quickstart.har") har_parser = HarParser(har_path) testcase = har_parser._make_testcase("v2") self.assertIsInstance(testcase, dict) self.assertIn("config", testcase) self.assertIn("teststeps", testcase) self.assertEqual(len(testcase["teststeps"]), 2)
def test_make_testcase_v1(self): har_path = os.path.join(os.path.dirname(__file__), "data", "demo-quickstart.har") har_parser = HarParser(har_path) testcase = har_parser._make_testcase("v1") self.assertIsInstance(testcase, list) self.assertEqual(len(testcase), 3) self.assertIn("config", testcase[0]) self.assertIn("test", testcase[1]) self.assertIn("test", testcase[2])
def upload_file_logic(files, project, module, account): """ 解析yaml或者json用例 :param files: :param project: :param module: :param account: :return: """ is_har = 0 for file in files: file_suffix = os.path.splitext(file)[1].lower() if file_suffix == '.json': with io.open(file, encoding='utf-8') as data_file: try: content = json.load(data_file) except JSONDecodeError: err_msg = u"JSONDecodeError: JSON file format error: {}".format( file) logging.error(err_msg) elif file_suffix in ['.yaml', '.yml']: with io.open(file, 'r', encoding='utf-8') as stream: content = yaml.load(stream) elif file_suffix == '.har': har_parser = HarParser(file) content = har_parser._make_testcase("v1") is_har = 1 version = 1 for test_case in content: test_dict = { 'project': project, 'module': module, 'author': account, 'include': [] } if 'config' in test_case.keys(): test_case.get('config')['config_info'] = test_dict # add_config_data(type=True, **test_case) if 'test' in test_case.keys(): # 忽略config test_case.get('test')['case_info'] = test_dict if 'validate' in test_case.get( 'test').keys(): # 适配validate两种格式 validate = test_case.get('test').pop('validate') new_validate = [] for check in validate: if 'comparator' not in check.keys(): for key, value in check.items(): tmp_check = { "check": value[0], "comparator": key, "expected": value[1] } new_validate.append(tmp_check) test_case.get('test')['validate'] = new_validate '''通过har导入的,如果头信息里面包含apiCode,则将用例名称设置为apiCode''' if is_har == 1: '''验证导入有问题,先不导入''' test_case.get('test')['validate'] = [] req = test_case.get('test').get('request') name = test_case.get('test').get('name') names = name.split('.') suffix = names[len(names) - 1] if suffix == 'js' or suffix == 'css' or suffix == 'svg' or suffix == 'ico' or suffix == 'woff' or suffix == 'png': continue if req is not None: method = req.get('headers').get('method') if method is not None: test_case['test']['name'] = method # 替换用例中名称包含的'/'符号,并追加版本号 if '/' in test_case['test']['name']: test_case['test']['name'] = str( test_case['test']['name']).replace( '/', '-') + '-' + str(version) add_case_data(type=True, **test_case) version = version + 1