python 如何用 Hypothesis 來自動化單元測試
高質量的代碼離不開單元測試,而設計單元測試的用例往往又比較耗時,而且難以想到一些極端情況,本文講述如何使用 Hypothesis 來自動化單元測試
刷過力扣算法題的同學都知道,有時候覺得代碼已經很完善了,一提交才發現很多情況沒有考慮到。然后感嘆力扣的單元測試真的牛比。
因此,高質量的代碼離不開單元測試,如果現在還沒有寫過單元測試,建議先去學習以下常用的單元測試庫[1],只要實踐過,才能感受到本文開頭提到的那些痛點。
Hypothesis 是一個 Python 庫,用于讓單元測試編寫起來更簡單,運行時功能更強大,可以在代碼中查找您不會想到的極端情況。它穩定,強大且易于添加到任何現有測試框架中。它的工作原理是讓您編寫斷言每種情況都應該正確的測試,而不僅僅是您偶然想到的那些。
Hypothesis 的基礎知識典型的單元測試需要自己寫一些測試用例,然后編寫測試函數,通過一段代碼運行它,然后根據預期結果檢查結果。
Hypothesis 有所不同。它是基于屬性進行單元測試。它通過生成與您的規范匹配的任意數據并檢查在這種情況下程序是否仍然有效。如果找到了一個失敗的用例,它將采用該示例并將其測試用例范圍縮減縮減為一定尺寸,然后對其進行簡化,直到找到一個仍會導致問題的小得多的示例。然后將其保存,后續單元測試時仍會使用這些用例。
現在就讓我們看看怎么用吧。
Hypothesis 快速入門1、安裝可以通過 pip 安裝,也可以通過源代碼安裝[2],也可以安裝一些擴展[3],如下:
pip install hypothesispip install hypothesis[pandas,django]2、使用
先寫一段代碼,保存在 mycode.py 中,功能是對字符串進行特定的編碼和解碼,內容如下:
def encode(input_string): count = 1 prev = '' lst = [] for character in input_string: if character != prev: if prev: entry = (prev, count) lst.append(entry) count = 1 prev = character else: count += 1 entry = (character, count) lst.append(entry) return lstdef decode(lst): q = '' for character, count in lst: q += character * count return q
對這段代碼進行單元測試,往往需要寫很多測試用例,現在我們使用 hypothesis 來自動為我們測試,編寫 test_mycode.py (文件名隨意),內容如下:
from hypothesis import givenfrom mycode import decode,encodefrom hypothesis.strategies import textimport unittestclass TestEncoding(unittest.TestCase): @given(text()) def test_decode_inverts_encode(self, s): self.assertEqual(decode(encode(s)), s)if __name__ == '__main__': unittest.main()
可以看出,這里并沒有出現具體的測試用例,而是使用來 text 的策略,相當于 hypothesis 自動窮舉來可能的情況,也可以看出它很容易可其他測試框架集成,這里是 unittest。現在來運行一下看看效果:
(py38env) ➜ tmp python test_mycode.pyFalsifying example: test_decode_inverts_encode( self=<__main__.TestEncoding testMethod=test_decode_inverts_encode>, s=’’,)E======================================================================ERROR: test_decode_inverts_encode (__main__.TestEncoding)----------------------------------------------------------------------Traceback (most recent call last): File 'test_mycode.py', line 9, in test_decode_inverts_encode def test_decode_inverts_encode(self, s): File '/Users/aaron/py38env/lib/python3.8/site-packages/hypothesis/core.py', line 1162, in wrapped_test raise the_error_hypothesis_found File 'test_mycode.py', line 10, in test_decode_inverts_encode self.assertEqual(decode(encode(s)), s) File '/Users/aaron/tmp/mycode.py', line 14, in encode entry = (character, count)UnboundLocalError: local variable ’character’ referenced before assignment----------------------------------------------------------------------Ran 1 test in 0.048sFAILED (errors=1)
這里測試出當字符串為 ’’ 的時候會拋出 UnboundLocalError 的異常。現在我們來修復這個 bug,然后把所有的測試用例 s 給打印出來,看看它用了哪些測試用例。
encode 函數加入以下代碼:
if not input_string: return []
test_mycode.py 文件打印出測試用例:
@given(text())def test_decode_inverts_encode(self, s): print(f'{s=}') self.assertEqual(decode(encode(s)), s)
再次執行:
(py38env) ➜ tmp python test_mycode.pys=’’s=’1’s=’0’s=’0’s=’0’s=’Ā’s=’U000cf5e5’s=’0’s=’’s=’0’s=’0’s=’E’s=')dù’x18U0003deb3¤jd's=’U0005bc37x07U000537a1ÝÀãiÎU000ce9e5x0b’s=’U0005bc37U0005bc37U000537a1ÝÀãiÎU000ce9e5x0b’s=’U0005bc37U000537a1U000537a1ÝÀãiÎU000ce9e5x0b’s=’ÀU000537a1U000537a1ÝÀãiÎU000ce9e5x0b’s=’U000965e1x12x85&U000f500aÄÃc’s=’nU0004466cx86Îx07’s=’ÊU00063f1ex01Gx88’s=’ÚVn’s=’VVn’s=’U0008debf??è’s=’U0008debf??è’s=’U0008debf??’s=’U0008debfU0008debf’s=’U0008debfU0008debfó]½àqx82#U00015196U0001c8beg’s=’U0008debfgó]½àqx82#U00015196U0001c8beg’s=’?’s=’Î’s=’ÎU00085b9e’s='Î8’?U00057c38Ù;x07U000a5ea8Ò»=U00091d5b~8뺈's=’U000d6497Ý>’s=’U000e0f01’s=’U000e0f01Å0y¢KN®’s=’U000e0f01Å0y¢KN®’s=’U00050a06’s=’ÅU000b98b3かU000ba80aá`Ã-Êux8cx90³FÔ'’s=’x8eU0004612ax83ç’s=’x8e’s=’x8ex98U000fb3e0U0010d2b3x10x82x94Ð渥’s=’¥W’s=’pU000e5a2aE·`ì’s=’U000b80f8x12U000c2d54’s=’.U000703de’s=’6U00010ffaU000f7994x8e’s=’116U000f7994x8e’s=’1?6U000f7994x8e’s=’4?6U000f7994x8e’s=’4x8e6U000f7994x8e’s=’0’s=’U0006a564´Ðx93üx9eb&ix1cÑ’s=’U000ceb6f’s=’U000ceb6fxa0x08’s=’U000ceb6fxa0x08’s=’U000ceb6fꄃx08’s=’U000ceb6fꄃ勻U0007cc15U000b2aaa×**’s=’U000ceb6fꄃ勻’s=’勻ꄃ勻’s=’Jx14?ö’s=’q)’s=’q)’s=’qU00060931’s=’q6’s=’U000e3441’s=’U000e3441U00019958¯’s=’x13’s=’U000f34dbk’s=’Kp&tÛà’s=’nöx93’s=’nnx93’s=’U00019c8dѳU00056cbdU000e3b2fU00058d302’s=’x90=Rx8bßx03’s=’x9a’s=’U000147e7’s=’U000147e7x85U0007a3ef’s=’U000147e7U00050a070Â>’s=’U000a4089x0eC+RÁx02x97x9cüÌïSSU0006cbc5;ÿ~x16x019VÇU000a32fdQ÷x15’s=’ÞÚ¾x19©Z®’s=’ਸ਼æ’s=’U000cd45a’s=’U000cd45aU000e15cbÑx08Jueb3eúßx07Ix91x9ax18x16Çx80x1a’s=’x8f}ºx0eqx0b’s=’x0e}ºx0eqx0b’s='U000e05a3&¶º[fõx8bÜR’ͼtx97íWx05U000caea9U0008fd74U000e8f1c¹?dfƾx13's=’x10U000e12e2ùU0006f96erýU00014bafx00x95U000dbc92ÉU00081613µU0003b865ZU0008cc3c’s=’úU000b561fx8fÎ’s=’tàÖ÷’s=’àx92©ÌU000618fax92’s=’U000aaf94x94x84U000cda69U0005291aU000a63deþ¿Ox8a>U000b458bÊ.U00086f07x1a’s=’U0009754e?U_xa0x13PQx18ºx07U0006c9c5.Á’s=’U00102456’s=’³WᵎÕ’s=’x14x1c’s=’x14’s=’x14U00105bcd'x10Ôx99U000a5032RU00056c44V&÷>+U000aaff2ñ®U000d7570%ª!U00032553´8x^«’s=’x00U000e2ac4¼ÄUrB’s=’x00U000e2ac4¼ÄUrB’s=’x00U000e2ac4¼ÄUrB’s=’ªx1aUx8aÇU000b2fb9U0005a586’.----------------------------------------------------------------------Ran 1 test in 0.180sOK
從執行結果可以看出,’’ 首先被測試,其次 hypothesis 使用了大量的極端測試用例,減輕了手寫的負擔,大大提升了效率。
雖然 hypothesis 具有自動記憶功能,你仍然可以顯式的指定某個測試用例一直被測試,而且這是推薦的做法,比如我想在每次的測試中都測試 ’’,可以這樣寫:
from hypothesis import given, examplefrom hypothesis.strategies import text@given(text())@example('')def test_decode_inverts_encode(s): assert decode(encode(s)) == s
這一點非常有用,提升了測試代碼的可讀性,可以用來告訴開發人員或者未來的自己,輸入的字符串必須要考慮 ’’ 的情形。
此外,執行單元測試,不一定要使用 unittest.main(),也可以這樣,是不是很方便:
if __name__ == '__main__': test_decode_inverts_encode()3、其他策略參考從哪里開始
以上僅僅是拋磚引玉,hypothesis 還有很多自動化的特性,不再一一列舉,最好的學習方法是邊做,邊嘗試。hypothesis 是一個開源項目,有著詳細的官方文檔[4],GitHub 倉庫[5]這里都是你開啟自動化測試的好地方:
參考資料[1]
庫: https://realpython.com/python-testing/
[2]
源代碼安裝: https://github.com/HypothesisWorks/hypothesis/blob/master/CONTRIBUTING.rst
[3]
擴展: https://hypothesis.readthedocs.io/en/latest/extras.html
[4]
官方文檔: https://hypothesis.readthedocs.io/en/latest/quickstart.html#running-tests
[5]
GitHub 倉庫: https://github.com/HypothesisWorks/hypothesis/
以上就是python 如何用 Hypothesis 來自動化單元測試的詳細內容,更多關于python 用 Hypothesis 來自動化單元測試的資料請關注好吧啦網其它相關文章!
相關文章: