Python基于Hypothesis測(cè)試庫(kù)生成測(cè)試數(shù)據(jù)
Hypothesis是Python的一個(gè)高級(jí)測(cè)試庫(kù)。它允許編寫(xiě)測(cè)試用例時(shí)參數(shù)化,然后生成使測(cè)試失敗的簡(jiǎn)單易懂的測(cè)試數(shù)據(jù)。可以用更少的工作在代碼中發(fā)現(xiàn)更多的bug。
安裝
pip install hypothesis
如何設(shè)計(jì)測(cè)試數(shù)據(jù)
通過(guò)介紹也許你還不了解它是干嘛的,沒(méi)關(guān)系!我們舉個(gè)例子。
首先,我有一個(gè)需要測(cè)試的函數(shù):
def add(a, b):'''實(shí)現(xiàn)加法運(yùn)算'''return a + b
測(cè)試代碼是這樣的:
import unittestclass AddTest(unittest.TestCase): def test_case1(self): c = add(1, 2) self.assertEqual(c, 3) def test_case2(self): c = add(0, 2) self.assertEqual(c, 2) def test_case3(self): c = add(-2, 2) self.assertEqual(c, 0)if __name__ == ’__main__’: unittest.main()
為了更全面的驗(yàn)證的 add() 函數(shù),我必須設(shè)計(jì)足夠多的 測(cè)試數(shù)據(jù), 同樣也需要很多條用例!
當(dāng)然,為了測(cè)試足夠多的數(shù)據(jù),我們也可以將代碼改稱(chēng)這樣。
import unittestfrom random import randintclass AddTest(unittest.TestCase): def test_case(self): for i in range(10): a = randint(-32768, 32767) b = randint(-32768, 32767) print('a->', a) print('b->', b) c1 = a + b c2 = add(a, b) self.assertEqual(c1, c2)if __name__ == ’__main__’: unittest.main()
通過(guò)調(diào)用 randint() 函數(shù)生成隨機(jī)數(shù)。循環(huán)10次(也可以是100次,1000次),用更少的代碼做更多的測(cè)試,測(cè)試的數(shù)據(jù)越多,發(fā)現(xiàn)bug的可能性越大。
測(cè)試結(jié)果如下:
> python test_hypothesis_demo.py
a-> 11503b-> -784a-> -31548b-> 13057a-> 22033b-> 3618a-> -32249b-> 28025a-> -15429b-> 31055a-> 16095b-> 13445a-> -31536b-> 14606a-> 18655b-> -18039a-> 17923b-> -12079a-> -9256b-> -26440.------------------------Ran 1 test in 0.002s
OK
用 hypothesis生成測(cè)試數(shù)據(jù)
上面的測(cè)試數(shù)據(jù)很難隨機(jī)到 邊界值,除非我手動(dòng)設(shè)計(jì)數(shù)據(jù),而且用for循環(huán)也不是太好的設(shè)計(jì)。是時(shí)候讓hypothesis登場(chǎng)了。
import unittestfrom hypothesis import given, settingsimport hypothesis.strategies as stclass AddTest(unittest.TestCase): @settings(max_examples=10) @given(a=st.integers(), b=st.integers()) def test_case(self, a, b): print('a->', a) print('b->', b) c1 = a + b c2 = add(a, b) self.assertEqual(c1, c2)if __name__ == ’__main__’: unittest.main()
通過(guò)@given() 裝飾測(cè)試用例,調(diào)用strategies 模塊下面的 integers() 方法生成隨機(jī)的測(cè)試數(shù)。在@setting()裝飾器中通過(guò)max_examples用來(lái)控制隨機(jī)數(shù)的個(gè)數(shù)。
運(yùn)行結(jié)果如下:
> python test_hypothesis_demo.py
a-> 0 b-> 0 a-> 5980 b-> -3607224505277606703a-> 324106882b-> 23975a-> 23272b-> 4917 a-> 107b-> -155 a-> -4500b-> -8303a-> 2683 b-> 4384 a-> 27b-> -81a-> -122472823694675410551869872440384533757 b-> -89a-> 19075b-> 4362 .-------------------------------------------------Ran 1 test in 0.032s
hypothesis 生成的數(shù)據(jù)會(huì)更具有 測(cè)試價(jià)值,對(duì)吧? hypothesis 還可以生成更多類(lèi)型的測(cè)試數(shù)據(jù)。例如 email格式和text格式。
email-> 0@A.comtext->email-> ^H@R70-s0Xke.Sb-UBn08.VzT--dz000I0o00r00s--EJY.e.Ov.aRaMcO text-> -email-> 6a#@T.HKttext-> ↕email-> ’/YAw/jnIZ!0fS+A@E7UJ.expErttext-> +�email-> *xh*-#t5$0-L8O&r10XnXU-**+e%0xy-@k.O.e.lEasetext-> #�����/���+�)�▲�email-> 2U!N0+|*%~@T.q-NX-0-0gWl.x.Lvtext->email-> &i/o!F*@xuW--03.p00-t0Y-0Z0.MW.K-000-n-sB0rR-0L.Y.y2u.NXptL0bgG-0U.XN--FLw351Etext-> �0▲-���email-> oK*-@p.ZiPtext-> ☺email-> /@mOL.Y-Q.j.p.d-3Mzi.i.Utv-M.yachtstext-> (email-> 4ql$y2%N4h@c.veRSIcheruNGtext->
這些數(shù)據(jù)看上去就具有很高的測(cè)試價(jià)值。好吧!測(cè)試一定明白我在說(shuō)什么。
問(wèn)題來(lái)了,我們可以將 hypothesis 生成的數(shù)據(jù)應(yīng)用到 Web或接口自動(dòng)化測(cè)試中么?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. docker compose idea CreateProcess error=2 系統(tǒng)找不到指定的文件的問(wèn)題2. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法3. 一文秒懂idea的git插件跟翻譯插件4. Python通過(guò)format函數(shù)格式化顯示值5. JS算法題解旋轉(zhuǎn)數(shù)組方法示例6. PHP設(shè)計(jì)模式之迭代器模式Iterator實(shí)例分析【對(duì)象行為型】7. python爬蟲(chóng)利用代理池更換IP的方法步驟8. Vue+express+Socket實(shí)現(xiàn)聊天功能9. JS中的常見(jiàn)數(shù)組遍歷案例詳解(forEach, map, filter, sort, reduce, every)10. python中pandas.read_csv()函數(shù)的深入講解
