From 9b2a06a9851b248684973b375120f9a66d72d478 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 17 May 2020 10:44:46 +0800 Subject: [PATCH] fix: convert_testcase_path --- httprunner/cli.py | 2 +- httprunner/ext/har2case/core_test.py | 6 ++-- httprunner/ext/make/__init__.py | 51 ++++++++++++++-------------- httprunner/ext/make/make_test.py | 28 ++++++++++++++- 4 files changed, 57 insertions(+), 30 deletions(-) diff --git a/httprunner/cli.py b/httprunner/cli.py index f577e77a..34d9d0ff 100644 --- a/httprunner/cli.py +++ b/httprunner/cli.py @@ -25,7 +25,7 @@ def main_run(extra_args): continue elif os.path.isfile(item): # replace YAML/JSON file path with generated python file - extra_args[index] = convert_testcase_path(item) + extra_args[index], _ = convert_testcase_path(item) tests_path_list.append(item) diff --git a/httprunner/ext/har2case/core_test.py b/httprunner/ext/har2case/core_test.py index 25fa60c4..22ec06e4 100644 --- a/httprunner/ext/har2case/core_test.py +++ b/httprunner/ext/har2case/core_test.py @@ -22,9 +22,9 @@ class TestHar(TestUtils): for validator in teststep_dict["validate"] } self.assertEqual(validators_mapping["status_code"], 200) - self.assertEqual(validators_mapping["content.IsSuccess"], True) - self.assertEqual(validators_mapping["content.Code"], 200) - self.assertEqual(validators_mapping["content.Message"], None) + self.assertEqual(validators_mapping["body.IsSuccess"], True) + self.assertEqual(validators_mapping["body.Code"], 200) + self.assertEqual(validators_mapping["body.Message"], None) def test_prepare_teststeps(self): teststeps = self.har_parser._prepare_teststeps() diff --git a/httprunner/ext/make/__init__.py b/httprunner/ext/make/__init__.py index 3fc6ca4a..d899488c 100644 --- a/httprunner/ext/make/__init__.py +++ b/httprunner/ext/make/__init__.py @@ -1,6 +1,6 @@ import os import subprocess -from typing import Union, Text, List +from typing import Union, Text, List, Tuple import jinja2 from loguru import logger @@ -28,6 +28,28 @@ if __name__ == "__main__": """ +def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]: + """convert single YAML/JSON testcase path to python file""" + if os.path.isdir(testcase_path): + # folder does not need to convert + return testcase_path, "" + + raw_file_name, file_suffix = os.path.splitext(os.path.basename(testcase_path)) + + file_suffix = file_suffix.lower() + if file_suffix not in [".json", ".yml", ".yaml"]: + raise exceptions.ParamsError("") + + file_name = raw_file_name.replace(" ", "_").replace(".", "_") + testcase_dir = os.path.dirname(testcase_path) + testcase_python_path = os.path.join(testcase_dir, f"{file_name}_test.py") + + # convert title case, e.g. request_with_variables => RequestWithVariables + name_in_title_case = file_name.title().replace("_", "") + + return testcase_python_path, name_in_title_case + + def make_testcase(testcase_path: str) -> Union[str, None]: logger.info(f"start to make testcase: {testcase_path}") try: @@ -37,12 +59,7 @@ def make_testcase(testcase_path: str) -> Union[str, None]: template = jinja2.Template(__TMPL__) - raw_file_name, _ = os.path.splitext(os.path.basename(testcase_path)) - # convert title case, e.g. request_with_variables => RequestWithVariables - name_in_title_case = raw_file_name.replace(".", " ").title().replace("_", "").replace(" ", "") - - testcase_dir = os.path.dirname(testcase_path) - testcase_python_path = os.path.join(testcase_dir, f"{raw_file_name}_test.py") + testcase_python_path, name_in_title_case = convert_testcase_path(testcase_path) config = testcase["config"] config["path"] = testcase_python_path @@ -60,26 +77,10 @@ def make_testcase(testcase_path: str) -> Union[str, None]: return testcase_python_path -def convert_testcase_path(testcase_path: Text) -> Text: - """convert single YAML/JSON testcase path to python file""" - if os.path.isdir(testcase_path): - # folder does not need to convert - return testcase_path - - file_suffix = os.path.splitext(testcase_path)[1].lower() - if file_suffix == ".json": - return testcase_path.replace(".json", "_test.py") - elif file_suffix == ".yaml": - return testcase_path.replace(".yaml", "_test.py") - elif file_suffix == ".yml": - return testcase_path.replace(".yml", "_test.py") - else: - raise exceptions.ParamsError("") - - def format_with_black(tests_path: Text): logger.info("format testcases with black ...") - tests_path = convert_testcase_path(tests_path) + tests_path, _ = convert_testcase_path(tests_path) + try: subprocess.run(["black", tests_path]) except subprocess.CalledProcessError as ex: diff --git a/httprunner/ext/make/make_test.py b/httprunner/ext/make/make_test.py index 14458bf0..0b6dac26 100644 --- a/httprunner/ext/make/make_test.py +++ b/httprunner/ext/make/make_test.py @@ -1,5 +1,5 @@ import unittest -from httprunner.ext.make import make_testcase, main_make +from httprunner.ext.make import make_testcase, main_make, convert_testcase_path class TestLoader(unittest.TestCase): @@ -18,3 +18,29 @@ class TestLoader(unittest.TestCase): "examples/postman_echo/request_methods/request_with_functions_test.py", testcase_python_list, ) + + def test_convert_testcase_path(self): + self.assertEqual( + convert_testcase_path("mubu.login.yml")[0], + "mubu_login_test.py" + ) + self.assertEqual( + convert_testcase_path("mubu login.yml")[0], + "mubu_login_test.py" + ) + self.assertEqual( + convert_testcase_path("/path/to/mubu.login.yml")[0], + "/path/to/mubu_login_test.py" + ) + self.assertEqual( + convert_testcase_path("/path/to 2/mubu.login.yml")[0], + "/path/to 2/mubu_login_test.py" + ) + self.assertEqual( + convert_testcase_path("/path/to 2/mubu.login.yml")[1], + "MubuLogin" + ) + self.assertEqual( + convert_testcase_path("/path/to 2/mubu login.yml")[1], + "MubuLogin" + )