python中的断言应该尽可能清晰易读
should_dsl的Python项目详细描述
shouldsl的目标是使用“几乎”自然语言(有时受python语言约束的限制),用python尽可能清晰易读地编写should期望。
为了使用这个dsl,需要从should_dsl模块导入should和should_not对象。
例如:
>>> from should_dsl import should >>> 1 |should| equal_to(1) >>> 'should' |should| include('oul') >>> 3 |should| be_into([0, 1, 2]) Traceback (most recent call last): ... ShouldNotSatisfied: 3 is not into [0, 1, 2]
equal_tomatcher验证对象相等性。如果要确保身份,必须使用be作为匹配项:
>>> 2 |should| be(2)
异常的一个很好的例子是:
>>> def raise_zerodivisionerror(): ... return 1/0 >>> raise_zerodivisionerror |should| throw(ZeroDivisionError)
should的版本为负:should_not:
>>> from should_dsl import should_not >>> 2 |should_not| be_into([1, 3, 5]) >>> 'should' |should_not| include('oul') Traceback (most recent call last): ... ShouldNotSatisfied: 'should' does include 'oul'