python3.6編寫的單元測試示例
本文實(shí)例講述了python3.6編寫的單元測試。分享給大家供大家參考,具體如下:
使用python3.6編寫一個(gè)單元測試demo,例如:對(duì)學(xué)生Student類編寫一個(gè)簡單的單元測試。
1、編寫Student類:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- class Student(object): def __init__(self,name,score): self.name = name self.score = score def get_grade(self): if self.score >= 80 and self.score <= 100: return 'A' elif self.score >= 60 and self.score <= 79: return 'B' elif self.score >= 0 and self.score <= 59: return 'C' else: raise ValueError('value is not between 0 and 100')
2、編寫一個(gè)測試類TestStudent,從unittest.TestCase繼承:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import unittest from student import Student class TestStudent(unittest.TestCase): def test_80_to_100(self): s1 = Student('Bart',80) s2 = Student('Lisa',100) self.assertEqual(s1.get_grade(),'A') self.assertEqual(s2.get_grade(),'A') def test_60_to_80(self): s1 = Student('Bart',60) s2 = Student('Lisa',79) self.assertEqual(s1.get_grade(),'B') self.assertEqual(s2.get_grade(),'B') def test_0_to_60(self): s1 = Student('Bart',0) s2 = Student('Lisa',59) self.assertEqual(s1.get_grade(),'C') self.assertEqual(s2.get_grade(),'C') def test_invalid(self): s1 = Student('Bart',-1) s2 = Student('Lisa',101) with self.assertRaises(ValueError): s1.get_grade() with self.assertRaises(ValueError): s2.get_grade() #運(yùn)行單元測試 if __name__ == '__main__': unittest.main()
3、運(yùn)行結(jié)果如下:
4、行單元測試另一種方法:在命令行通過參數(shù)-m unittest直接運(yùn)行單元測試,例如:python -m unittest student_test
最后對(duì)使用unittest模塊的一些總結(jié):
- 編寫單元測試時(shí),需要編寫一個(gè)測試類,從
unittest.TestCase
繼承 - 對(duì)每一個(gè)類測試都需要編寫一個(gè)
test_xxx()
方法 - 最常用的斷言就是
assertEqual()
- 另一種重要的斷言就是期待拋出指定類型的Error,eg:
with self.assertRaises(KeyError):
- 另一種方法是在命令行通過參數(shù)-m unittest直接運(yùn)行單元測試:eg:
python -m unittest student_test
- 最簡單的運(yùn)行方式是xx.py的最后加上兩行代碼:
if __name__ == '__main__': unittest.main()
更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python入門與進(jìn)階經(jīng)典教程》、《Python字符串操作技巧匯總》、《Python列表(list)操作技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python基于Hypothesis測試庫生成測試數(shù)據(jù)
- Python Unittest自動(dòng)化單元測試框架詳解
- 在Python中進(jìn)行自動(dòng)化單元測試的教程
- 全面介紹python中很常用的單元測試框架unitest
- python單元測試框架pytest的使用示例
- Python編寫單元測試代碼實(shí)例
- Python unittest單元測試openpyxl實(shí)現(xiàn)過程解析
- Python unittest單元測試框架實(shí)現(xiàn)參數(shù)化
- Python unittest單元測試框架及斷言方法
- Python Django框架單元測試之文件上傳測試示例
- python 如何用 Hypothesis 來自動(dòng)化單元測試
相關(guān)文章
Python如何根據(jù)關(guān)鍵字逐行提取文本內(nèi)容問題
這篇文章主要介紹了Python如何根據(jù)關(guān)鍵字逐行提取文本內(nèi)容問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08手把手教你將Flask應(yīng)用封裝成Docker服務(wù)的實(shí)現(xiàn)
這篇文章主要介紹了手把手教你將Flask應(yīng)用封裝成Docker服務(wù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08python實(shí)現(xiàn)電子產(chǎn)品商店
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)電子產(chǎn)品商店,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02Python中使用socket發(fā)送HTTP請(qǐng)求數(shù)據(jù)接收不完整問題解決方法
這篇文章主要介紹了Python中使用socket發(fā)送HTTP請(qǐng)求數(shù)據(jù)接收不完整問題解決方法,本文使用一個(gè)循環(huán)解決了數(shù)據(jù)不完整問題,需要的朋友可以參考下2015-02-02python利用wx實(shí)現(xiàn)界面按鈕和按鈕監(jiān)聽和字體改變的方法
今天小編就為大家分享一篇python利用wx實(shí)現(xiàn)界面按鈕和按鈕監(jiān)聽和字體改變的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07django xadmin action兼容自定義model權(quán)限教程
這篇文章主要介紹了django xadmin action兼容自定義model權(quán)限教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03