diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 7dc389dc..0bda2b7d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -11,6 +11,11 @@ - fix: support log debug level for load testing - fix: failed to load json/data content in api reference - fix: failed to convert postman collection containing multipart/form-data requests to pytest +- fix: only get the first parameter in referenced testcase + +**python version** + +- refactor: make pytest testcases ## v4.1.4 (2022-06-17) diff --git a/examples/data/a_b_c/T1_test.py b/examples/data/a_b_c/T1_test.py index 66827eb8..6caf90b1 100644 --- a/examples/data/a_b_c/T1_test.py +++ b/examples/data/a_b_c/T1_test.py @@ -1,8 +1,6 @@ # NOTE: Generated By HttpRunner v4.1.4 # FROM: a-b.c/1.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseT1(HttpRunner): diff --git a/examples/data/a_b_c/T2_3_test.py b/examples/data/a_b_c/T2_3_test.py index ee738e1a..3a4bb701 100644 --- a/examples/data/a_b_c/T2_3_test.py +++ b/examples/data/a_b_c/T2_3_test.py @@ -1,15 +1,13 @@ # NOTE: Generated By HttpRunner v4.1.4 # FROM: a-b.c/2 3.yml - +from httprunner import HttpRunner, Config, Step, RunRequest +from httprunner import RunTestCase import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent.parent)) - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase - from a_b_c.T1_test import TestCaseT1 as T1 diff --git a/examples/httpbin/basic_test.py b/examples/httpbin/basic_test.py index 42733b08..274ab8e0 100644 --- a/examples/httpbin/basic_test.py +++ b/examples/httpbin/basic_test.py @@ -1,8 +1,6 @@ -# NOTE: Generated By HttpRunner v4.0.0 +# NOTE: Generated By HttpRunner v4.1.4 # FROM: basic.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseBasic(HttpRunner): diff --git a/examples/httpbin/hooks_test.py b/examples/httpbin/hooks_test.py index cb249adb..e0d9d951 100644 --- a/examples/httpbin/hooks_test.py +++ b/examples/httpbin/hooks_test.py @@ -1,8 +1,6 @@ -# NOTE: Generated By HttpRunner v4.0.0 +# NOTE: Generated By HttpRunner v4.1.4 # FROM: hooks.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseHooks(HttpRunner): diff --git a/examples/httpbin/load_image_test.py b/examples/httpbin/load_image_test.py index 9c56466c..b614c4b5 100644 --- a/examples/httpbin/load_image_test.py +++ b/examples/httpbin/load_image_test.py @@ -1,8 +1,6 @@ -# NOTE: Generated By HttpRunner v4.0.0 +# NOTE: Generated By HttpRunner v4.1.4 # FROM: load_image.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseLoadImage(HttpRunner): diff --git a/examples/httpbin/upload_test.py b/examples/httpbin/upload_test.py index 56d2c1c8..c48fa2fd 100644 --- a/examples/httpbin/upload_test.py +++ b/examples/httpbin/upload_test.py @@ -1,8 +1,6 @@ -# NOTE: Generated By HttpRunner v4.0.0 +# NOTE: Generated By HttpRunner v4.1.4 # FROM: upload.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseUpload(HttpRunner): diff --git a/examples/httpbin/validate_test.py b/examples/httpbin/validate_test.py index 2a3e3e0f..18f38d2e 100644 --- a/examples/httpbin/validate_test.py +++ b/examples/httpbin/validate_test.py @@ -1,8 +1,6 @@ -# NOTE: Generated By HttpRunner v4.0.0 +# NOTE: Generated By HttpRunner v4.1.4 # FROM: validate.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseValidate(HttpRunner): diff --git a/examples/postman_echo/cookie_manipulation/hardcode_test.py b/examples/postman_echo/cookie_manipulation/hardcode_test.py index efc33f10..d6d08ffc 100644 --- a/examples/postman_echo/cookie_manipulation/hardcode_test.py +++ b/examples/postman_echo/cookie_manipulation/hardcode_test.py @@ -1,8 +1,6 @@ -# NOTE: Generated By HttpRunner v4.0.0 +# NOTE: Generated By HttpRunner v4.1.4 # FROM: cookie_manipulation/hardcode.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseHardcode(HttpRunner): diff --git a/examples/postman_echo/cookie_manipulation/set_delete_cookies_test.py b/examples/postman_echo/cookie_manipulation/set_delete_cookies_test.py index 73d9928d..ea9abbc3 100644 --- a/examples/postman_echo/cookie_manipulation/set_delete_cookies_test.py +++ b/examples/postman_echo/cookie_manipulation/set_delete_cookies_test.py @@ -1,8 +1,6 @@ -# NOTE: Generated By HttpRunner v4.0.0 +# NOTE: Generated By HttpRunner v4.1.4 # FROM: cookie_manipulation/set_delete_cookies.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseSetDeleteCookies(HttpRunner): diff --git a/examples/postman_echo/request_methods/hardcode_test.py b/examples/postman_echo/request_methods/hardcode_test.py index c12f31e0..ba0702a4 100644 --- a/examples/postman_echo/request_methods/hardcode_test.py +++ b/examples/postman_echo/request_methods/hardcode_test.py @@ -1,8 +1,6 @@ # NOTE: Generated By HttpRunner v4.1.4 # FROM: request_methods/hardcode.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseHardcode(HttpRunner): diff --git a/examples/postman_echo/request_methods/request_with_functions_test.py b/examples/postman_echo/request_methods/request_with_functions_test.py index a836d6bf..54afbef1 100644 --- a/examples/postman_echo/request_methods/request_with_functions_test.py +++ b/examples/postman_echo/request_methods/request_with_functions_test.py @@ -1,8 +1,6 @@ # NOTE: Generated By HttpRunner v4.1.4 # FROM: request_methods/request_with_functions.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseRequestWithFunctions(HttpRunner): diff --git a/examples/postman_echo/request_methods/request_with_parameters_test.py b/examples/postman_echo/request_methods/request_with_parameters_test.py index 0ef26f47..120f8d58 100644 --- a/examples/postman_echo/request_methods/request_with_parameters_test.py +++ b/examples/postman_echo/request_methods/request_with_parameters_test.py @@ -1,16 +1,11 @@ # NOTE: Generated By HttpRunner v4.1.4 # FROM: request_methods/request_with_parameters.yml - - import pytest - +from httprunner import HttpRunner, Config, Step, RunRequest from httprunner import Parameters -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase - - class TestCaseRequestWithParameters(HttpRunner): @pytest.mark.parametrize( "param", diff --git a/examples/postman_echo/request_methods/request_with_testcase_reference_test.py b/examples/postman_echo/request_methods/request_with_testcase_reference_test.py index 7763a434..34c406e1 100644 --- a/examples/postman_echo/request_methods/request_with_testcase_reference_test.py +++ b/examples/postman_echo/request_methods/request_with_testcase_reference_test.py @@ -1,15 +1,13 @@ # NOTE: Generated By HttpRunner v4.1.4 # FROM: request_methods/request_with_testcase_reference.yml - +from httprunner import HttpRunner, Config, Step, RunRequest +from httprunner import RunTestCase import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent.parent)) - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase - from request_methods.request_with_functions_test import ( TestCaseRequestWithFunctions as RequestWithFunctions, ) diff --git a/examples/postman_echo/request_methods/request_with_variables_test.py b/examples/postman_echo/request_methods/request_with_variables_test.py index 732feaec..9834cb4d 100644 --- a/examples/postman_echo/request_methods/request_with_variables_test.py +++ b/examples/postman_echo/request_methods/request_with_variables_test.py @@ -1,8 +1,6 @@ # NOTE: Generated By HttpRunner v4.1.4 # FROM: request_methods/request_with_variables.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseRequestWithVariables(HttpRunner): diff --git a/examples/postman_echo/request_methods/validate_with_functions_test.py b/examples/postman_echo/request_methods/validate_with_functions_test.py index fd6ffaaa..fa906fe4 100644 --- a/examples/postman_echo/request_methods/validate_with_functions_test.py +++ b/examples/postman_echo/request_methods/validate_with_functions_test.py @@ -1,8 +1,6 @@ # NOTE: Generated By HttpRunner v4.1.4 # FROM: request_methods/validate_with_functions.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseValidateWithFunctions(HttpRunner): diff --git a/examples/postman_echo/request_methods/validate_with_variables_test.py b/examples/postman_echo/request_methods/validate_with_variables_test.py index 05a31462..275be39f 100644 --- a/examples/postman_echo/request_methods/validate_with_variables_test.py +++ b/examples/postman_echo/request_methods/validate_with_variables_test.py @@ -1,8 +1,6 @@ # NOTE: Generated By HttpRunner v4.1.4 # FROM: request_methods/validate_with_variables.yml - - -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +from httprunner import HttpRunner, Config, Step, RunRequest class TestCaseValidateWithVariables(HttpRunner): diff --git a/hrp/internal/convert/converter.go b/hrp/internal/convert/converter.go index 63e07690..107afa15 100644 --- a/hrp/internal/convert/converter.go +++ b/hrp/internal/convert/converter.go @@ -3,7 +3,6 @@ package convert import ( _ "embed" "fmt" - "os" "path/filepath" "reflect" @@ -342,35 +341,3 @@ func makeTestCaseFromJSONYAML(iCaseConverter ICaseConverter) (*hrp.TCase, error) } return tCase, nil } - -func convertToPyTest(iCaseConverter ICaseConverter) (string, error) { - // convert to temporary json testcase - jsonPath, err := iCaseConverter.ToJSON() - inputType := iCaseConverter.Struct().InputType - if err != nil { - return "", errors.Wrapf(err, "(%s -> pytest step 1) failed to convert to temporary json testcase", inputType.String()) - } - defer func() { - if jsonPath != "" { - if err = os.Remove(jsonPath); err != nil { - log.Error().Err(err).Msgf("(%s -> pytest step defer) failed to clean temporary json testcase", inputType.String()) - } - } - }() - - // convert from temporary json testcase to pytest - converterJSON := NewConverterJSON(NewTCaseConverter(jsonPath)) - pyTestPath, err := converterJSON.MakePyTestScript() - if err != nil { - return "", errors.Wrap(err, "(json -> pytest step 2) failed to convert from temporary json testcase to pytest ") - } - - // rename resultant pytest - renamedPyTestPath := iCaseConverter.Struct().genOutputPath(suffixPyTest) - err = os.Rename(pyTestPath, renamedPyTestPath) - if err != nil { - log.Error().Err(err).Msg("(json -> pytest step 3) failed to rename the resultant pytest file") - return pyTestPath, nil - } - return renamedPyTestPath, nil -} diff --git a/hrp/internal/convert/converter_pytest.go b/hrp/internal/convert/converter_pytest.go index 233bcded..c2edca2c 100644 --- a/hrp/internal/convert/converter_pytest.go +++ b/hrp/internal/convert/converter_pytest.go @@ -1 +1,40 @@ package convert + +import ( + "os" + + "github.com/pkg/errors" + "github.com/rs/zerolog/log" +) + +func convertToPyTest(iCaseConverter ICaseConverter) (string, error) { + // convert to temporary json testcase + jsonPath, err := iCaseConverter.ToJSON() + inputType := iCaseConverter.Struct().InputType + if err != nil { + return "", errors.Wrapf(err, "(%s -> pytest step 1) failed to convert to temporary json testcase", inputType.String()) + } + defer func() { + if jsonPath != "" { + if err = os.Remove(jsonPath); err != nil { + log.Error().Err(err).Msgf("(%s -> pytest step defer) failed to clean temporary json testcase", inputType.String()) + } + } + }() + + // convert from temporary json testcase to pytest + converterJSON := NewConverterJSON(NewTCaseConverter(jsonPath)) + pyTestPath, err := converterJSON.MakePyTestScript() + if err != nil { + return "", errors.Wrap(err, "(json -> pytest step 2) failed to convert from temporary json testcase to pytest ") + } + + // rename resultant pytest + renamedPyTestPath := iCaseConverter.Struct().genOutputPath(suffixPyTest) + err = os.Rename(pyTestPath, renamedPyTestPath) + if err != nil { + log.Error().Err(err).Msg("(json -> pytest step 3) failed to rename the resultant pytest file") + return pyTestPath, nil + } + return renamedPyTestPath, nil +} diff --git a/httprunner/make.py b/httprunner/make.py index 5dc2b506..24d4b508 100644 --- a/httprunner/make.py +++ b/httprunner/make.py @@ -36,38 +36,36 @@ __TEMPLATE__ = jinja2.Template( """# NOTE: Generated By HttpRunner {{ version }} # FROM: {{ testcase_path }} -{% if imports_list and diff_levels > 0 %} -import sys -from pathlib import Path -sys.path.insert(0, str(Path(__file__){% for _ in range(diff_levels) %}.parent{% endfor %})) -{% endif %} - -{% if parameters or skip %} +{%- if parameters or skip %} import pytest {% endif %} +from httprunner import HttpRunner, Config, Step, RunRequest -{% if parameters %} +{%- if parameters %} from httprunner import Parameters -{% endif %} +{%- endif %} -from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase -{% for import_str in imports_list %} +{%- if reference_testcase %} +from httprunner import RunTestCase +{%- endif %} + +{%- for import_str in imports_list %} {{ import_str }} -{% endfor %} +{%- endfor %} class {{ class_name }}(HttpRunner): {% if parameters and skip %} - @pytest.mark.parametrize("param", Parameters({{parameters}})) + @pytest.mark.parametrize("param", Parameters({{ parameters }})) @pytest.mark.skip(reason={{ skip }}) def test_start(self, param): super().test_start(param) - + {% elif parameters %} - @pytest.mark.parametrize("param", Parameters({{parameters}})) + @pytest.mark.parametrize("param", Parameters({{ parameters }})) def test_start(self, param): super().test_start(param) - + {% elif skip %} @pytest.mark.skip(reason={{ skip }}) def test_start(self): @@ -429,16 +427,24 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text: testcase_path = convert_relative_project_root_dir(testcase_abs_path) # current file compared to ProjectRootDir diff_levels = len(testcase_path.split(os.sep)) + if len(imports_list) > 0 and diff_levels > 0: + parent = ".parent" * diff_levels + import_deps = f""" +import sys +from pathlib import Path +sys.path.insert(0, str(Path(__file__){parent})) +""" + imports_list.insert(0, import_deps) data = { "version": __version__, "testcase_path": testcase_path, - "diff_levels": diff_levels, "class_name": f"TestCase{testcase_cls_name}", "imports_list": imports_list, "config_chain_style": make_config_chain_style(config), "skip": make_config_skip(config), "parameters": config.get("parameters"), + "reference_testcase": any(step.get("testcase") for step in teststeps), "teststeps_chain_style": [ make_teststep_chain_style(step) for step in teststeps ],