mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-13 08:59:44 +08:00
change: rename StepData to StepResult
This commit is contained in:
@@ -12,13 +12,13 @@ from httprunner.utils import get_platform, ExtendJSONEncoder
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def session_fixture(request):
|
||||
"""setup and teardown each task"""
|
||||
logger.info(f"start running testcases ...")
|
||||
logger.info("start running testcases ...")
|
||||
|
||||
start_at = time.time()
|
||||
|
||||
yield
|
||||
|
||||
logger.info(f"task finished, generate task summary for --save-tests")
|
||||
logger.info("task finished, generate task summary for --save-tests")
|
||||
|
||||
summary = {
|
||||
"success": True,
|
||||
@@ -36,24 +36,27 @@ def session_fixture(request):
|
||||
summary["success"] &= testcase_summary.success
|
||||
|
||||
summary["stat"]["testcases"]["total"] += 1
|
||||
summary["stat"]["teststeps"]["total"] += len(testcase_summary.step_datas)
|
||||
summary["stat"]["teststeps"]["total"] += len(testcase_summary.step_results)
|
||||
if testcase_summary.success:
|
||||
summary["stat"]["testcases"]["success"] += 1
|
||||
summary["stat"]["teststeps"]["successes"] += len(
|
||||
testcase_summary.step_datas
|
||||
testcase_summary.step_results
|
||||
)
|
||||
else:
|
||||
summary["stat"]["testcases"]["fail"] += 1
|
||||
summary["stat"]["teststeps"]["successes"] += (
|
||||
len(testcase_summary.step_datas) - 1
|
||||
len(testcase_summary.step_results) - 1
|
||||
)
|
||||
summary["stat"]["teststeps"]["failures"] += 1
|
||||
|
||||
testcase_summary_json = testcase_summary.dict()
|
||||
testcase_summary_json["records"] = testcase_summary_json.pop("step_datas")
|
||||
testcase_summary_json["records"] = testcase_summary_json.pop("step_results")
|
||||
summary["details"].append(testcase_summary_json)
|
||||
|
||||
summary_path = r"/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/postman_echo/logs/request_methods/hardcode.summary.json"
|
||||
summary_path = os.path.join(
|
||||
os.getcwd(),
|
||||
"examples/postman_echo/logs/request_methods/hardcode.summary.json"
|
||||
)
|
||||
summary_dir = os.path.dirname(summary_path)
|
||||
os.makedirs(summary_dir, exist_ok=True)
|
||||
|
||||
@@ -61,4 +64,3 @@ def session_fixture(request):
|
||||
json.dump(summary, f, indent=4, ensure_ascii=False, cls=ExtendJSONEncoder)
|
||||
|
||||
logger.info(f"generated task summary: {summary_path}")
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ def session_fixture(request):
|
||||
|
||||
yield
|
||||
|
||||
logger.debug(f"teardown task fixture")
|
||||
logger.debug("teardown task fixture")
|
||||
|
||||
# teardown task
|
||||
# TODO: upload task summary
|
||||
|
||||
@@ -257,12 +257,12 @@ def ensure_cli_args(args: List) -> List:
|
||||
"""
|
||||
# remove deprecated --failfast
|
||||
if "--failfast" in args:
|
||||
logger.warning(f"remove deprecated argument: --failfast")
|
||||
logger.warning("remove deprecated argument: --failfast")
|
||||
args.pop(args.index("--failfast"))
|
||||
|
||||
# convert --report-file to --html
|
||||
if "--report-file" in args:
|
||||
logger.warning(f"replace deprecated argument --report-file with --html")
|
||||
logger.warning("replace deprecated argument --report-file with --html")
|
||||
index = args.index("--report-file")
|
||||
args[index] = "--html"
|
||||
args.append("--self-contained-html")
|
||||
@@ -270,7 +270,7 @@ def ensure_cli_args(args: List) -> List:
|
||||
# keep compatibility with --save-tests in v2
|
||||
if "--save-tests" in args:
|
||||
logger.warning(
|
||||
f"generate conftest.py keep compatibility with --save-tests in v2"
|
||||
"generate conftest.py keep compatibility with --save-tests in v2"
|
||||
)
|
||||
args.pop(args.index("--save-tests"))
|
||||
_generate_conftest_for_summary(args)
|
||||
@@ -327,21 +327,21 @@ def session_fixture(request):
|
||||
summary["success"] &= testcase_summary.success
|
||||
|
||||
summary["stat"]["testcases"]["total"] += 1
|
||||
summary["stat"]["teststeps"]["total"] += len(testcase_summary.step_datas)
|
||||
summary["stat"]["teststeps"]["total"] += len(testcase_summary.step_results)
|
||||
if testcase_summary.success:
|
||||
summary["stat"]["testcases"]["success"] += 1
|
||||
summary["stat"]["teststeps"]["successes"] += len(
|
||||
testcase_summary.step_datas
|
||||
testcase_summary.step_results
|
||||
)
|
||||
else:
|
||||
summary["stat"]["testcases"]["fail"] += 1
|
||||
summary["stat"]["teststeps"]["successes"] += (
|
||||
len(testcase_summary.step_datas) - 1
|
||||
len(testcase_summary.step_results) - 1
|
||||
)
|
||||
summary["stat"]["teststeps"]["failures"] += 1
|
||||
|
||||
testcase_summary_json = testcase_summary.dict()
|
||||
testcase_summary_json["records"] = testcase_summary_json.pop("step_datas")
|
||||
testcase_summary_json["records"] = testcase_summary_json.pop("step_results")
|
||||
summary["details"].append(testcase_summary_json)
|
||||
|
||||
summary_path = r"{{SUMMARY_PATH_PLACEHOLDER}}"
|
||||
|
||||
@@ -150,20 +150,20 @@ class SessionData(BaseModel):
|
||||
validators: Dict = {}
|
||||
|
||||
|
||||
class StepData(BaseModel):
|
||||
class StepResult(BaseModel):
|
||||
"""teststep data, each step maybe corresponding to one request or one testcase"""
|
||||
|
||||
name: Text = "" # teststep name
|
||||
step_type: Text = "" # teststep type, request or testcase
|
||||
success: bool = False
|
||||
data: Union[SessionData, List['StepData']] = None
|
||||
data: Union[SessionData, List['StepResult']] = None
|
||||
elapsed: float = 0.0 # teststep elapsed time
|
||||
content_size: float = 0 # response content size
|
||||
export_vars: VariablesMapping = {}
|
||||
attachment: Text = "" # teststep attachment
|
||||
|
||||
|
||||
StepData.update_forward_refs()
|
||||
StepResult.update_forward_refs()
|
||||
|
||||
|
||||
class IStep(object):
|
||||
@@ -177,7 +177,7 @@ class IStep(object):
|
||||
def struct(self) -> TStep:
|
||||
raise NotImplementedError
|
||||
|
||||
def run(self, runner) -> StepData:
|
||||
def run(self, runner) -> StepResult:
|
||||
# runner: HttpRunner
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -189,7 +189,7 @@ class TestCaseSummary(BaseModel):
|
||||
time: TestCaseTime
|
||||
in_out: TestCaseInOut = {}
|
||||
log: Text = ""
|
||||
step_datas: List[StepData] = []
|
||||
step_results: List[StepResult] = []
|
||||
|
||||
|
||||
class PlatformInfo(BaseModel):
|
||||
|
||||
@@ -17,7 +17,7 @@ from httprunner.client import HttpSession
|
||||
from httprunner.config import Config
|
||||
from httprunner.exceptions import ParamsError
|
||||
from httprunner.loader import load_project_meta
|
||||
from httprunner.models import (ProjectMeta, StepData, TConfig, TestCaseInOut,
|
||||
from httprunner.models import (ProjectMeta, StepResult, TConfig, TestCaseInOut,
|
||||
TestCaseSummary, TestCaseTime, VariablesMapping)
|
||||
from httprunner.parser import Parser
|
||||
from httprunner.utils import merge_variables
|
||||
@@ -35,7 +35,7 @@ class SessionRunner(object):
|
||||
__config: TConfig
|
||||
__project_meta: ProjectMeta = None
|
||||
__export: List[Text] = []
|
||||
__step_datas: List[StepData] = []
|
||||
__step_results: List[StepResult] = []
|
||||
__session_variables: VariablesMapping = {}
|
||||
# time
|
||||
__start_at: float = 0
|
||||
@@ -58,7 +58,7 @@ class SessionRunner(object):
|
||||
self.root_dir, "logs", f"{self.case_id}.run.log"
|
||||
)
|
||||
|
||||
self.__step_datas.clear()
|
||||
self.__step_results.clear()
|
||||
self.session = self.session or HttpSession()
|
||||
self.parser = self.parser or Parser(self.__project_meta.functions)
|
||||
|
||||
@@ -120,8 +120,8 @@ class SessionRunner(object):
|
||||
start_at_iso_format = datetime.utcfromtimestamp(start_at_timestamp).isoformat()
|
||||
|
||||
summary_success = True
|
||||
for step_data in self.__step_datas:
|
||||
if not step_data.success:
|
||||
for step_result in self.__step_results:
|
||||
if not step_result.success:
|
||||
summary_success = False
|
||||
break
|
||||
|
||||
@@ -139,7 +139,7 @@ class SessionRunner(object):
|
||||
export_vars=self.get_export_variables(),
|
||||
),
|
||||
log=self.__log_path,
|
||||
step_datas=self.__step_datas,
|
||||
step_results=self.__step_results,
|
||||
)
|
||||
|
||||
def merge_step_variables(self, variables: VariablesMapping) -> VariablesMapping:
|
||||
@@ -152,7 +152,7 @@ class SessionRunner(object):
|
||||
# parse variables
|
||||
return self.parser.parse_variables(variables)
|
||||
|
||||
def __run_step(self, step) -> Dict:
|
||||
def __run_step(self, step):
|
||||
"""run teststep, step maybe any kind that implements IStep interface
|
||||
|
||||
Args:
|
||||
@@ -164,14 +164,14 @@ class SessionRunner(object):
|
||||
# run step
|
||||
if USE_ALLURE:
|
||||
with allure.step(f"step: {step.name()}"):
|
||||
step_result = step.run(self)
|
||||
step_result: StepResult = step.run(self)
|
||||
else:
|
||||
step_result = step.run(self)
|
||||
step_result: StepResult = step.run(self)
|
||||
|
||||
# save extracted variables to session variables
|
||||
self.__session_variables.update(step_result.export_vars)
|
||||
# update testcase summary
|
||||
self.__step_datas.append(step_result)
|
||||
self.__step_results.append(step_result)
|
||||
|
||||
logger.info(f"run step end: {step.name()} <<<<<<\n")
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import Union
|
||||
|
||||
from httprunner.models import StepData, TRequest, TStep, TestCase
|
||||
from httprunner.models import StepResult, TRequest, TStep, TestCase
|
||||
from httprunner.runner import HttpRunner
|
||||
from httprunner.step_request import RequestWithOptionalArgs, StepRequestExtraction, StepRequestValidation
|
||||
from httprunner.step_testcase import StepRefCase
|
||||
@@ -36,5 +36,5 @@ class Step(object):
|
||||
def type(self) -> str:
|
||||
return self.__step.type()
|
||||
|
||||
def run(self, runner: HttpRunner) -> StepData:
|
||||
def run(self, runner: HttpRunner) -> StepResult:
|
||||
return self.__step.run(runner)
|
||||
|
||||
@@ -6,7 +6,7 @@ from loguru import logger
|
||||
from httprunner import utils
|
||||
from httprunner.exceptions import ValidationFailure
|
||||
from httprunner.ext.uploader import prepare_upload_step
|
||||
from httprunner.models import (Hooks, IStep, MethodEnum, StepData, TRequest,
|
||||
from httprunner.models import (Hooks, IStep, MethodEnum, StepResult, TRequest,
|
||||
TStep, VariablesMapping)
|
||||
from httprunner.parser import build_url
|
||||
from httprunner.response import ResponseObject
|
||||
@@ -58,12 +58,13 @@ def call_hooks(runner: HttpRunner, hooks: Hooks, step_variables: VariablesMappin
|
||||
logger.error(f"Invalid hook format: {hook}")
|
||||
|
||||
|
||||
def run_step_request(runner: HttpRunner, step: TStep) -> StepData:
|
||||
def run_step_request(runner: HttpRunner, step: TStep) -> StepResult:
|
||||
"""run teststep: request"""
|
||||
step_data = StepData(
|
||||
step_result = StepResult(
|
||||
name=step.name,
|
||||
success=False,
|
||||
)
|
||||
start_time = time.time()
|
||||
|
||||
step.variables = runner.merge_step_variables(step.variables)
|
||||
|
||||
@@ -127,7 +128,7 @@ def run_step_request(runner: HttpRunner, step: TStep) -> StepData:
|
||||
# extract
|
||||
extractors = step.extract
|
||||
extract_mapping = resp_obj.extract(extractors, step.variables)
|
||||
step_data.export_vars = extract_mapping
|
||||
step_result.export_vars = extract_mapping
|
||||
|
||||
variables_mapping = step.variables
|
||||
variables_mapping.update(extract_mapping)
|
||||
@@ -138,21 +139,20 @@ def run_step_request(runner: HttpRunner, step: TStep) -> StepData:
|
||||
resp_obj.validate(
|
||||
validators, variables_mapping
|
||||
)
|
||||
step_data.success = True
|
||||
step_result.success = True
|
||||
except ValidationFailure:
|
||||
log_req_resp_details()
|
||||
# log testcase duration before raise ValidationFailure
|
||||
step_data.elapsed = time.time() - runner.__start_at
|
||||
raise
|
||||
finally:
|
||||
session_data = runner.session.data
|
||||
session_data.success = step_data.success
|
||||
session_data.success = step_result.success
|
||||
session_data.validators = resp_obj.validation_results
|
||||
|
||||
# save step data
|
||||
step_data.data = session_data
|
||||
step_result.data = session_data
|
||||
step_result.elapsed = time.time() - start_time
|
||||
|
||||
return step_data
|
||||
return step_result
|
||||
|
||||
|
||||
class StepRequestValidation(IStep):
|
||||
@@ -418,6 +418,7 @@ class RequestWithOptionalArgs(IStep):
|
||||
|
||||
|
||||
class RunRequest(object):
|
||||
|
||||
def __init__(self, name: Text):
|
||||
self.__step = TStep(name=name)
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class TestRunRequest(unittest.TestCase):
|
||||
summary = runner.get_summary()
|
||||
self.assertTrue(summary.success)
|
||||
self.assertEqual(summary.name, "request methods testcase with functions")
|
||||
self.assertEqual(len(summary.step_datas), 3)
|
||||
self.assertEqual(summary.step_datas[0].name, "get with params")
|
||||
self.assertEqual(summary.step_datas[1].name, "post raw text")
|
||||
self.assertEqual(summary.step_datas[2].name, "post form data")
|
||||
self.assertEqual(len(summary.step_results), 3)
|
||||
self.assertEqual(summary.step_results[0].name, "get with params")
|
||||
self.assertEqual(summary.step_results[1].name, "post raw text")
|
||||
self.assertEqual(summary.step_results[2].name, "post form data")
|
||||
|
||||
@@ -3,14 +3,14 @@ from typing import Callable, Text
|
||||
from loguru import logger
|
||||
|
||||
from httprunner import exceptions
|
||||
from httprunner.models import IStep, StepData, TStep
|
||||
from httprunner.models import IStep, StepResult, TStep, TestCaseSummary
|
||||
from httprunner.runner import HttpRunner
|
||||
from httprunner.step_request import call_hooks
|
||||
|
||||
|
||||
def run_step_testcase(runner: HttpRunner, step: TStep) -> StepData:
|
||||
def run_step_testcase(runner: HttpRunner, step: TStep) -> StepResult:
|
||||
"""run teststep: referenced testcase"""
|
||||
step_data = StepData(name=step.name)
|
||||
step_result = StepResult(name=step.name)
|
||||
step_variables = step.variables
|
||||
step_export = step.export
|
||||
|
||||
@@ -32,15 +32,15 @@ def run_step_testcase(runner: HttpRunner, step: TStep) -> StepData:
|
||||
if step.teardown_hooks:
|
||||
call_hooks(runner, step.teardown_hooks, step.variables, "teardown testcase")
|
||||
|
||||
summary = ref_case_runner.get_summary()
|
||||
step_data.data = summary.step_datas # list of step data
|
||||
step_data.export_vars = summary.in_out.export_vars
|
||||
step_data.success = summary.success
|
||||
summary: TestCaseSummary = ref_case_runner.get_summary()
|
||||
step_result.data = summary.step_results # list of step data
|
||||
step_result.export_vars = summary.in_out.export_vars
|
||||
step_result.success = summary.success
|
||||
|
||||
if step_data.export_vars:
|
||||
logger.info(f"export variables: {step_data.export_vars}")
|
||||
if step_result.export_vars:
|
||||
logger.info(f"export variables: {step_result.export_vars}")
|
||||
|
||||
return step_data
|
||||
return step_result
|
||||
|
||||
|
||||
class StepRefCase(IStep):
|
||||
|
||||
@@ -12,12 +12,12 @@ class TestRunTestCase(unittest.TestCase):
|
||||
|
||||
def test_run_testcase_by_path(self):
|
||||
|
||||
step_data = RunTestCase("run referenced testcase").call(
|
||||
step_result = RunTestCase("run referenced testcase").call(
|
||||
TestCaseRequestWithFunctions
|
||||
).run(self.runner)
|
||||
self.assertTrue(step_data.success)
|
||||
self.assertEqual(step_data.name, "run referenced testcase")
|
||||
self.assertEqual(len(step_data.data), 3)
|
||||
self.assertEqual(step_data.data[0].name, "get with params")
|
||||
self.assertEqual(step_data.data[1].name, "post raw text")
|
||||
self.assertEqual(step_data.data[2].name, "post form data")
|
||||
self.assertTrue(step_result.success)
|
||||
self.assertEqual(step_result.name, "run referenced testcase")
|
||||
self.assertEqual(len(step_result.data), 3)
|
||||
self.assertEqual(step_result.data[0].name, "get with params")
|
||||
self.assertEqual(step_result.data[1].name, "post raw text")
|
||||
self.assertEqual(step_result.data[2].name, "post form data")
|
||||
|
||||
Reference in New Issue
Block a user