gpt4 book ai didi

python - 我如何断言列表与 pytest 相等

转载 作者:太空狗 更新时间:2023-10-29 20:32:20 25 4
gpt4 key购买 nike

我正在尝试使用 pytest 进行一些单元测试。

我正在考虑做这样的事情:

actual = b_manager.get_b(complete_set)
assert actual is not None
assert actual.columns == ['bl', 'direction', 'day']

第一个断言没问题,但第二个断言出现值错误。

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我认为用 pytest 断言两个不同列表的相等性不是正确的方法。

我如何断言数据框列(列表)等于预期列?

谢谢

最佳答案

参见 this :

Note:

You can simply use the assert statement for asserting test expectations. pytest’s Advanced assertion introspection will intelligently report intermediate values of the assert expression freeing you from the need to learn the many names of JUnit legacy methods.

this :

Special comparisons are done for a number of cases:

  • comparing long strings: a context diff is shown
  • comparing long sequences: first failing indices
  • comparing dicts: different entries

还有 reporting demo :

failure_demo.py:59: AssertionError
_______ TestSpecialisedExplanations.test_eq_list ________

self = <failure_demo.TestSpecialisedExplanations object at 0xdeadbeef>

def test_eq_list(self):
> assert [0, 1, 2] == [0, 1, 3]
E assert [0, 1, 2] == [0, 1, 3]
E At index 2 diff: 2 != 3
E Use -v to get the full diff

看到那边关于列表与文字 == 相等的断言了吗? pytest 已经为您完成了艰苦的工作。

关于python - 我如何断言列表与 pytest 相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46914222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com