mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-17 15:27:35 +08:00
refactor: make pytest testcases
This commit is contained in:
@@ -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
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user