詳解Python之unittest單元測試代碼
前言
編寫函數(shù)或者類時(shí),還可以為其編寫測試。通過測試,可確定代碼面對各種輸入都能夠按要求的那樣工作。
本次我將介紹如何使用Python模塊unittest中的工具來測試代碼。
測試函數(shù)
首先我們先編寫一個(gè)簡單的函數(shù),它接受姓、名、和中間名三個(gè)參數(shù),并返回完整的姓名:
names.py
def get_fullname(firstname,lastname,middel=''):
'''創(chuàng)建全名'''
if middel:
full_name = firstname + ' ' + middel + ' ' + lastname
return full_name.title()
else:
full_name = firstname + ' ' + lastname
return full_name.title()
然后再當(dāng)前目錄下編寫調(diào)用函數(shù)程序
get_name.py
from names import get_fullname
message = "Please input 'q' to quit."
print(message)
while True:
first = input("Please input your firstname: ")
if first == 'q':
break
last = input("Please input your lastname: ")
if last == 'q':
break
middels = input("Please input your middel name or None: ")
if last == 'q':
break
formant_name = get_fullname(first,last,middels)
print("\tYour are fullname is: " + formant_name.title())
調(diào)用結(jié)果:
Please input 'q' to quit.
進(jìn)程已結(jié)束,退出代碼0
Please input your firstname: xiao
Please input your lastname: peng
Please input your middel or None:
Your are fullname is: Xiao Peng
Please input your firstname: xiao
Please input your lastname: peng
Please input your middel or None: you
Your are fullname is: Xiao You Peng
Please input your firstname: q
創(chuàng)建測試程序
創(chuàng)建測試用例的語法需要一段時(shí)間才能習(xí)慣,但測試用例創(chuàng)建后,再針對函數(shù)的單元測試就很簡單了。先導(dǎo)入模塊unittest以及要測試的函數(shù),再創(chuàng)建一個(gè)繼承函數(shù)unittest.TestCase的類,
并編寫一系列方法對函數(shù)行為的不同方便進(jìn)行測試。
下面介紹測試上面names.py函數(shù)是否能夠正確的獲取姓名:
Test_get_name.py
import unittest
from names import get_fullname
class NamesTestCase(unittest.TestCase):
'''定義測試類'''
def test_get_name2(self):
'''測試2個(gè)字的名字'''
formatied_name2 = get_fullname('xiao','pengyou')
self.assertEqual(formatied_name2,'Xiao Pengyou')
def test_get_name3(self):
'''測試3個(gè)字的名字'''
formatied_name3 = get_fullname('xiao','peng',middel='you')
self.assertEqual(formatied_name3,'Xiao Peng You')
if __name__ == '__init__':
unittest.main()
測試結(jié)果:
Ran 2 tests in 0.034s
OK
兩個(gè)測試單元測試通過測試!
在當(dāng)前的大目錄下會生成一個(gè)測試報(bào)告,可以通過瀏覽器進(jìn)行打開查看。

由圖可知,兩個(gè)測試通過,并顯示測試的時(shí)間?。?!
unittest.TestCase的各種斷言方法
unittest各種斷言方法
| 方 法 | 用 途 |
| assertEqual(a,b) | 核實(shí)a == b |
| assertNotEqual(a,b) | 核實(shí)a != b |
| assertTrue(x) | 核實(shí)x為True |
| assertFalse(x) | 核實(shí)x為False |
| assertIn(item,list) | 核實(shí)item在list中 |
| assertNotIn(item,list) | 核實(shí)item不在list中 |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python matplotlib實(shí)用繪圖技巧匯總
這篇文章主要給大家介紹了關(guān)于Python matplotlib實(shí)用繪圖技巧的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Python中帶時(shí)區(qū)的日期轉(zhuǎn)換工具類總結(jié)
這篇文章主要為大家詳細(xì)介紹了一些Python中帶時(shí)區(qū)的日期轉(zhuǎn)換工具類,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-05-05
django實(shí)現(xiàn)更改數(shù)據(jù)庫某個(gè)字段以及字段段內(nèi)數(shù)據(jù)
這篇文章主要介紹了django實(shí)現(xiàn)更改數(shù)據(jù)庫某個(gè)字段以及字段段內(nèi)數(shù)據(jù),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python利用全連接神經(jīng)網(wǎng)絡(luò)求解MNIST問題詳解
這篇文章主要介紹了Python利用全連接神經(jīng)網(wǎng)絡(luò)求解MNIST問題,結(jié)合實(shí)例形式詳細(xì)分析了單隱藏層神經(jīng)網(wǎng)絡(luò)與多層神經(jīng)網(wǎng)絡(luò),以及Python全連接神經(jīng)網(wǎng)絡(luò)求解MNIST問題相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
Python 按字典dict的鍵排序,并取出相應(yīng)的鍵值放于list中的實(shí)例
今天小編就為大家分享一篇Python 按字典dict的鍵排序,并取出相應(yīng)的鍵值放于list中的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
詳談pandas中agg函數(shù)和apply函數(shù)的區(qū)別
下面小編就為大家分享一篇詳談pandas中agg函數(shù)和apply函數(shù)的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04

