成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

Python基于Hypothesis測試庫生成測試數(shù)據(jù)

瀏覽:35日期:2022-07-27 09:25:55

Hypothesis是Python的一個(gè)高級測試庫。它允許編寫測試用例時(shí)參數(shù)化,然后生成使測試失敗的簡單易懂的測試數(shù)據(jù)。可以用更少的工作在代碼中發(fā)現(xiàn)更多的bug。

安裝

pip install hypothesis

如何設(shè)計(jì)測試數(shù)據(jù)

通過介紹也許你還不了解它是干嘛的,沒關(guān)系!我們舉個(gè)例子。

首先,我有一個(gè)需要測試的函數(shù):

def add(a, b):'''實(shí)現(xiàn)加法運(yùn)算'''return a + b

測試代碼是這樣的:

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ì)足夠多的 測試數(shù)據(jù), 同樣也需要很多條用例!

當(dāng)然,為了測試足夠多的數(shù)據(jù),我們也可以將代碼改稱這樣。

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()

通過調(diào)用 randint() 函數(shù)生成隨機(jī)數(shù)。循環(huán)10次(也可以是100次,1000次),用更少的代碼做更多的測試,測試的數(shù)據(jù)越多,發(fā)現(xiàn)bug的可能性越大。

測試結(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生成測試數(shù)據(jù)

上面的測試數(shù)據(jù)很難隨機(jī)到 邊界值,除非我手動(dòng)設(shè)計(jì)數(shù)據(jù),而且用for循環(huán)也不是太好的設(shè)計(jì)。是時(shí)候讓hypothesis登場了。

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()

通過@given() 裝飾測試用例,調(diào)用strategies 模塊下面的 integers() 方法生成隨機(jī)的測試數(shù)。在@setting()裝飾器中通過max_examples用來控制隨機(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ì)更具有 測試價(jià)值,對吧? hypothesis 還可以生成更多類型的測試數(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ù)看上去就具有很高的測試價(jià)值。好吧!測試一定明白我在說什么。

問題來了,我們可以將 hypothesis 生成的數(shù)據(jù)應(yīng)用到 Web或接口自動(dòng)化測試中么?

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章: