pytest更好的列表和可编辑的断言消息
pytest-iterassert的Python项目详细描述
皮特斯特伊特拉塞特
您是否曾经想在单元测试中使用all
或any
,但是发现了断言
缺少信息?对集合中的类属性进行断言
让你希望你是用Java编写代码(有一个很好的断言框架)?那么
这是你的pytest助手!
pytest-iterassert提供
all_match
和{
示例
def test_generator_without_iterassert() -> None: > assert all(i < 1 for i in range(3)) E assert False E + where False = all(<genexpr> at 0x10221a250>)
all_match
和{
举个更复杂的例子怎么样?断言类实例的属性是 很普通。在
def test_attr_of_classes_without_iterassert() -> None: foos = [Foo(1), Foo(2), Foo(3)] > assert all(foo.bar < 3 for foo in foos) E assert False E + where False = all(<genexpr> at 0x10597ca50>)
iterassert
使将函数应用于iterable变得很容易,并将说服
pytest显示该函数的结果!在
def test_attr_of_classes_with_iterassert_1() -> None: foos = [Foo(1), Foo(2), Foo(3)] > assert all_match(foos, get_bar) < 3 E assert all(9001, 9002, 9003) < 3 E + where all(9001, 9002, 9003) = all_match([<Foo(1)>, <Foo(2)>, <Foo(3)>], get_bar)
也可以对所有项目运行更复杂的检查,方法是 在函数内部检查:
def test_attr_of_classes_with_iterassert_2() -> None: foos = [Foo(1), Foo(2), Foo(3)] > assert all_match(foos, check_bar) E assert all(False, False, False) E + where all(False, False, False) = all_match([<Foo(1)>, <Foo(2)>, <Foo(3)>], check_bar)
注意,在本例中,与内置函数all_match
和{
而且,如果您需要合并更多的转换,但希望看到
中间项,capture
也允许这样做:
def test_attr_of_classes_with_iterassert_3() -> None: foos = [Foo(1), Foo(2), Foo(3)] > assert all_match(capture(foo.bar for foo in foos), check_val) E assert all(False, False, False) E + where all(False, False, False) = all_match([9001, 9002, 9003], check_val) E + where [9001, 9002, 9003] = capture(<genexpr> at 0x1031220d0>)
即使是测试总结也说明了这一切:
FAILED example.py::test_generator_without_iterassert - assert False FAILED example.py::test_generator_with_iterassert - assert all(0, 1, 2) < 1 FAILED example.py::test_attr_of_classes_without_iterassert - assert False FAILED example.py::test_attr_of_classes_with_iterassert_1 - assert all(9001, 9002, 9003) < 3 FAILED example.py::test_attr_of_classes_with_iterassert_2 - assert all(False, False, False) FAILED example.py::test_attr_of_classes_with_iterassert_3 - assert all(False, False, False)
安装
pytest-iterassert is on
PyPI,因此您可以简单地安装
viapip install pytest-iterassert
(需要python3.6或更高版本)。在
(如果你真的很勇敢,你也可以将all_match
和{
变更日志
[0.0.3]-2020-05-10
- 添加
capture
,并允许all_match
和{}不接受 运算符/操作数,用于映射函数内部的检查
[0.0.2]-2020-05-07
- 初次发行
发展
此库使用Poetry进行管理
依赖关系。您只需要运行poetry install
,它将创建一个
安装了所有开发人员依赖项的虚拟环境。在
请在提交请求之前运行poetry run ./lint
。在
许可证
此库是根据Mozilla Public License 2.0版授权的。更多
有关信息,请参阅LICENSE
。在
- 项目
标签: