assert斷言語句調試程序

以前工做在測試中常常會碰到斷言掛起,每次出現斷言只知道有故障了,可是並無深究斷言是怎麼回事,今天學習了python中的斷言語句,其實挺簡單。python

1.assert語句的語法:express

assert expression [,reason]app

expression :表達式爲真時,什麼都不作,表達式爲假時,拋出AssertionError異常;學習

reason:可選參數,用於對判斷條件進行描述,爲了之後更好的知道哪裏出現了問題。測試

2.assert語句只在調試階段有效。因此咱們平時測試用的debug版本纔會有斷言掛起,release版本沒有斷言。spa

apple=int(input("請輸入蘋果數量:")) children=int(input("請輸入小朋友的數量:")) assert apple>children,"蘋果不夠分"
#結果
請輸入蘋果數量:10 請輸入小朋友的數量:20 Traceback (most recent call last): File "C:\Python27\main.py", line 9, in <module>
    assert apple>children,"蘋果不夠分" AssertionError: 蘋果不夠分 >>>
相關文章
相關標籤/搜索