4Manuals

  • PDF Cloud HOME

pyhamcrest-比较两个列表 Download

    如何将文本边界框与pyplot.Rectangle对齐? 导入类问题 在Python数据框中选择列时出错 使用Rabbit的pika确认消息 如何在按住键的同时暂停VideoStream? Python OpenCV TypeError:无法处理此数据类型 使用buildozer不会下载sdl2_image SMTPSenderRefused,421,超出超时 Tensorflow多线程推理比单线程推理慢 关于python中变量的困惑。 python如何使用变量?

我刚刚开始学习python。当前正在编写单元测试以断言期望列表中的元素是否存在于实际列表中

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']>

为了断言期望列表中的项目“ a”是否存在于实际列表中,我应该如何以及使用什么序列匹配器?

2 个答案:

答案 0 :(得分:1)

您应该只使用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))

我们使用可循环拆包的方式将列表作为参数输入,现在当您运行它时,它应该不会出错。

答案 1 :(得分:0)

我对has_items函数一无所知,但是您可以使用这样的东西吗?

assertTrue(all(item in expected_list_of_items for item in actual_list_of_items))



Similar searches
    我的键盘调用了click(),但click()没有委托 pyspark等效于将常量数组作为列添加到数据框 kubernetes服务无法访问多端口服务上的端口 如何创建一个动态的SectionList? 无法创建ADT