def main(): parser = argparse.ArgumentParser( description= "Convert postman testcases to JSON testcases for HttpRunner.") parser.add_argument("-V", "--version", dest='version', action='store_true', help="show version") parser.add_argument('--log-level', default='INFO', help="Specify logging level, default is INFO.") parser.add_argument('postman_testset_file', nargs='?', help="Specify postman testset file.") parser.add_argument( 'output_testset_file', nargs='?', help="Optional. Specify converted YAML/JSON testset file.") args = parser.parse_args() if args.version: print("{}".format(__version__)) exit(0) log_level = getattr(logging, args.log_level.upper()) logging.basicConfig(level=log_level) postman_testset_file = args.postman_testset_file output_testset_file = args.output_testset_file if not postman_testset_file or not postman_testset_file.endswith(".json"): logging.error("postman_testset_file file not specified.") sys.exit(1) output_file_type = "JSON" if not output_testset_file: postman_file = os.path.splitext(postman_testset_file)[0] output_testset_file = "{}.{}.{}".format(postman_file, "output", output_file_type.lower()) else: output_file_suffix = os.path.splitext(output_testset_file)[1] if output_file_suffix in [".json"]: output_file_type = "JSON" else: logging.error("Converted file could only be in JSON format.") sys.exit(1) postman_parser = PostmanParser(postman_testset_file) postman_parser.gen_json(output_testset_file) return 0
def main(): parser = argparse.ArgumentParser( description="Convert postman testcases to JSON testcases for HttpRunner.") parser.add_argument("-V", "--version", dest='version', action='store_true', help="show version") parser.add_argument('--log-level', default='INFO', help="Specify logging level, default is INFO.") parser.add_argument('postman_testset_file', nargs='?', help="Specify postman testset file.") parser.add_argument('--output_file_type', nargs='?', help="Optional. Specify output file type.") parser.add_argument('--output_dir', nargs='?', help="Optional. Specify output directory.") args = parser.parse_args() if args.version: print("{}".format(__version__)) exit(0) log_level = getattr(logging, args.log_level.upper()) logging.basicConfig(level=log_level) postman_testset_file = args.postman_testset_file output_file_type = args.output_file_type output_dir = args.output_dir if not postman_testset_file or not postman_testset_file.endswith(".json"): logging.error("postman_testset_file file not specified.") sys.exit(1) if not output_file_type: output_file_type = "json" else: output_file_type = output_file_type.lower() if output_file_type not in ["json", "yml", "yaml"]: logging.error("output file only support json/yml/yaml.") sys.exit(1) if not output_dir: output_dir = '.' postman_parser = PostmanParser(postman_testset_file) parse_result = postman_parser.parse_data() postman_parser.save(parse_result, output_dir, output_file_type=output_file_type) return 0
def setUp(self): self.postman_parser = PostmanParser("tests/data/test.json")
class TestParser(unittest.TestCase): def setUp(self): self.postman_parser = PostmanParser("tests/data/test.json") def test_init(self): self.assertEqual(self.postman_parser.postman_testcase_file, "tests/data/test.json") def test_read_postman_data(self): with open("tests/data/test.json", encoding='utf-8', mode='r') as f: content = json.load(f) other_content = self.postman_parser.read_postman_data() self.assertEqual(content, other_content) def test_parse_each_item_get(self): with open("tests/data/test_get.json", encoding='utf-8', mode='r') as f: item = json.load(f) result = { "name": "test_get", "def": "test_get", "validate": [], "variables": [{ "search": "345" }], "request": { "method": "GET", "url": "http://www.baidu.com", "headers": {}, "params": { "search": "$search" } } } fun_result = self.postman_parser.parse_each_item(item) self.assertEqual(result, fun_result) def test_parse_each_item_post(self): with open("tests/data/test_post.json", encoding='utf-8', mode='r') as f: item = json.load(f) result = { "name": "test_post", "def": "test_post", "validate": [], "variables": [{ "search": "123" }], "request": { "method": "POST", "url": "http://www.baidu.com", "headers": {}, "json": { "search": "$search" } } } fun_result = self.postman_parser.parse_each_item(item) self.assertEqual(result, fun_result) def test_gen_json(self): output_file = "tests/data/output.json" self.postman_parser.gen_json(output_file) os.remove(output_file)
class TestParser(unittest.TestCase): def setUp(self): self.postman_parser = PostmanParser("tests/data/test.json") def test_init(self): self.assertEqual(self.postman_parser.postman_testcase_file, "tests/data/test.json") def test_read_postman_data(self): with open("tests/data/test.json", encoding='utf-8', mode='r') as f: content = json.load(f) other_content = self.postman_parser.read_postman_data() self.assertEqual(content, other_content) def test_parse_url(self): request_url = { "raw": "https://postman-echo.com/get?foo1=bar1&foo2=bar2", "protocol": "https", "host": ["postman-echo", "com"], "path": ["get"], "query": [{ "key": "foo1", "value": "bar1" }, { "key": "foo2", "value": "bar2" }] } url = self.postman_parser.parse_url(request_url) self.assertEqual(url, request_url["raw"]) request_url = "https://postman-echo.com/get?foo1=bar1&foo2=bar2" url = self.postman_parser.parse_url(request_url) self.assertEqual(url, request_url) def test_parse_header(self): request_header = [{ "key": "Content-Type", "name": "Content-Type", "value": "application/json", "type": "text" }] target_header = {"Content-Type": "application/json"} header = self.postman_parser.parse_header(request_header) self.assertEqual(header, target_header) header = self.postman_parser.parse_header([]) self.assertEqual(header, {}) def test_parse_each_item_get(self): with open("tests/data/test_get.json", encoding='utf-8', mode='r') as f: item = json.load(f) result = { "name": "test_get", "validate": [], "variables": [{ "search": "345" }], "request": { "method": "GET", "url": "http://www.baidu.com", "headers": {}, "params": { "search": "$search" } } } fun_result = self.postman_parser.parse_each_item(item) self.assertEqual(result, fun_result) def test_parse_each_item_post(self): with open("tests/data/test_post.json", encoding='utf-8', mode='r') as f: item = json.load(f) result = { "name": "test_post", "validate": [], "variables": [{ "search": "123" }], "request": { "method": "POST", "url": "http://www.baidu.com", "headers": {}, "data": { "search": "$search" } } } fun_result = self.postman_parser.parse_each_item(item) self.assertEqual(result, fun_result) def test_parse_data(self): result = self.postman_parser.parse_data() self.assertEqual(len(result), 21) def test_save(self): result = self.postman_parser.parse_data() self.postman_parser.save(result, "save") shutil.rmtree("save")
class TestParser(unittest.TestCase): def setUp(self): self.postman_parser = PostmanParser("tests/data/test.json") def test_init(self): self.assertEqual(self.postman_parser.postman_testcase_file, "tests/data/test.json") def test_read_postman_data(self): with open("tests/data/test.json") as f: content = json.load(f) other_content = self.postman_parser.read_postman_data() self.assertEqual(content, other_content) def test_parse_each_item_get(self): item = { "name": "test_get", "request": { "url": { "raw": "http://www.baidu.com?search=345", "protocol": "http", "host": [ "www", "baidu", "com" ], "query": [ { "key": "search", "value": "345", "equals": True, "description": "" } ], "variable": [] }, "method": "GET", "header": [], "body": {}, "description": "" }, "response": [] } result = { "name": "test_get", "def": "test_get", "validate": [], "request": { "method": "GET", "url": "http://www.baidu.com", "headers": {}, "params": { "search": "345" } } } fun_result = self.postman_parser.parse_each_item(item) self.assertEqual(result, fun_result) def test_parse_each_item_post(self): item = { "name": "test_post", "request": { "url": "http://www.baidu.com", "method": "POST", "header": [], "body": { "mode": "formdata", "formdata": [ { "key": "search", "value": "123", "description": "", "type": "text" } ] }, "description": "" }, "response": [] } result = { "name": "test_post", "def": "test_post", "validate": [], "request": { "method": "POST", "url": "http://www.baidu.com", "headers": {}, "json": { "search": "123" } } } fun_result = self.postman_parser.parse_each_item(item) self.assertEqual(result, fun_result) def test_gen_json(self): output_file = "tests/data/output.json" self.postman_parser.gen_json(output_file) os.remove(output_file)