欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python unittest模塊用法實(shí)例分析

 更新時(shí)間:2018年05月25日 09:25:07   作者:canlynet  
這篇文章主要介紹了Python unittest模塊用法,結(jié)合實(shí)例形式分析了unittest模塊功能及相關(guān)函數(shù)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Python unittest模塊用法。分享給大家供大家參考,具體如下:

python的unittest模塊提供了一個(gè)測(cè)試框架,只要我們寫一個(gè)繼承unittest.TestCase的類,類中用setUp做初始化,用tearDown做清理。

主要用到的函數(shù)有:

failedinfo表示不成立打印信息failedinfo,為可選參數(shù)
self.fail([msg])會(huì)無(wú)條件的導(dǎo)致測(cè)試失敗,不推薦使用。
self.assertEqual(value1, value2, failedinfo) # 斷言value1 == value2
self.assertTrue(表達(dá)式, failedinfo) # 斷言value為真
self.assertFalse(表達(dá)式, failedinfo) # 斷言value為假
# 斷言肯定發(fā)生異常,如果沒(méi)發(fā)生異常,則為測(cè)試失敗。
# 參數(shù)1為異常,參數(shù)二為拋出異常的調(diào)用對(duì)象,剩余參數(shù)為傳遞給可調(diào)用對(duì)象的參數(shù)。
self.assertRaises(ValueError, self.widget.resize, -1, -1)
調(diào)用時(shí)機(jī)的加self,如self.assertEqual(self.seq, range(10)),self.assertTrue(value > 100)

更詳細(xì)的教程見(jiàn):http://pyunit.sourceforge.net/pyunit_cn.html

Python代碼:

#coding=utf-8
import random
import unittest
class TestSequenceFunctions(unittest.TestCase):
  def setUp(self):
    self.seq = range(10)
  def test_shuffle(self):
    # make sure the shuffled sequence does not lose any elements
    random.shuffle(self.seq)
    self.seq.sort()
    self.assertEqual(self.seq, range(10))
    # should raise an exception for an immutable sequence
    self.assertRaises(TypeError, random.shuffle, (1,2,3))
  def test_choice(self):
    element = random.choice(self.seq)
    self.assertTrue(element in self.seq)
  def test_sample(self):
    with self.assertRaises(ValueError):
      random.sample(self.seq, 20)
    for element in random.sample(self.seq, 5):
      self.assertTrue(element in self.seq)
results_fields = [
  ("username", unicode),
  ("showid", unicode),
  ("total_pv", int),
  ("pubdate", unicode),
  ("tags", list),
  ("showname", unicode),
  ("pg", int),
  ("ext", str),
]
results_fields_map = dict(results_fields)
class TestDictValueFormatFunctions(unittest.TestCase):
  def setUp(self):
    self.results = [{
      "username": u"瘋狂豆花",
      "showid": u"130e28f0fe0811e0a046",
      "total_pv": 14503214,
      "pubdate": u"2012-07-07 01:22:47",
      "tags": [
        "軒轅劍",
        "天之痕"
        ],
      "showname" : u"軒轅劍之天之痕",
      "pg" : 1,
      "ext" : "mp4"
    }
    ]
  def test_format(self):
    self.assertTrue(isinstance(self.results, list), "self.results's type must be dict but got {0}".format(type(self.results)))
    for r in self.results:
      for f in results_fields_map:
        value = r.get(f, None)
        self.assertTrue(isinstance(value, results_fields_map[f]), u"{0}'s type must be {1} but got {2}".format(value, results_fields_map[f], type(value)))
        #self.assertTrue(isinstance(value, results_fields_map[f]))
  def test_value(self):
    for r in self.results:
      self.assertEqual(r["pg"], 1)
      self.assertEqual(r["ext"], u"mp4")
if __name__ == '__main__':
  # unittest.main() # 用這個(gè)是最簡(jiǎn)單的,下面的用法可以同時(shí)測(cè)試多個(gè)類
  # unittest.TextTestRunner(verbosity=2).run(suite1) # 這個(gè)等價(jià)于上述但可設(shè)置verbosity=2,省去了運(yùn)行時(shí)加-v
  suite1 = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
  suite2 = unittest.TestLoader().loadTestsFromTestCase(TestDictValueFormatFunctions)
  suite = unittest.TestSuite([suite1, suite2])
  unittest.TextTestRunner(verbosity=2).run(suite)

運(yùn)行結(jié)果:

test_choice (__main__.TestSequenceFunctions) ... ok
test_sample (__main__.TestSequenceFunctions) ... ok
test_shuffle (__main__.TestSequenceFunctions) ... ok
test_format (__main__.TestDictValueFormatFunctions) ... ok
test_value (__main__.TestDictValueFormatFunctions) ... ok

----------------------------------------------------------------------
Ran 5 tests in 0.013s

OK

更多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ì)有所幫助。

相關(guān)文章

  • Python基于React-Dropzone實(shí)現(xiàn)上傳組件的示例代碼

    Python基于React-Dropzone實(shí)現(xiàn)上傳組件的示例代碼

    本文主要介紹了在React-Flask框架上開(kāi)發(fā)上傳組件的技巧。文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 在python中使用正則表達(dá)式查找可嵌套字符串組

    在python中使用正則表達(dá)式查找可嵌套字符串組

    這篇文章主要介紹了在python中使用正則表達(dá)式查找可嵌套字符串組的相關(guān)資料,需要的朋友可以參考下
    2017-10-10
  • Python代碼調(diào)試技巧教程詳解

    Python代碼調(diào)試技巧教程詳解

    這篇文章主要為大家介紹了Python代碼的一些方便快捷的調(diào)試技巧,文中含有詳細(xì)新的步驟操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • python爬蟲爬取指定內(nèi)容的解決方法

    python爬蟲爬取指定內(nèi)容的解決方法

    這篇文章主要介紹了python爬蟲爬取指定內(nèi)容,爬取一些網(wǎng)站下指定的內(nèi)容,一般來(lái)說(shuō)可以用xpath來(lái)直接從網(wǎng)頁(yè)上來(lái)獲取,但是當(dāng)我們獲取的內(nèi)容不唯一的時(shí)候我們無(wú)法選擇,我們所需要的、所指定的內(nèi)容,需要的朋友可以參考下
    2022-06-06
  • pytorch中unsqueeze用法小結(jié)

    pytorch中unsqueeze用法小結(jié)

    unsqueeze()的作用是用來(lái)增加給定tensor的維度的,本文主要介紹了pytorch中unsqueeze用法小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • TensorFlow人工智能學(xué)習(xí)創(chuàng)建數(shù)據(jù)實(shí)現(xiàn)示例詳解

    TensorFlow人工智能學(xué)習(xí)創(chuàng)建數(shù)據(jù)實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了TensorFlow人工智能學(xué)習(xí)創(chuàng)建數(shù)據(jù)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • python正則表達(dá)式完成車牌號(hào)檢驗(yàn)的代碼實(shí)例

    python正則表達(dá)式完成車牌號(hào)檢驗(yàn)的代碼實(shí)例

    這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式完成車牌號(hào)檢驗(yàn)的相關(guān)資料,在Python中正則表達(dá)式是一種用于匹配和操作字符串的強(qiáng)大工具,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • python 進(jìn)階學(xué)習(xí)之python裝飾器小結(jié)

    python 進(jìn)階學(xué)習(xí)之python裝飾器小結(jié)

    這篇文章主要介紹了python 進(jìn)階學(xué)習(xí)之python裝飾器小結(jié),本文通過(guò)場(chǎng)景分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Python UnicodeEncodeError: ''gbk'' codec can''t encode character 解決方法

    Python UnicodeEncodeError: ''gbk'' codec can''t encode chara

    這篇文章主要介紹了Python UnicodeEncodeError: 'gbk' codec can't encode character 解決方法,需要的朋友可以參考下
    2015-04-04
  • 使用pytorch進(jìn)行圖像分類的詳細(xì)步驟

    使用pytorch進(jìn)行圖像分類的詳細(xì)步驟

    使用PyTorch進(jìn)行圖像分類是深度學(xué)習(xí)中的一個(gè)常見(jiàn)任務(wù),涉及一系列步驟,從數(shù)據(jù)預(yù)處理到模型訓(xùn)練和評(píng)估,下面將詳細(xì)描述每個(gè)步驟,從零開(kāi)始構(gòu)建一個(gè)圖像分類器,需要的朋友可以參考下
    2024-09-09

最新評(píng)論