用于执行断言并提供信息丰富的异常消息的包。

AssertionLib的Python项目详细描述


https://readthedocs.org/projects/assertionlib/badge/?version=latesthttps://badge.fury.io/py/AssertionLib.svghttps://github.com/nlesc-nano/AssertionLib/workflows/Python%20package/badge.svghttps://codecov.io/gh/nlesc-nano/AssertionLib/branch/master/graph/badge.svghttps://zenodo.org/badge/214183943.svg

https://img.shields.io/badge/python-3.6-blue.svghttps://img.shields.io/badge/python-3.7-blue.svghttps://img.shields.io/badge/python-3.8-blue.svghttps://img.shields.io/badge/python-3.9-blue.svg

断言IB 3.1.3

用于执行断言并提供信息丰富的异常消息的包。在

安装

  • PyPi:pip install AssertionLib
  • GitHub:pip install git+https://github.com/nlesc-nano/AssertionLib

用法

所有可用断言方法的综合概述是 在documentation中提供。 一些基本断言的例子:

>>>importnumpyasnp>>>fromassertionlibimportassertion# Assert the output of specific callables>>>assertion.eq(5,5)# 5 == 5>>>assertion.lt(5,6)# 5 < 6>>>assertion.gt(6,5)# 5 > 6>>>assertion.isinstance(5,int)>>>assertion.hasattr(5,'__init__')>>>assertion.any([False,False,True])>>>assertion.isfinite(1.0)# Simply assert a value>>>assertion(5==5)>>>assertion(isinstance(5,int))# Apply post-processing before conducting the assertion>>>ar_large=np.ones(10)>>>ar_small=np.zeros(10)>>>assertion.gt(ar_large,ar_small,post_process=np.all)# all(ar_large > ar_small)# Perform an assertion which will raise an AssertionError>>>assertion.eq(5,6,message='Fancy custom error message')# 5 == 6Traceback(mostrecentcalllast):...AssertionError:output=eq(a,b);assertoutputexception:AssertionError=AssertionError('Fancy custom error message')output:bool=Falsea:int=5b:int=6

由于方法签名不正确而引发的AssertionError的几个示例:

^{pr2}$
>>>fromassertionlibimportassertion>>>assertion.eq(5,5,5,5)Traceback(mostrecentcalllast):...AssertionError:output=eq(a,b,_a,_b);assertoutputexception:TypeError=TypeError('eq expected 2 arguments, got 4')output:NoneType=Nonea:int=5b:int=5_a:int=5_b:int=5

exception参数的演示。 提供异常类型将断言所提供的异常已引发 在断言过程中/之前:

>>>fromassertionlibimportassertion>>>len(5)Traceback(mostrecentcalllast):...TypeError:objectoftype'int'hasnolen()
>>>fromassertionlibimportassertion>>>assertion.len(5,exception=TypeError)# i.e. len(5) should raise a TypeError>>>assertion.len([5],exception=TypeError)Traceback(mostrecentcalllast):...AssertionError:output=len(obj);assertoutputexception:AssertionError=AssertionError("Failed to raise 'TypeError'")output:int=1obj:list=[5]

最后,可以使用以下两种自定义输出方式中的一种进行断言, 向AssertionManager.assert()提供可调用属性或创建自定义断言 方法并使用AssertionManager.add_to_instance()将其添加到实例中:

>>>fromassertionlibimportassertion>>>defmy_fancy_func(a:object)->bool:...returnFalse# Approach #1, supply to-be asserted callable to assertion.assert_()>>>assertion.assert_(my_fancy_func,5)Traceback(mostrecentcalllast):...AssertionError:output=my_fancy_func(a);assertoutputexception:AssertionError=AssertionError(None)output:bool=Falsea:int=5
>>>fromassertionlibimportassertion# Approach #2, permanantly add a new bound method using assertion.add_to_instance()>>>assertion.add_to_instance(my_fancy_func)>>>assertion.my_fancy_func(5)Traceback(mostrecentcalllast):...AssertionError:output=my_fancy_func(a);assertoutputexception:AssertionError=AssertionError(None)output:bool=Falsea:int=5

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用于切换状态结果的枚举的java输入   检测*NIX上打开的端口时出现java问题   java捕获和保存屏幕截图   java SLF4JLogback:基于日志级别的多模式   Java从字符串中删除动态子字符串   在spring引导中contextLoads测试时运行java Liquibase   基于规则集的任意嵌套POJO的java任意就地转换   java如何做,边做边循环这段代码?   java是什么导致jmh测量中的错误?   java Spring RabbitTemplate执行方法可见性   java jms创建连接http超时weblogic   java如何在JMapViewer中的两点之间放置像箭头一样的图像   在我的形状计算器Java程序中使用带有Switch/Case语句的循环   字符串如何在Java中比较字符和数字