refactor: make pytest testcases

This commit is contained in:
debugtalk
2022-06-26 19:44:30 +08:00
parent ccaa0d3361
commit 2caf5d5159
20 changed files with 92 additions and 110 deletions
+5
View File
@@ -11,6 +11,11 @@
- fix: support log debug level for load testing - fix: support log debug level for load testing
- fix: failed to load json/data content in api reference - fix: failed to load json/data content in api reference
- fix: failed to convert postman collection containing multipart/form-data requests to pytest - 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) ## v4.1.4 (2022-06-17)
+1 -3
View File
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.1.4 # NOTE: Generated By HttpRunner v4.1.4
# FROM: a-b.c/1.yml # FROM: a-b.c/1.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseT1(HttpRunner): class TestCaseT1(HttpRunner):
+2 -4
View File
@@ -1,15 +1,13 @@
# NOTE: Generated By HttpRunner v4.1.4 # NOTE: Generated By HttpRunner v4.1.4
# FROM: a-b.c/2 3.yml # FROM: a-b.c/2 3.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import RunTestCase
import sys import sys
from pathlib import Path from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent)) 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 from a_b_c.T1_test import TestCaseT1 as T1
+2 -4
View File
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.0.0 # NOTE: Generated By HttpRunner v4.1.4
# FROM: basic.yml # FROM: basic.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseBasic(HttpRunner): class TestCaseBasic(HttpRunner):
+2 -4
View File
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.0.0 # NOTE: Generated By HttpRunner v4.1.4
# FROM: hooks.yml # FROM: hooks.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseHooks(HttpRunner): class TestCaseHooks(HttpRunner):
+2 -4
View File
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.0.0 # NOTE: Generated By HttpRunner v4.1.4
# FROM: load_image.yml # FROM: load_image.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseLoadImage(HttpRunner): class TestCaseLoadImage(HttpRunner):
+2 -4
View File
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.0.0 # NOTE: Generated By HttpRunner v4.1.4
# FROM: upload.yml # FROM: upload.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseUpload(HttpRunner): class TestCaseUpload(HttpRunner):
+2 -4
View File
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.0.0 # NOTE: Generated By HttpRunner v4.1.4
# FROM: validate.yml # FROM: validate.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseValidate(HttpRunner): class TestCaseValidate(HttpRunner):
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.0.0 # NOTE: Generated By HttpRunner v4.1.4
# FROM: cookie_manipulation/hardcode.yml # FROM: cookie_manipulation/hardcode.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseHardcode(HttpRunner): class TestCaseHardcode(HttpRunner):
@@ -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: cookie_manipulation/set_delete_cookies.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseSetDeleteCookies(HttpRunner): class TestCaseSetDeleteCookies(HttpRunner):
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.1.4 # NOTE: Generated By HttpRunner v4.1.4
# FROM: request_methods/hardcode.yml # FROM: request_methods/hardcode.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseHardcode(HttpRunner): class TestCaseHardcode(HttpRunner):
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.1.4 # NOTE: Generated By HttpRunner v4.1.4
# FROM: request_methods/request_with_functions.yml # FROM: request_methods/request_with_functions.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseRequestWithFunctions(HttpRunner): class TestCaseRequestWithFunctions(HttpRunner):
@@ -1,16 +1,11 @@
# NOTE: Generated By HttpRunner v4.1.4 # NOTE: Generated By HttpRunner v4.1.4
# FROM: request_methods/request_with_parameters.yml # FROM: request_methods/request_with_parameters.yml
import pytest import pytest
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import Parameters from httprunner import Parameters
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseRequestWithParameters(HttpRunner): class TestCaseRequestWithParameters(HttpRunner):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"param", "param",
@@ -1,15 +1,13 @@
# NOTE: Generated By HttpRunner v4.1.4 # NOTE: Generated By HttpRunner v4.1.4
# FROM: request_methods/request_with_testcase_reference.yml # FROM: request_methods/request_with_testcase_reference.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import RunTestCase
import sys import sys
from pathlib import Path from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent)) 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 ( from request_methods.request_with_functions_test import (
TestCaseRequestWithFunctions as RequestWithFunctions, TestCaseRequestWithFunctions as RequestWithFunctions,
) )
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.1.4 # NOTE: Generated By HttpRunner v4.1.4
# FROM: request_methods/request_with_variables.yml # FROM: request_methods/request_with_variables.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseRequestWithVariables(HttpRunner): class TestCaseRequestWithVariables(HttpRunner):
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.1.4 # NOTE: Generated By HttpRunner v4.1.4
# FROM: request_methods/validate_with_functions.yml # FROM: request_methods/validate_with_functions.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseValidateWithFunctions(HttpRunner): class TestCaseValidateWithFunctions(HttpRunner):
@@ -1,8 +1,6 @@
# NOTE: Generated By HttpRunner v4.1.4 # NOTE: Generated By HttpRunner v4.1.4
# FROM: request_methods/validate_with_variables.yml # FROM: request_methods/validate_with_variables.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseValidateWithVariables(HttpRunner): class TestCaseValidateWithVariables(HttpRunner):
-33
View File
@@ -3,7 +3,6 @@ package convert
import ( import (
_ "embed" _ "embed"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@@ -342,35 +341,3 @@ func makeTestCaseFromJSONYAML(iCaseConverter ICaseConverter) (*hrp.TCase, error)
} }
return tCase, nil 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
}
+39
View File
@@ -1 +1,40 @@
package convert 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
}
+21 -15
View File
@@ -36,35 +36,33 @@ __TEMPLATE__ = jinja2.Template(
"""# NOTE: Generated By HttpRunner {{ version }} """# NOTE: Generated By HttpRunner {{ version }}
# FROM: {{ testcase_path }} # FROM: {{ testcase_path }}
{% if imports_list and diff_levels > 0 %} {%- if parameters or skip %}
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 %}
import pytest import pytest
{% endif %} {% endif %}
from httprunner import HttpRunner, Config, Step, RunRequest
{% if parameters %} {%- if parameters %}
from httprunner import Parameters from httprunner import Parameters
{% endif %} {%- endif %}
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase {%- if reference_testcase %}
{% for import_str in imports_list %} from httprunner import RunTestCase
{%- endif %}
{%- for import_str in imports_list %}
{{ import_str }} {{ import_str }}
{% endfor %} {%- endfor %}
class {{ class_name }}(HttpRunner): class {{ class_name }}(HttpRunner):
{% if parameters and skip %} {% if parameters and skip %}
@pytest.mark.parametrize("param", Parameters({{parameters}})) @pytest.mark.parametrize("param", Parameters({{ parameters }}))
@pytest.mark.skip(reason={{ skip }}) @pytest.mark.skip(reason={{ skip }})
def test_start(self, param): def test_start(self, param):
super().test_start(param) super().test_start(param)
{% elif parameters %} {% elif parameters %}
@pytest.mark.parametrize("param", Parameters({{parameters}})) @pytest.mark.parametrize("param", Parameters({{ parameters }}))
def test_start(self, param): def test_start(self, param):
super().test_start(param) super().test_start(param)
@@ -429,16 +427,24 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
testcase_path = convert_relative_project_root_dir(testcase_abs_path) testcase_path = convert_relative_project_root_dir(testcase_abs_path)
# current file compared to ProjectRootDir # current file compared to ProjectRootDir
diff_levels = len(testcase_path.split(os.sep)) 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 = { data = {
"version": __version__, "version": __version__,
"testcase_path": testcase_path, "testcase_path": testcase_path,
"diff_levels": diff_levels,
"class_name": f"TestCase{testcase_cls_name}", "class_name": f"TestCase{testcase_cls_name}",
"imports_list": imports_list, "imports_list": imports_list,
"config_chain_style": make_config_chain_style(config), "config_chain_style": make_config_chain_style(config),
"skip": make_config_skip(config), "skip": make_config_skip(config),
"parameters": config.get("parameters"), "parameters": config.get("parameters"),
"reference_testcase": any(step.get("testcase") for step in teststeps),
"teststeps_chain_style": [ "teststeps_chain_style": [
make_teststep_chain_style(step) for step in teststeps make_teststep_chain_style(step) for step in teststeps
], ],