我刚刚开始学习python。当前正在编写单元测试以断言期望列表中的元素是否存在于实际列表中 但是我遇到类似的错误 为了断言期望列表中的项目“ a”是否存在于实际列表中,我应该如何以及使用什么序列匹配器? 答案 0 :(得分:1) 您应该只使用 我们使用可循环拆包的方式将列表作为参数输入,现在当您运行它时,它应该不会出错。 答案 1 :(得分:0) 我对def test_compare_list_of_items():
actual_list_of_items = ['a','b']
expected_list_of_items = ['a']
assert_that(actual_list_of_items, has_item(has_items(expected_list_of_items)))
E Expected: a sequence containing (a sequence containing <['a']>)
E but: was <['a', 'b']>
2 个答案:
has_item
时才使用has_items
。根据{{3}},这需要多个匹配器。然后您的功能变为def test_compare_list_of_items():
actual_list_of_items = ['a','b']
expected_list_of_items = ['a']
assert_that(actual_list_of_items, has_items(*expected_list_of_items))
has_items
函数一无所知,但是您可以使用这样的东西吗?assertTrue(all(item in expected_list_of_items for item in actual_list_of_items))