Skip to content

Commit

Permalink
feature: #1142 extend the last one of the step in sub-testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengtong0898 committed Jan 9, 2022
1 parent 3f2ca42 commit 5534492
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
config:
name: "request methods testcase: reference testcase"
variables:
foo1: testsuite_config_bar1
expect_foo1: testsuite_config_bar1
expect_foo2: config_bar2
base_url: "https://postman-echo.com"
verify: False

teststeps:
-
name: "extend the last one of the step in sub-testcase"
variables:
foo1: testcase_ref_bar1
expect_foo1: testcase_ref_bar1
testcase: request_methods/request_with_functions.yml
validate:
- eq: ["status_code", 200]
- eq: ["$foo3", "bar21"] # can access all extract-variables in sub-testcase
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# NOTE: Generated By HttpRunner v3.1.6
# FROM: request_methods/request_with_testcase_validate.yml


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


class TestCaseRequestWithTestcaseValidate(HttpRunner):

config = (
Config("request methods testcase: reference testcase")
.variables(
**{
"foo1": "testsuite_config_bar1",
"expect_foo1": "testsuite_config_bar1",
"expect_foo2": "config_bar2",
}
)
.base_url("https://postman-echo.com")
.verify(False)
)

teststeps = [
Step(
RunTestCase("extend the last one of the step in sub-testcase")
.with_variables(
**{"foo1": "testcase_ref_bar1", "expect_foo1": "testcase_ref_bar1"}
)
.call(RequestWithFunctions)
.export(*["foo3"])
.validate()
.assert_equal("status_code", 200)
.assert_equal("$foo3", "bar21")
),
]


if __name__ == "__main__":
TestCaseRequestWithTestcaseValidate().test_start()
14 changes: 14 additions & 0 deletions httprunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
ProjectMeta,
TestCase,
Hooks,
Validators
)


Expand All @@ -46,6 +47,7 @@ class HttpRunner(object):
__project_meta: ProjectMeta = None
__case_id: Text = ""
__export: List[Text] = []
__validators: Validators = []
__step_datas: List[StepData] = []
__session: HttpSession = None
__session_variables: VariablesMapping = {}
Expand Down Expand Up @@ -88,6 +90,10 @@ def with_export(self, export: List[Text]) -> "HttpRunner":
self.__export = export
return self

def with_validators(self, validators: Validators):
self.__validators = validators
return self

def __call_hooks(
self, hooks: Hooks, step_variables: VariablesMapping, hook_msg: Text,
) -> NoReturn:
Expand Down Expand Up @@ -234,6 +240,7 @@ def __run_step_testcase(self, step: TStep) -> StepData:
"""run teststep: referenced testcase"""
step_data = StepData(name=step.name)
step_variables = step.variables
step_validators = step.validators
step_export = step.export

# setup hooks
Expand All @@ -248,6 +255,7 @@ def __run_step_testcase(self, step: TStep) -> StepData:
.with_case_id(self.__case_id)
.with_variables(step_variables)
.with_export(step_export)
.with_validators(step_validators)
.run()
)

Expand All @@ -265,6 +273,7 @@ def __run_step_testcase(self, step: TStep) -> StepData:
.with_case_id(self.__case_id)
.with_variables(step_variables)
.with_export(step_export)
.with_validators(step_validators)
.run_path(ref_testcase_path)
)

Expand Down Expand Up @@ -338,6 +347,11 @@ def run_testcase(self, testcase: TestCase) -> "HttpRunner":
# save extracted variables of teststeps
extracted_variables: VariablesMapping = {}

# extend validators
last_step = self.__teststeps[-1]
if last_step.request:
last_step.validators.extend(self.__validators)

# run teststeps
for step in self.__teststeps:
# override variables
Expand Down
3 changes: 3 additions & 0 deletions httprunner/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ def export(self, *var_name: Text) -> "StepRefCase":
def perform(self) -> TStep:
return self.__step_context

def validate(self) -> StepRequestValidation:
return StepRequestValidation(self.__step_context)


class RunTestCase(object):
def __init__(self, name: Text):
Expand Down

0 comments on commit 5534492

Please sign in to comment.