Python 如何使用 pytest 断言列表相等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46914222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I assert lists equality with pytest
提问by bAN
I'm trying to make some unit tests with pytest.
我正在尝试使用pytest进行一些单元测试。
I was thinking about doing things like that:
我正在考虑做这样的事情:
actual = b_manager.get_b(complete_set)
assert actual is not None
assert actual.columns == ['bl', 'direction', 'day']
The first assertion in ok but with the second I have an value error.
第一个断言在 ok 但第二个我有一个值错误。
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I assume it is not the right way to assert the equality of two different lists with pytest.
我认为这不是用 pytest 断言两个不同列表相等的正确方法。
How can I assert that the dataframe columns (a list) is equal to the expected one?
如何断言数据框列(列表)等于预期的列?
Thanks
谢谢
采纳答案by ritchie46
You could do a list comprehension to check equality of all values. If you call all
on the list comprehensions result, it will return True
if all parameters are equal.
你可以做一个列表理解来检查所有值的相等性。如果您调用all
列表推导结果,True
如果所有参数都相等,它将返回。
actual = ['bl', 'direction', 'day']
assert all([a == b for a, b in zip(actual, ['bl', 'direction', 'day'])])
print(all([a == b for a, b in zip(actual, ['bl', 'direction', 'day'])]))
>>> True
回答by Rockallite
See this:
看到这个:
Note:
You can simply use the
assert
statement for asserting test expectations. pytest's Advanced assertion introspectionwill intelligently report intermediate values of the assert expression freeing you from the need to learn the many names of JUnit legacy methods.
笔记:
您可以简单地使用该
assert
语句来断言测试期望。pytest 的高级断言自省将智能地报告断言表达式的中间值,使您无需了解JUnit 遗留方法的许多名称。
And 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
针对多种情况进行了特殊比较:
- 比较长字符串:显示上下文差异
- 比较长序列:第一个失败的索引
- 比较字典:不同的条目
And the 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
See the assertion for lists equality with literal ==
over there? pytest has done the hard work for you.
看到==
那里的列表与文字相等的断言吗?pytest 为您完成了艰苦的工作。
回答by Dylan Orzel
If you are using the built in unittest.TestCase
, there is already a method that can do that for you: unittest.TestCase.assertListEqual
if you care about the list ordering, and unittest.TestCase.assertCountEqual
if you don't.
如果您正在使用内置的unittest.TestCase
,那么已经有一种方法可以为您做到这一点:unittest.TestCase.assertListEqual
如果您关心列表排序,unittest.TestCase.assertCountEqual
如果您不关心。
https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.assertCountEqual
https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.assertCountEqual
回答by Adam Erickson
Use the numpy
function array_equal
:
使用numpy
功能array_equal
:
import numpy as np
test_arrays_equal():
a = np.array([[1, 3], [2, 3], [1, 4], [2, 4], [1, 5], [2, 5]])
b = np.array([[1, 3], [2, 3], [1, 4], [2, 4], [1, 5], [2, 5]])
assert np.array_equal(a, b)
回答by GGGuser
Usually you want to assert that all elements from one list are present in another, you probably do not care about indexes. Imagine you have:
通常你想断言一个列表中的所有元素都存在于另一个列表中,你可能不关心索引。想象一下你有:
a = [1, 2, 3]
b = [3, 2, 1]
Doing just:
只做:
assert a == b
Will fail as indexes do not match.
Instead you should use sets:
将失败,因为索引不匹配。
相反,您应该使用集合:
assert set(a) == set(b)
If there is a possibility of duplicates, you should check that:
如果存在重复的可能性,您应该检查:
assert len(a) == len(set(a))
回答by salah
convert the numpy arrays to python lists and you'll get a better response than simply using all
or any
. This way, if the test fails you'll see how the expected vs actual differ
将 numpy 数组转换为 python 列表,您将获得比简单使用all
or更好的响应any
。这样,如果测试失败,您将看到预期与实际的差异