should.js 的 Python 版:should
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)
- 更多例子請參考 test.py
不足:
-
因為 Python 語言特性, 不能對 type 或 object 進行 mixin. 所以只能使用靜態調用的方式. 最前面用
it
調用句子通一點了 -
Python 中邏輯詞
and
,not
,with
是保留字, 可以作為屬性名稱存在, 但直接用點號(.)調用解釋器會報語法錯誤... 所以取反屬性用no
. 連結詞就用also
代替了.
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!