Python Assert 為何不盡如人意?
Python中的斷言用起來非常簡單,你可以在assert后面跟上任意判斷條件,如果斷言失敗則會拋出異常。
>>> assert 1 + 1 == 2>>> assert isinstance('Hello', str)>>> assert isinstance('Hello', int)Traceback (most recent call last): File "<input>", line 1, in <module>AssertionError
其實assert看上去不錯,然而用起來并不爽。就比如有人告訴你程序錯了,但是不告訴哪里錯了。很多時候這樣的assert還不如不寫,寫了我就想罵娘。直接拋一個異常來得更痛快一些。
改進方案 #1
一個稍微改進一丟丟的方案就是把必要的信息也放到assert語句后面,比如這樣。
>>> s = "nothin is impossible.">>> key = "nothing">>> assert key in s, "Key: '{}' is not in Target: '{}'".format(key, s)Traceback (most recent call last): File "<input>", line 1, in <module>AssertionError: Key: 'nothing' is not in Target: 'nothin is impossible.'
看上去還行吧,但是其實寫的很蛋疼。假如你是一名測試汪,有成千上萬的測試案例需要做斷言做驗證,相信你面對以上做法,心中一定有千萬只那種馬奔騰而過。
改進方案 #2
不管你是你是搞測試還是開發的,想必聽過不少測試框架。你猜到我要說什么了吧?對,不用測試框架里的斷言機制,你是不是灑。
py.test
py.test 是一個輕量級的測試框架,所以它壓根就沒寫自己的斷言系統,但是它對Python自帶的斷言做了強化處理,如果斷言失敗,那么框架本身會盡可能多地提供斷言失敗的原因。那么也就意味著,用py.test實現測試,你一行代碼都不用改。
import pytestdef test_case(): expected = "Hello" actual = "hello" assert expected == actualif __name__ == '__main__': pytest.main()"""================================== FAILURES ===================================__________________________________ test_case __________________________________ def test_case(): expected = "Hello" actual = "hello"> assert expected == actualE assert 'Hello' == 'hello'E - HelloE ? ^E + helloE ? ^assertion_in_python.py:7: AssertionError========================== 1 failed in 0.05 seconds ===========================""""
unittest
Python自帶的unittest單元測試框架就有了自己的斷言方法self.assertXXX()
,而且不推薦使用assert XXX
語句。
import unittestclass TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FoO')if __name__ == '__main__': unittest.main() """FailureExpected :'FOO'Actual :'FoO'Traceback (most recent call last): File "assertion_in_python.py", line 6, in test_upper self.assertEqual('foo'.upper(), 'FoO')AssertionError: 'FOO' != 'FoO'"""
新聞熱點
疑難解答