mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 14:32:31 +08:00
refactor: rename variable step_context
This commit is contained in:
@@ -57,37 +57,43 @@ class Config(object):
|
|||||||
|
|
||||||
|
|
||||||
class StepRequestValidation(object):
|
class StepRequestValidation(object):
|
||||||
def __init__(self, step: TStep):
|
def __init__(self, step_context: TStep):
|
||||||
self.__t_step = step
|
self.__step_context = step_context
|
||||||
|
|
||||||
def assert_equal(
|
def assert_equal(
|
||||||
self, jmes_path: Text, expected_value: Any
|
self, jmes_path: Text, expected_value: Any
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"equal": [jmes_path, expected_value]})
|
self.__step_context.validators.append({"equal": [jmes_path, expected_value]})
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_not_equal(
|
def assert_not_equal(
|
||||||
self, jmes_path: Text, expected_value: Any
|
self, jmes_path: Text, expected_value: Any
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"not_equal": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"not_equal": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_greater_than(
|
def assert_greater_than(
|
||||||
self, jmes_path: Text, expected_value: Union[int, float]
|
self, jmes_path: Text, expected_value: Union[int, float]
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"greater_than": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"greater_than": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_less_than(
|
def assert_less_than(
|
||||||
self, jmes_path: Text, expected_value: Union[int, float]
|
self, jmes_path: Text, expected_value: Union[int, float]
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"less_than": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"less_than": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_greater_or_equals(
|
def assert_greater_or_equals(
|
||||||
self, jmes_path: Text, expected_value: Union[int, float]
|
self, jmes_path: Text, expected_value: Union[int, float]
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append(
|
self.__step_context.validators.append(
|
||||||
{"greater_or_equals": [jmes_path, expected_value]}
|
{"greater_or_equals": [jmes_path, expected_value]}
|
||||||
)
|
)
|
||||||
return self
|
return self
|
||||||
@@ -95,19 +101,23 @@ class StepRequestValidation(object):
|
|||||||
def assert_less_or_equals(
|
def assert_less_or_equals(
|
||||||
self, jmes_path: Text, expected_value: Union[int, float]
|
self, jmes_path: Text, expected_value: Union[int, float]
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"less_or_equals": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"less_or_equals": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_length_equal(
|
def assert_length_equal(
|
||||||
self, jmes_path: Text, expected_value: int
|
self, jmes_path: Text, expected_value: int
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"length_equal": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"length_equal": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_length_greater_than(
|
def assert_length_greater_than(
|
||||||
self, jmes_path: Text, expected_value: int
|
self, jmes_path: Text, expected_value: int
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append(
|
self.__step_context.validators.append(
|
||||||
{"length_greater_than": [jmes_path, expected_value]}
|
{"length_greater_than": [jmes_path, expected_value]}
|
||||||
)
|
)
|
||||||
return self
|
return self
|
||||||
@@ -115,7 +125,7 @@ class StepRequestValidation(object):
|
|||||||
def assert_length_less_than(
|
def assert_length_less_than(
|
||||||
self, jmes_path: Text, expected_value: int
|
self, jmes_path: Text, expected_value: int
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append(
|
self.__step_context.validators.append(
|
||||||
{"length_less_than": [jmes_path, expected_value]}
|
{"length_less_than": [jmes_path, expected_value]}
|
||||||
)
|
)
|
||||||
return self
|
return self
|
||||||
@@ -123,7 +133,7 @@ class StepRequestValidation(object):
|
|||||||
def assert_length_greater_or_equals(
|
def assert_length_greater_or_equals(
|
||||||
self, jmes_path: Text, expected_value: int
|
self, jmes_path: Text, expected_value: int
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append(
|
self.__step_context.validators.append(
|
||||||
{"length_greater_or_equals": [jmes_path, expected_value]}
|
{"length_greater_or_equals": [jmes_path, expected_value]}
|
||||||
)
|
)
|
||||||
return self
|
return self
|
||||||
@@ -131,7 +141,7 @@ class StepRequestValidation(object):
|
|||||||
def assert_length_less_or_equals(
|
def assert_length_less_or_equals(
|
||||||
self, jmes_path: Text, expected_value: int
|
self, jmes_path: Text, expected_value: int
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append(
|
self.__step_context.validators.append(
|
||||||
{"length_less_or_equals": [jmes_path, expected_value]}
|
{"length_less_or_equals": [jmes_path, expected_value]}
|
||||||
)
|
)
|
||||||
return self
|
return self
|
||||||
@@ -139,55 +149,65 @@ class StepRequestValidation(object):
|
|||||||
def assert_string_equals(
|
def assert_string_equals(
|
||||||
self, jmes_path: Text, expected_value: int
|
self, jmes_path: Text, expected_value: int
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"string_equals": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"string_equals": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_startswith(
|
def assert_startswith(
|
||||||
self, jmes_path: Text, expected_value: Text
|
self, jmes_path: Text, expected_value: Text
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"startswith": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"startswith": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_endswith(
|
def assert_endswith(
|
||||||
self, jmes_path: Text, expected_value: Text
|
self, jmes_path: Text, expected_value: Text
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"endswith": [jmes_path, expected_value]})
|
self.__step_context.validators.append({"endswith": [jmes_path, expected_value]})
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_regex_match(
|
def assert_regex_match(
|
||||||
self, jmes_path: Text, expected_value: Text
|
self, jmes_path: Text, expected_value: Text
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"regex_match": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"regex_match": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_contains(
|
def assert_contains(
|
||||||
self, jmes_path: Text, expected_value: Any
|
self, jmes_path: Text, expected_value: Any
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"contains": [jmes_path, expected_value]})
|
self.__step_context.validators.append({"contains": [jmes_path, expected_value]})
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_contained_by(
|
def assert_contained_by(
|
||||||
self, jmes_path: Text, expected_value: Any
|
self, jmes_path: Text, expected_value: Any
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"contained_by": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"contained_by": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def assert_type_match(
|
def assert_type_match(
|
||||||
self, jmes_path: Text, expected_value: Text
|
self, jmes_path: Text, expected_value: Text
|
||||||
) -> "StepRequestValidation":
|
) -> "StepRequestValidation":
|
||||||
self.__t_step.validators.append({"type_match": [jmes_path, expected_value]})
|
self.__step_context.validators.append(
|
||||||
|
{"type_match": [jmes_path, expected_value]}
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def perform(self) -> TStep:
|
def perform(self) -> TStep:
|
||||||
return self.__t_step
|
return self.__step_context
|
||||||
|
|
||||||
|
|
||||||
class StepRequestExtraction(object):
|
class StepRequestExtraction(object):
|
||||||
def __init__(self, step: TStep):
|
def __init__(self, step_context: TStep):
|
||||||
self.__t_step = step
|
self.__step_context = step_context
|
||||||
|
|
||||||
def with_jmespath(self, jmes_path: Text, var_name: Text) -> "StepRequestExtraction":
|
def with_jmespath(self, jmes_path: Text, var_name: Text) -> "StepRequestExtraction":
|
||||||
self.__t_step.extract[var_name] = jmes_path
|
self.__step_context.extract[var_name] = jmes_path
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# def with_regex(self):
|
# def with_regex(self):
|
||||||
@@ -199,134 +219,134 @@ class StepRequestExtraction(object):
|
|||||||
# pass
|
# pass
|
||||||
|
|
||||||
def validate(self) -> StepRequestValidation:
|
def validate(self) -> StepRequestValidation:
|
||||||
return StepRequestValidation(self.__t_step)
|
return StepRequestValidation(self.__step_context)
|
||||||
|
|
||||||
def perform(self) -> TStep:
|
def perform(self) -> TStep:
|
||||||
return self.__t_step
|
return self.__step_context
|
||||||
|
|
||||||
|
|
||||||
class RequestWithOptionalArgs(object):
|
class RequestWithOptionalArgs(object):
|
||||||
def __init__(self, step: TStep):
|
def __init__(self, step_context: TStep):
|
||||||
self.__t_step = step
|
self.__step_context = step_context
|
||||||
|
|
||||||
def with_params(self, **params) -> "RequestWithOptionalArgs":
|
def with_params(self, **params) -> "RequestWithOptionalArgs":
|
||||||
self.__t_step.request.params.update(params)
|
self.__step_context.request.params.update(params)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def with_headers(self, **headers) -> "RequestWithOptionalArgs":
|
def with_headers(self, **headers) -> "RequestWithOptionalArgs":
|
||||||
self.__t_step.request.headers.update(headers)
|
self.__step_context.request.headers.update(headers)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def with_cookies(self, **cookies) -> "RequestWithOptionalArgs":
|
def with_cookies(self, **cookies) -> "RequestWithOptionalArgs":
|
||||||
self.__t_step.request.cookies.update(cookies)
|
self.__step_context.request.cookies.update(cookies)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def with_data(self, data) -> "RequestWithOptionalArgs":
|
def with_data(self, data) -> "RequestWithOptionalArgs":
|
||||||
self.__t_step.request.data = data
|
self.__step_context.request.data = data
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def with_json(self, req_json) -> "RequestWithOptionalArgs":
|
def with_json(self, req_json) -> "RequestWithOptionalArgs":
|
||||||
self.__t_step.request.req_json = req_json
|
self.__step_context.request.req_json = req_json
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def set_timeout(self, timeout: float) -> "RequestWithOptionalArgs":
|
def set_timeout(self, timeout: float) -> "RequestWithOptionalArgs":
|
||||||
self.__t_step.request.timeout = timeout
|
self.__step_context.request.timeout = timeout
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def set_verify(self, verify: bool) -> "RequestWithOptionalArgs":
|
def set_verify(self, verify: bool) -> "RequestWithOptionalArgs":
|
||||||
self.__t_step.request.verify = verify
|
self.__step_context.request.verify = verify
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def set_allow_redirects(self, allow_redirects: bool) -> "RequestWithOptionalArgs":
|
def set_allow_redirects(self, allow_redirects: bool) -> "RequestWithOptionalArgs":
|
||||||
self.__t_step.request.allow_redirects = allow_redirects
|
self.__step_context.request.allow_redirects = allow_redirects
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def upload(self, **file_info) -> "RequestWithOptionalArgs":
|
def upload(self, **file_info) -> "RequestWithOptionalArgs":
|
||||||
self.__t_step.request.upload.update(file_info)
|
self.__step_context.request.upload.update(file_info)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# def hooks(self):
|
# def hooks(self):
|
||||||
# pass
|
# pass
|
||||||
|
|
||||||
def extract(self) -> StepRequestExtraction:
|
def extract(self) -> StepRequestExtraction:
|
||||||
return StepRequestExtraction(self.__t_step)
|
return StepRequestExtraction(self.__step_context)
|
||||||
|
|
||||||
def validate(self) -> StepRequestValidation:
|
def validate(self) -> StepRequestValidation:
|
||||||
return StepRequestValidation(self.__t_step)
|
return StepRequestValidation(self.__step_context)
|
||||||
|
|
||||||
def perform(self) -> TStep:
|
def perform(self) -> TStep:
|
||||||
return self.__t_step
|
return self.__step_context
|
||||||
|
|
||||||
|
|
||||||
class RunRequest(object):
|
class RunRequest(object):
|
||||||
def __init__(self, name: Text):
|
def __init__(self, name: Text):
|
||||||
self.__t_step = TStep(name=name)
|
self.__step_context = TStep(name=name)
|
||||||
|
|
||||||
def with_variables(self, **variables) -> "RunRequest":
|
def with_variables(self, **variables) -> "RunRequest":
|
||||||
self.__t_step.variables.update(variables)
|
self.__step_context.variables.update(variables)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def get(self, url: Text) -> RequestWithOptionalArgs:
|
def get(self, url: Text) -> RequestWithOptionalArgs:
|
||||||
self.__t_step.request = TRequest(method=MethodEnum.GET, url=url)
|
self.__step_context.request = TRequest(method=MethodEnum.GET, url=url)
|
||||||
return RequestWithOptionalArgs(self.__t_step)
|
return RequestWithOptionalArgs(self.__step_context)
|
||||||
|
|
||||||
def post(self, url: Text) -> RequestWithOptionalArgs:
|
def post(self, url: Text) -> RequestWithOptionalArgs:
|
||||||
self.__t_step.request = TRequest(method=MethodEnum.POST, url=url)
|
self.__step_context.request = TRequest(method=MethodEnum.POST, url=url)
|
||||||
return RequestWithOptionalArgs(self.__t_step)
|
return RequestWithOptionalArgs(self.__step_context)
|
||||||
|
|
||||||
def put(self, url: Text) -> RequestWithOptionalArgs:
|
def put(self, url: Text) -> RequestWithOptionalArgs:
|
||||||
self.__t_step.request = TRequest(method=MethodEnum.PUT, url=url)
|
self.__step_context.request = TRequest(method=MethodEnum.PUT, url=url)
|
||||||
return RequestWithOptionalArgs(self.__t_step)
|
return RequestWithOptionalArgs(self.__step_context)
|
||||||
|
|
||||||
def head(self, url: Text) -> RequestWithOptionalArgs:
|
def head(self, url: Text) -> RequestWithOptionalArgs:
|
||||||
self.__t_step.request = TRequest(method=MethodEnum.HEAD, url=url)
|
self.__step_context.request = TRequest(method=MethodEnum.HEAD, url=url)
|
||||||
return RequestWithOptionalArgs(self.__t_step)
|
return RequestWithOptionalArgs(self.__step_context)
|
||||||
|
|
||||||
def delete(self, url: Text) -> RequestWithOptionalArgs:
|
def delete(self, url: Text) -> RequestWithOptionalArgs:
|
||||||
self.__t_step.request = TRequest(method=MethodEnum.DELETE, url=url)
|
self.__step_context.request = TRequest(method=MethodEnum.DELETE, url=url)
|
||||||
return RequestWithOptionalArgs(self.__t_step)
|
return RequestWithOptionalArgs(self.__step_context)
|
||||||
|
|
||||||
def options(self, url: Text) -> RequestWithOptionalArgs:
|
def options(self, url: Text) -> RequestWithOptionalArgs:
|
||||||
self.__t_step.request = TRequest(method=MethodEnum.OPTIONS, url=url)
|
self.__step_context.request = TRequest(method=MethodEnum.OPTIONS, url=url)
|
||||||
return RequestWithOptionalArgs(self.__t_step)
|
return RequestWithOptionalArgs(self.__step_context)
|
||||||
|
|
||||||
def patch(self, url: Text) -> RequestWithOptionalArgs:
|
def patch(self, url: Text) -> RequestWithOptionalArgs:
|
||||||
self.__t_step.request = TRequest(method=MethodEnum.PATCH, url=url)
|
self.__step_context.request = TRequest(method=MethodEnum.PATCH, url=url)
|
||||||
return RequestWithOptionalArgs(self.__t_step)
|
return RequestWithOptionalArgs(self.__step_context)
|
||||||
|
|
||||||
|
|
||||||
class StepRefCase(object):
|
class StepRefCase(object):
|
||||||
def __init__(self, step: TStep):
|
def __init__(self, step_context: TStep):
|
||||||
self.__t_step = step
|
self.__step_context = step_context
|
||||||
|
|
||||||
def export(self, *var_name: Text) -> "StepRefCase":
|
def export(self, *var_name: Text) -> "StepRefCase":
|
||||||
self.__t_step.export.extend(var_name)
|
self.__step_context.export.extend(var_name)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def perform(self) -> TStep:
|
def perform(self) -> TStep:
|
||||||
return self.__t_step
|
return self.__step_context
|
||||||
|
|
||||||
|
|
||||||
class RunTestCase(object):
|
class RunTestCase(object):
|
||||||
def __init__(self, name: Text):
|
def __init__(self, name: Text):
|
||||||
self.__t_step = TStep(name=name)
|
self.__step_context = TStep(name=name)
|
||||||
|
|
||||||
def with_variables(self, **variables) -> "RunTestCase":
|
def with_variables(self, **variables) -> "RunTestCase":
|
||||||
self.__t_step.variables.update(variables)
|
self.__step_context.variables.update(variables)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def call(self, testcase: Callable) -> StepRefCase:
|
def call(self, testcase: Callable) -> StepRefCase:
|
||||||
self.__t_step.testcase = testcase
|
self.__step_context.testcase = testcase
|
||||||
return StepRefCase(self.__t_step)
|
return StepRefCase(self.__step_context)
|
||||||
|
|
||||||
def perform(self) -> TStep:
|
def perform(self) -> TStep:
|
||||||
return self.__t_step
|
return self.__step_context
|
||||||
|
|
||||||
|
|
||||||
class Step(object):
|
class Step(object):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
step: Union[
|
step_context: Union[
|
||||||
StepRequestValidation,
|
StepRequestValidation,
|
||||||
StepRequestExtraction,
|
StepRequestExtraction,
|
||||||
RequestWithOptionalArgs,
|
RequestWithOptionalArgs,
|
||||||
@@ -334,15 +354,15 @@ class Step(object):
|
|||||||
StepRefCase,
|
StepRefCase,
|
||||||
],
|
],
|
||||||
):
|
):
|
||||||
self.__t_step = step.perform()
|
self.__step_context = step_context.perform()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def request(self) -> TRequest:
|
def request(self) -> TRequest:
|
||||||
return self.__t_step.request
|
return self.__step_context.request
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def testcase(self) -> TestCase:
|
def testcase(self) -> TestCase:
|
||||||
return self.__t_step.testcase
|
return self.__step_context.testcase
|
||||||
|
|
||||||
def perform(self) -> TStep:
|
def perform(self) -> TStep:
|
||||||
return self.__t_step
|
return self.__step_context
|
||||||
|
|||||||
Reference in New Issue
Block a user