mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-14 11:37:36 +08:00
feat: add builtin comparators for StepValidation
This commit is contained in:
@@ -9,23 +9,23 @@ def equal(check_value, expect_value):
|
||||
assert check_value == expect_value
|
||||
|
||||
|
||||
def less_than(check_value, expect_value):
|
||||
assert check_value < expect_value
|
||||
|
||||
|
||||
def less_than_or_equals(check_value, expect_value):
|
||||
assert check_value <= expect_value
|
||||
|
||||
|
||||
def greater_than(check_value, expect_value):
|
||||
assert check_value > expect_value
|
||||
|
||||
|
||||
def greater_than_or_equals(check_value, expect_value):
|
||||
def less_than(check_value, expect_value):
|
||||
assert check_value < expect_value
|
||||
|
||||
|
||||
def greater_or_equals(check_value, expect_value):
|
||||
assert check_value >= expect_value
|
||||
|
||||
|
||||
def not_equals(check_value, expect_value):
|
||||
def less_or_equals(check_value, expect_value):
|
||||
assert check_value <= expect_value
|
||||
|
||||
|
||||
def not_equal(check_value, expect_value):
|
||||
assert check_value != expect_value
|
||||
|
||||
|
||||
@@ -33,34 +33,29 @@ def string_equals(check_value, expect_value):
|
||||
assert str(check_value) == str(expect_value)
|
||||
|
||||
|
||||
def length_equals(check_value, expect_value):
|
||||
def length_equal(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
expect_len = _cast_to_int(expect_value)
|
||||
assert len(check_value) == expect_len
|
||||
assert len(check_value) == expect_value
|
||||
|
||||
|
||||
def length_greater_than(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
expect_len = _cast_to_int(expect_value)
|
||||
assert len(check_value) > expect_len
|
||||
assert isinstance(expect_value, (int, float))
|
||||
assert len(check_value) > expect_value
|
||||
|
||||
|
||||
def length_greater_than_or_equals(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
expect_len = _cast_to_int(expect_value)
|
||||
assert len(check_value) >= expect_len
|
||||
def length_greater_or_equals(check_value, expect_value):
|
||||
assert isinstance(expect_value, (int, float))
|
||||
assert len(check_value) >= expect_value
|
||||
|
||||
|
||||
def length_less_than(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
expect_len = _cast_to_int(expect_value)
|
||||
assert len(check_value) < expect_len
|
||||
assert isinstance(expect_value, (int, float))
|
||||
assert len(check_value) < expect_value
|
||||
|
||||
|
||||
def length_less_than_or_equals(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
expect_len = _cast_to_int(expect_value)
|
||||
assert len(check_value) <= expect_len
|
||||
def length_less_or_equals(check_value, expect_value):
|
||||
assert isinstance(expect_value, (int, float))
|
||||
assert len(check_value) <= expect_value
|
||||
|
||||
|
||||
def contains(check_value, expect_value):
|
||||
@@ -100,10 +95,3 @@ def startswith(check_value, expect_value):
|
||||
|
||||
def endswith(check_value, expect_value):
|
||||
assert str(check_value).endswith(str(expect_value))
|
||||
|
||||
|
||||
def _cast_to_int(expect_value):
|
||||
try:
|
||||
return int(expect_value)
|
||||
except Exception:
|
||||
raise AssertionError("%r can't cast to int" % str(expect_value))
|
||||
|
||||
@@ -16,41 +16,35 @@ def get_uniform_comparator(comparator: Text):
|
||||
return "equal"
|
||||
elif comparator in ["lt", "less_than"]:
|
||||
return "less_than"
|
||||
elif comparator in ["le", "less_than_or_equals"]:
|
||||
return "less_than_or_equals"
|
||||
elif comparator in ["le", "less_or_equals"]:
|
||||
return "less_or_equals"
|
||||
elif comparator in ["gt", "greater_than"]:
|
||||
return "greater_than"
|
||||
elif comparator in ["ge", "greater_than_or_equals"]:
|
||||
return "greater_than_or_equals"
|
||||
elif comparator in ["ne", "not_equals"]:
|
||||
return "not_equals"
|
||||
elif comparator in ["ge", "greater_or_equals"]:
|
||||
return "greater_or_equals"
|
||||
elif comparator in ["ne", "not_equal"]:
|
||||
return "not_equal"
|
||||
elif comparator in ["str_eq", "string_equals"]:
|
||||
return "string_equals"
|
||||
elif comparator in ["len_eq", "length_equals", "count_eq"]:
|
||||
return "length_equals"
|
||||
elif comparator in ["len_eq", "length_equal"]:
|
||||
return "length_equal"
|
||||
elif comparator in [
|
||||
"len_gt",
|
||||
"count_gt",
|
||||
"length_greater_than",
|
||||
"count_greater_than",
|
||||
]:
|
||||
return "length_greater_than"
|
||||
elif comparator in [
|
||||
"len_ge",
|
||||
"count_ge",
|
||||
"length_greater_than_or_equals",
|
||||
"count_greater_than_or_equals",
|
||||
"length_greater_or_equals",
|
||||
]:
|
||||
return "length_greater_than_or_equals"
|
||||
elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:
|
||||
return "length_greater_or_equals"
|
||||
elif comparator in ["len_lt", "length_less_than"]:
|
||||
return "length_less_than"
|
||||
elif comparator in [
|
||||
"len_le",
|
||||
"count_le",
|
||||
"length_less_than_or_equals",
|
||||
"count_less_than_or_equals",
|
||||
"length_less_or_equals",
|
||||
]:
|
||||
return "length_less_than_or_equals"
|
||||
return "length_less_or_equals"
|
||||
else:
|
||||
return comparator
|
||||
|
||||
|
||||
@@ -55,19 +55,117 @@ class StepValidation(object):
|
||||
self.__t_step = step
|
||||
|
||||
def assert_equal(self, jmes_path: Text, expected_value: Any) -> "StepValidation":
|
||||
self.__t_step.validators.append({"eq": [jmes_path, expected_value]})
|
||||
self.__t_step.validators.append({"equal": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_not_equal(
|
||||
self, jmes_path: Text, expected_value: Any
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"not_equal": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_greater_than(
|
||||
self, jmes_path: Text, expected_value: Any
|
||||
self, jmes_path: Text, expected_value: Union[int, float]
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"gt": [jmes_path, expected_value]})
|
||||
self.__t_step.validators.append({"greater_than": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_less_than(
|
||||
self, jmes_path: Text, expected_value: Union[int, float]
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"less_than": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_greater_or_equals(
|
||||
self, jmes_path: Text, expected_value: Union[int, float]
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append(
|
||||
{"greater_or_equals": [jmes_path, expected_value]}
|
||||
)
|
||||
return self
|
||||
|
||||
def assert_less_or_equals(
|
||||
self, jmes_path: Text, expected_value: Union[int, float]
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"less_or_equals": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_length_equal(
|
||||
self, jmes_path: Text, expected_value: int
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"length_equal": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_length_greater_than(
|
||||
self, jmes_path: Text, expected_value: int
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append(
|
||||
{"length_greater_than": [jmes_path, expected_value]}
|
||||
)
|
||||
return self
|
||||
|
||||
def assert_length_less_than(
|
||||
self, jmes_path: Text, expected_value: int
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append(
|
||||
{"length_less_than": [jmes_path, expected_value]}
|
||||
)
|
||||
return self
|
||||
|
||||
def assert_length_greater_or_equals(
|
||||
self, jmes_path: Text, expected_value: int
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append(
|
||||
{"length_greater_or_equals": [jmes_path, expected_value]}
|
||||
)
|
||||
return self
|
||||
|
||||
def assert_length_less_or_equals(
|
||||
self, jmes_path: Text, expected_value: int
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append(
|
||||
{"length_less_or_equals": [jmes_path, expected_value]}
|
||||
)
|
||||
return self
|
||||
|
||||
def assert_string_equals(
|
||||
self, jmes_path: Text, expected_value: int
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"string_equals": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_startswith(
|
||||
self, jmes_path: Text, expected_value: Text
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"startswith": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_endswith(
|
||||
self, jmes_path: Text, expected_value: Text
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"endswith": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_regex_match(
|
||||
self, jmes_path: Text, expected_value: Text
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"regex_match": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_contains(self, jmes_path: Text, expected_value: Any) -> "StepValidation":
|
||||
self.__t_step.validators.append({"contains": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_contained_by(
|
||||
self, jmes_path: Text, expected_value: Any
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"lt": [jmes_path, expected_value]})
|
||||
self.__t_step.validators.append({"contained_by": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def assert_type_match(
|
||||
self, jmes_path: Text, expected_value: Text
|
||||
) -> "StepValidation":
|
||||
self.__t_step.validators.append({"type_match": [jmes_path, expected_value]})
|
||||
return self
|
||||
|
||||
def perform(self) -> TStep:
|
||||
|
||||
@@ -43,7 +43,7 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
|
||||
content,
|
||||
)
|
||||
self.assertIn(
|
||||
'.call(RequestWithFunctions)', content,
|
||||
".call(RequestWithFunctions)", content,
|
||||
)
|
||||
|
||||
def test_make_testcase_folder(self):
|
||||
|
||||
@@ -12,33 +12,32 @@ class TestUtils(unittest.TestCase):
|
||||
self.assertIn("abc", os.environ)
|
||||
self.assertEqual(os.environ["abc"], "123")
|
||||
|
||||
def current_validators(self):
|
||||
def test_validators(self):
|
||||
from httprunner.builtin import comparators
|
||||
|
||||
functions_mapping = loader.load_module_functions(comparators)
|
||||
|
||||
functions_mapping["equals"](None, None)
|
||||
functions_mapping["equals"](1, 1)
|
||||
functions_mapping["equals"]("abc", "abc")
|
||||
functions_mapping["equal"](None, None)
|
||||
functions_mapping["equal"](1, 1)
|
||||
functions_mapping["equal"]("abc", "abc")
|
||||
with self.assertRaises(AssertionError):
|
||||
functions_mapping["equals"]("123", 123)
|
||||
functions_mapping["equal"]("123", 123)
|
||||
|
||||
functions_mapping["less_than"](1, 2)
|
||||
functions_mapping["less_than_or_equals"](2, 2)
|
||||
functions_mapping["less_or_equals"](2, 2)
|
||||
|
||||
functions_mapping["greater_than"](2, 1)
|
||||
functions_mapping["greater_than_or_equals"](2, 2)
|
||||
functions_mapping["greater_or_equals"](2, 2)
|
||||
|
||||
functions_mapping["not_equals"](123, "123")
|
||||
functions_mapping["not_equal"](123, "123")
|
||||
|
||||
functions_mapping["length_equals"]("123", 3)
|
||||
# Because the Numbers in a CSV file are by default treated as strings,
|
||||
# you need to convert them to Numbers, and we'll test that out here.
|
||||
functions_mapping["length_equals"]("123", "3")
|
||||
functions_mapping["length_equal"]("123", 3)
|
||||
with self.assertRaises(AssertionError):
|
||||
functions_mapping["length_equals"]("123", "abc")
|
||||
functions_mapping["length_equal"]("123", "3")
|
||||
with self.assertRaises(AssertionError):
|
||||
functions_mapping["length_equal"]("123", "abc")
|
||||
functions_mapping["length_greater_than"]("123", 2)
|
||||
functions_mapping["length_greater_than_or_equals"]("123", 3)
|
||||
functions_mapping["length_greater_or_equals"]("123", 3)
|
||||
|
||||
functions_mapping["contains"]("123abc456", "3ab")
|
||||
functions_mapping["contains"](["1", "2"], "1")
|
||||
|
||||
Reference in New Issue
Block a user