【问题标题】:Python Hamcrest - test that list is not emptyPython Hamcrest - 测试该列表不为空
【发布时间】:2013-05-13 14:59:02
【问题描述】:

是否有一种干净的方法来测试 Hamcrest (python) 中的列表是否为空。有几种方法可以通过检查列表长度等来做到这一点,但我想要一些读起来很适合测试的东西。

【问题讨论】:

    标签: python hamcrest


    【解决方案1】:

    使用empty 匹配器:

    assert_that(mylist, is_not(empty()))
    

    【讨论】:

    • 谢谢,但我不能导入空的,它似乎不存在于 pyhamcrest 中。
    • 看起来缺少 hamcrest/library/collection/__init__.py from is_empty import empty。进行修复,然后提交拉取请求。
    【解决方案2】:

    我正在使用assertNotEqual([], list) 来检查列表是否为空

    【讨论】:

      【解决方案3】:

      具有最佳可读性的最简洁的方式可能是这样的:

      from hamcrest import assert_that, is_, empty, not_
      
      assert_that([], is_(not_(empty())))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-24
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多