pyhamcrest 包含字典列表的匹配器
pyhamcrest contains matcher for list of dicts
为什么 pyhamcrest 失败 contains() 2 个或更多字典列表的匹配器,但适用于 1 个字典列表?
为字典列表编写 hamcrest (pyhamcrest) 匹配器的最佳方法是什么?
在此单元测试示例中,第一个测试通过,第二个测试失败。
import unittest
from hamcrest import contains, assert_that, has_entries
class ContainsTest(unittest.TestCase):
dict1, dict2 = {"a": 1, "b": 2}, {"a": 1, "b": 2}
sequence1, sequence2 = list(), list()
sequence1.append(dict1)
sequence2.append(dict1)
sequence2.append(dict2)
@staticmethod
def test_sequence():
assert_that(ContainsTest.sequence1, contains(has_entries({'a': 1, 'b': 2})))
assert_that(ContainsTest.sequence2, contains(has_entries({'a': 1, 'b': 2})))
if __name__ == "__main__":
ContainsTest.test_sequence()
单元测试输出:
File "/usr/local/lib/python3.6/site-packages/hamcrest/core/assert_that.py",
line 57, in _assert_match raise AssertionError(description)
AssertionError:
Expected: a sequence containing [a dictionary containing {'a': <1>, 'b': <2>}]
but: Not matched: <{'a': 1, 'b': 2}>
Ran 1 test in 0.027s
FAILED (failures=1)
Process finished with exit code 1
您正在寻找 has_item
,而不是 contains
。
有点令人困惑,PyHamcrest 的 contains
并不是 __contains__
魔术方法意义上的遏制检查。它的语义是模仿原始 Hamcrest 的 contains
matcher。它不是在测试列表中的某些元素是否与给定的匹配器匹配;它希望每个列表元素都有一个单独的匹配器,并将匹配器应用于列表的相应元素。 has_item
是检查某些项目是否匹配的。
引用 docs:
Matches if sequence’s elements satisfy a given list of matchers, in
order.
Parameters: match1,... – A comma-separated list of matchers.
This matcher iterates the evaluated sequence and a given list of matchers,
seeing if each element satisfies its corresponding matcher.
Any argument that is not a matcher is implicitly wrapped in an
equal_to matcher to check for equality.
您已经为包含两个元素的列表提供了一个匹配器。 PyHamcrest 需要两个匹配器。第二个字典没有得到匹配。
为什么 pyhamcrest 失败 contains() 2 个或更多字典列表的匹配器,但适用于 1 个字典列表?
为字典列表编写 hamcrest (pyhamcrest) 匹配器的最佳方法是什么?
在此单元测试示例中,第一个测试通过,第二个测试失败。
import unittest
from hamcrest import contains, assert_that, has_entries
class ContainsTest(unittest.TestCase):
dict1, dict2 = {"a": 1, "b": 2}, {"a": 1, "b": 2}
sequence1, sequence2 = list(), list()
sequence1.append(dict1)
sequence2.append(dict1)
sequence2.append(dict2)
@staticmethod
def test_sequence():
assert_that(ContainsTest.sequence1, contains(has_entries({'a': 1, 'b': 2})))
assert_that(ContainsTest.sequence2, contains(has_entries({'a': 1, 'b': 2})))
if __name__ == "__main__":
ContainsTest.test_sequence()
单元测试输出:
File "/usr/local/lib/python3.6/site-packages/hamcrest/core/assert_that.py",
line 57, in _assert_match raise AssertionError(description)
AssertionError:
Expected: a sequence containing [a dictionary containing {'a': <1>, 'b': <2>}]
but: Not matched: <{'a': 1, 'b': 2}>
Ran 1 test in 0.027s
FAILED (failures=1)
Process finished with exit code 1
您正在寻找 has_item
,而不是 contains
。
有点令人困惑,PyHamcrest 的 contains
并不是 __contains__
魔术方法意义上的遏制检查。它的语义是模仿原始 Hamcrest 的 contains
matcher。它不是在测试列表中的某些元素是否与给定的匹配器匹配;它希望每个列表元素都有一个单独的匹配器,并将匹配器应用于列表的相应元素。 has_item
是检查某些项目是否匹配的。
引用 docs:
Matches if sequence’s elements satisfy a given list of matchers, in order.
Parameters: match1,... – A comma-separated list of matchers.
This matcher iterates the evaluated sequence and a given list of matchers, seeing if each element satisfies its corresponding matcher.Any argument that is not a matcher is implicitly wrapped in an equal_to matcher to check for equality.
您已经为包含两个元素的列表提供了一个匹配器。 PyHamcrest 需要两个匹配器。第二个字典没有得到匹配。