unittest框架的TestCase提供了如下斷言方法
方法 | 檢查 | 版本 |
assertEqual(a,b) | a==b | |
assertNotEqual(a,b) | a!=b | |
assertTrue(x) | bool(x) is True | |
assertFale(x) | bool(x) is False | |
assertIs(a,b) | a is b | 3.1 |
assertNot(a,b) | a is not b | 3.1 |
assertNone(x) | x is None | 3.1 |
assertNotNoe(x) | x is not None | 3.1 |
assertIn(a,b) | a is in b | 3.1 |
assertNotIn(a,b) | a is not in b | 3.1 |
assertIsInstance(a,b) | isinstance(a,b) | 3.2 |
assertNotIsInstance(a,b) | not isinstance(a,b) | 3.2 |
assertEqual(a,b,msg=None)斷言第一個(gè)參數(shù)和第二個(gè)參數(shù)是否相等,不相等測(cè)試失敗,msg可選參數(shù),用于定義測(cè)試失敗時(shí)打印的信息
文件isPrime
import unittest #判斷是否為質(zhì)數(shù) class IsPrime(): def __init__(self,number): self.number=number def isPrime(self): if self.number<=1: return False for i in range(2,self.number): if self.number % i ==0: return False return True class TestIsPrime(unittest.TestCase): def setup(self): print('test start') def test_case(self): j=IsPrime(5) print(j.isPrime()) self.assertTrue(j.isPrime(),msg='not is prime') def teardown(self): print('test end') if __name__ =='__main__': unittest.main()
當(dāng)前名稱(chēng):Pyhon學(xué)習(xí):斷言方法-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article8/dcsiop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、搜索引擎優(yōu)化、靜態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)站策劃、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容