refactor: make pytest testcases

This commit is contained in:
debugtalk
2022-06-26 13:21:25 +08:00
parent ccaa0d3361
commit 2caf5d5159
20 changed files with 92 additions and 110 deletions

View File

@@ -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)

View File

@@ -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):

View File

@@ -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

View File

@@ -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):

View File

@@ -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):

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 httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseLoadImage(HttpRunner):

View File

@@ -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):

View File

@@ -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):

View File

@@ -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):

View File

@@ -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):

View File

@@ -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):

View File

@@ -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):

View File

@@ -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",

View File

@@ -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,
)

View File

@@ -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):

View File

@@ -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):

View File

@@ -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):

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
],