From b484c24a3e27482c968089f1e1792a34ed68c0dc Mon Sep 17 00:00:00 2001 From: Long Date: Wed, 11 Dec 2019 17:06:40 +0800 Subject: [PATCH] =?UTF-8?q?bug=20fix:=20=E5=BD=93=E4=BD=BF=E7=94=A8.csv?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=86=85=E5=AE=B9=E6=9D=A5=E5=81=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=A9=B1=E5=8A=A8=E6=B5=8B=E8=AF=95=E6=97=B6,=20lengt?= =?UTF-8?q?h=5Fxxx=E5=87=BD=E6=95=B0=E9=83=BD=E4=BC=9A=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- httprunner/builtin/comparators.py | 22 +++++++++++++++++----- tests/test_utils.py | 5 +++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/httprunner/builtin/comparators.py b/httprunner/builtin/comparators.py index 3a522993..586a5965 100644 --- a/httprunner/builtin/comparators.py +++ b/httprunner/builtin/comparators.py @@ -37,27 +37,32 @@ def string_equals(check_value, expect_value): def length_equals(check_value, expect_value): assert isinstance(expect_value, integer_types) - assert len(check_value) == expect_value + expect_len = _cast_to_int(expect_value) + assert len(check_value) == expect_len def length_greater_than(check_value, expect_value): assert isinstance(expect_value, integer_types) - assert len(check_value) > expect_value + expect_len = _cast_to_int(expect_value) + assert len(check_value) > expect_len def length_greater_than_or_equals(check_value, expect_value): assert isinstance(expect_value, integer_types) - assert len(check_value) >= expect_value + expect_len = _cast_to_int(expect_value) + assert len(check_value) >= expect_len def length_less_than(check_value, expect_value): assert isinstance(expect_value, integer_types) - assert len(check_value) < expect_value + expect_len = _cast_to_int(expect_value) + assert len(check_value) < expect_len def length_less_than_or_equals(check_value, expect_value): assert isinstance(expect_value, integer_types) - assert len(check_value) <= expect_value + expect_len = _cast_to_int(expect_value) + assert len(check_value) <= expect_len def contains(check_value, expect_value): @@ -97,3 +102,10 @@ def startswith(check_value, expect_value): def endswith(check_value, expect_value): assert builtin_str(check_value).endswith(builtin_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)) \ No newline at end of file diff --git a/tests/test_utils.py b/tests/test_utils.py index c61eaabe..6675188f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -80,6 +80,11 @@ class TestUtils(ApiServerUnittest): functions_mapping["not_equals"](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') + with self.assertRaises(AssertionError): + functions_mapping["length_equals"]("123", 'abc') functions_mapping["length_greater_than"]("123", 2) functions_mapping["length_greater_than_or_equals"]("123", 3)