【问题标题】:pytest assertion results in false but does not give more information about failurepytest 断言结果为假,但未提供有关失败的更多信息
【发布时间】:2022-01-04 04:09:50
【问题描述】:

我是 pytest 新手,下面是我的代码。我正在使用断言。断言工作正确,这是错误的,因为数据不匹配。我需要帮助来捕获哪些数据不匹配。


def test_content_database():
    records_src = [(1,James,smith,123),(2,Granpa,smith,124),(3,Linda,smith,123)]
    records_trg = [(1,James,smith,**124**),(2,Granpa,**ron**,124),(3,Linda,smith,123)]
    print("ASSERTION RESULTED IN : ", all([a == b for a, b in zip(records_src, records_trg)]))
    assert all([a == b for a, b in zip(records_src, records_trg)]), "DATA IS NOT MATCHING"

**控制台输出是错误的,这是正确的,但我想捕获哪些数据不匹配**


main_test.py:8 (test_content_database)
def test_content_database():
        records_src = dbcon.get_data_connection_src()
        records_trg = dbcon.get_data_connection_trg_diff()
        print("ASSERTION RESULTED IN : ", all([a == b for a, b in zip(records_src, records_trg)]))
>       assert all([a == b for a, b in zip(records_src, records_trg)]), "DATA IS NOT MATCHING"
E    AssertionError: DATA IS NOT MATCHING
E    assert False
E     +  where False = all([False, False, True, True, True, True, ...])

main_test.py:13: AssertionError

【问题讨论】:

  • 你不能。 all 本身不会返回比TrueFalse 更多的信息。如果您想确切知道哪些元素是False,您将不得不手动遍历列表
  • 我建议不要将太多内容打包到单行断言中——你最好有一个包含单个断言的实际循环(然后 pytest 会显示差异)跨度>

标签: python pytest assert


【解决方案1】:

assert records_src == records_trg 怎么样,因为无论如何你都在检查相等性?

附带说明,您可以添加一个 pytest cli 选项 - --show-locals

【讨论】:

    猜你喜欢
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2021-12-03
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多