From 55344928c22c5f0c4f282e968a8a43cfef7ebb91 Mon Sep 17 00:00:00 2001 From: zhengtong0898 Date: Sun, 9 Jan 2022 22:11:35 +0800 Subject: [PATCH] feature: #1142 extend the last one of the step in sub-testcase --- .../request_with_testcase_validate.yml | 19 +++++++ .../request_with_testcase_validate_test.py | 49 +++++++++++++++++++ httprunner/runner.py | 14 ++++++ httprunner/testcase.py | 3 ++ 4 files changed, 85 insertions(+) create mode 100644 examples/postman_echo/request_methods/request_with_testcase_validate.yml create mode 100644 examples/postman_echo/request_methods/request_with_testcase_validate_test.py diff --git a/examples/postman_echo/request_methods/request_with_testcase_validate.yml b/examples/postman_echo/request_methods/request_with_testcase_validate.yml new file mode 100644 index 000000000..49533444e --- /dev/null +++ b/examples/postman_echo/request_methods/request_with_testcase_validate.yml @@ -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 diff --git a/examples/postman_echo/request_methods/request_with_testcase_validate_test.py b/examples/postman_echo/request_methods/request_with_testcase_validate_test.py new file mode 100644 index 000000000..c44659b2e --- /dev/null +++ b/examples/postman_echo/request_methods/request_with_testcase_validate_test.py @@ -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() diff --git a/httprunner/runner.py b/httprunner/runner.py index 00a46f53b..d4917dc99 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -33,6 +33,7 @@ ProjectMeta, TestCase, Hooks, + Validators ) @@ -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 = {} @@ -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: @@ -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 @@ -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() ) @@ -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) ) @@ -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 diff --git a/httprunner/testcase.py b/httprunner/testcase.py index 86d1554fa..4fbd88bfd 100644 --- a/httprunner/testcase.py +++ b/httprunner/testcase.py @@ -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):