should.js 的 Python 版:should

jopen 10年前

Python 版本的 should 断言库

有借鉴这个 pyshould

TODO

  • 原 should 的其它接口
  • keys 取反的逻辑需要重新考虑
  • 详尽一点的文档

既然有了 pyshould, 为什么要重新造轮子?

  • an, of, a, and, be, have, with, which 这些应该作为链式调用的属性存在而不是 be_integer 这样的函数前缀

  • 不适应 pyshould 中重载运算符的做法

借鉴的地方?

因为 Python 里的匿名函数没有 JavaScript 中那样强大, 对于 throw 的测试没有采用原 should 的方式, 而选择用 pyshould 中 with 的方式

  • 借鉴个毛啊, 虽然 Python 的匿名函数不那么强大, 但应付 raise 完全足够了

安装:

pip install should

使用方法:

from should import it    # 一般的断言  it(1).should.be.int  it({}).should.be.no.ok  it(2).should.be.equal(2)  it(10).should.be.no.equal(8)  it([1,2,3]).should.contain(3)    # with 版异常断言, 不支持 no, 在 0.5 版本会被删除  with should.raises(ValueError):      int('abc')    # lambda 版异常断言  it(lambda: int('abc')).should.throw(ValueError)  it(lambda: int('123')).should.no.throw(ValueError)

不足:

  • 因为 Python 语言特性, 不能对 type 或 object 进行 mixin. 所以只能使用静态调用的方式. 最前面用 it 调用句子通一点了

  • Python 中逻辑词 and,not,with 是保留字, 可以作为属性名称存在, 但直接用点号(.)调用解释器会报语法错误... 所以取反属性用 no. 连结词就用also 代替了.

项目主页:http://www.open-open.com/lib/view/home/1418869174698