Python中斷言Assertion的一些改進(jìn)方案
Python Assert 為何不盡如人意?
Python中的斷言用起來非常簡(jiǎn)單,你可以在assert后面跟上任意判斷條件,如果斷言失敗則會(huì)拋出異常。
>>> assert 1 + 1 == 2
>>> assert isinstance('Hello', str)
>>> assert isinstance('Hello', int)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AssertionError
其實(shí)assert看上去不錯(cuò),然而用起來并不爽。就比如有人告訴你程序錯(cuò)了,但是不告訴哪里錯(cuò)了。很多時(shí)候這樣的assert還不如不寫,寫了我就想罵娘。直接拋一個(gè)異常來得更痛快一些。
改進(jìn)方案 #1
一個(gè)稍微改進(jìn)一丟丟的方案就是把必要的信息也放到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.'
看上去還行吧,但是其實(shí)寫的很蛋疼。假如你是一名測(cè)試汪,有成千上萬的測(cè)試案例需要做斷言做驗(yàn)證,相信你面對(duì)以上做法,心中一定有千萬只那種馬奔騰而過。
改進(jìn)方案 #2
不管你是你是搞測(cè)試還是開發(fā)的,想必聽過不少測(cè)試框架。你猜到我要說什么了吧?對(duì),不用測(cè)試框架里的斷言機(jī)制,你是不是灑。
py.test
py.test 是一個(gè)輕量級(jí)的測(cè)試框架,所以它壓根就沒寫自己的斷言系統(tǒng),但是它對(duì)Python自帶的斷言做了強(qiáng)化處理,如果斷言失敗,那么框架本身會(huì)盡可能多地提供斷言失敗的原因。那么也就意味著,用py.test實(shí)現(xiàn)測(cè)試,你一行代碼都不用改。
import pytest
def test_case():
expected = "Hello"
actual = "hello"
assert expected == actual
if __name__ == '__main__':
pytest.main()
"""
================================== FAILURES ===================================
__________________________________ test_case __________________________________
def test_case():
expected = "Hello"
actual = "hello"
> assert expected == actual
E assert 'Hello' == 'hello'
E - Hello
E ? ^
E + hello
E ? ^
assertion_in_python.py:7: AssertionError
========================== 1 failed in 0.05 seconds ===========================
""""
unittest
Python自帶的unittest單元測(cè)試框架就有了自己的斷言方法self.assertXXX() ,而且不推薦使用assert XXX語句。
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FoO')
if __name__ == '__main__':
unittest.main()
"""
Failure
Expected :'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'
"""
ptest
我非常喜歡ptest,感謝Karl大神寫了這么一個(gè)測(cè)試框架。ptest中的斷言可讀性很好,而且通過IDE的智能提示你能輕松完成各種斷言語句。
from ptest.decorator import *
from ptest.assertion import *
@TestClass()
class TestCases:
@Test()
def test1(self):
actual = 'foo'
expected = 'bar'
assert_that(expected).is_equal_to(actual)
"""
Start to run following 1 tests:
------------------------------
...
[demo.assertion_in_python.TestCases.test1@Test] Failed with following message:
...
AssertionError: Unexpectedly that the str <bar> is not equal to str <foo>.
"""
改進(jìn)方案 #3
不僅僅是你和我對(duì)Python中的斷言表示不滿足,所以大家都爭(zhēng)相發(fā)明自己的assert包。在這里我強(qiáng)烈推薦assertpy 這個(gè)包,它異常強(qiáng)大而且好評(píng)如潮。
pip install assertpy
看例子:
from assertpy import assert_that
def test_something():
assert_that(1 + 2).is_equal_to(3)
assert_that('foobar')\
.is_length(6)\
.starts_with('foo')\
.ends_with('bar')
assert_that(['a', 'b', 'c'])\
.contains('a')\
.does_not_contain('x')
從它的主頁文檔上你會(huì)發(fā)現(xiàn)它支持了幾乎你能想到的所有測(cè)試場(chǎng)景,包括但不限于以下列表。
Strings
Numbers
Lists
Tuples
Dicts
Sets
Booleans
Dates
Files
Objects
而且它的斷言信息簡(jiǎn)潔明了,不多不少。
Expected <foo> to be of length <4>, but was <3>. Expected <foo> to be empty string, but was not. Expected <False>, but was not. Expected <foo> to contain only digits, but did not. Expected <123> to contain only alphabetic chars, but did not. Expected <foo> to contain only uppercase chars, but did not. Expected <FOO> to contain only lowercase chars, but did not. Expected <foo> to be equal to <bar>, but was not. Expected <foo> to be not equal to <foo>, but was. Expected <foo> to be case-insensitive equal to <BAR>, but was not.
在發(fā)現(xiàn)assertpy之前我也想寫一個(gè)類似的包,盡可能通用一些。但是現(xiàn)在,我為毛要重新去造輪子?完全沒必要!
總結(jié)
斷言在軟件系統(tǒng)中有非常重要的作用,寫的好可以讓你的系統(tǒng)更穩(wěn)定。Python中默認(rèn)的斷言語句其實(shí)還有一個(gè)作用,如果你寫了一個(gè)類型相關(guān)的斷言,IDE會(huì)把這個(gè)對(duì)象當(dāng)成這種類型,這時(shí)候智能提示就有如神助。
要不要把內(nèi)置的斷言語句換成可讀性更好功能更強(qiáng)大的第三方斷言,完全取決于實(shí)際情況。比如你真的需要驗(yàn)證某個(gè)東西并且很關(guān)心驗(yàn)證結(jié)果,那么必須不能用簡(jiǎn)單的assert;如果你只是擔(dān)心某個(gè)點(diǎn)可能有坑或者讓IDE認(rèn)識(shí)某個(gè)對(duì)象,用內(nèi)置的assert既簡(jiǎn)單又方便。
所以說,項(xiàng)目經(jīng)驗(yàn)還是蠻重要的。以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Python 使用folium繪制leaflet地圖的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Python 使用folium繪制leaflet地圖的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python爬蟲報(bào)錯(cuò)<response [406]>(已解決)
本文主要介紹了Python爬蟲報(bào)錯(cuò)<response [406]>,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python基于隱馬爾可夫模型實(shí)現(xiàn)中文拼音輸入
這篇文章主要介紹了python基于隱馬爾可夫模型實(shí)現(xiàn)中文拼音輸入的相關(guān)資料,需要的朋友可以參考下2016-04-04
python腳本設(shè)置系統(tǒng)時(shí)間的兩種方法
這篇文章主要介紹了python腳本設(shè)置系統(tǒng)時(shí)間的兩種方法,其一是調(diào)用socket直接發(fā)送udp包到國(guó)家授時(shí)中心,其二是調(diào)用ntplib包,感興趣的小伙伴們可以參考一下2016-02-02
Python中requests庫的學(xué)習(xí)方法詳解
這篇文章主要為大家詳細(xì)介紹了Python中requests庫的學(xué)習(xí)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
Python實(shí)現(xiàn)桶排序與快速排序算法結(jié)合應(yīng)用示例
這篇文章主要介紹了Python實(shí)現(xiàn)桶排序與快速排序算法結(jié)合應(yīng)用,結(jié)合實(shí)例形式分析了Python快速排序及桶排序結(jié)合應(yīng)用的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11

