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

Python unittest單元測試openpyxl實現(xiàn)過程解析

 更新時間:2020年05月27日 08:46:17   作者:1142783691  
這篇文章主要介紹了Python unittest單元測試openpyxl實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一。初識單元測試

1)定義:

單元:函數(shù)或者是類
單元測試:測試類或者函數(shù)

python內(nèi)置的單元測試框架:unittest

2)單元測試的意義

好處:投入小,收益大。能夠精準的,更早的發(fā)現(xiàn)問題。

3)單元測試與測試關(guān)系

python 很難測試 java 的單元。
關(guān)鍵是單元測試一般是開發(fā)或者測試開發(fā)做的。

測試一般會在集成、系統(tǒng)、驗收進行測試

4)unittest的注意事項:

1.模塊名需要以 test_ 開頭

2.類名:以 Test 開頭

3.測試用例的方法名稱以 test_ 開頭

4.單元測試寫入方式(其中TestLogin是測試模塊):TestLogin(unittest.TestCase)

5)如何寫測試用例

#首先需要引入單元測試框架
import unittest
#login模塊校驗規(guī)則
def login(username=None, password=None):
  """登錄"""
  if (not username) or (not password):
    # 用戶名或者密碼為空
    return {"msg": "empty"}
  if username == 'yuz' and password == '123456':
    # 正確的用戶名和密碼
    return {"msg": "success"}
  return {"msg": "error"}

#單元測試用例
class TestLogin(unittest.TestCase):
  def setUp(self):
    pass
  def tearDown(self):
    pass
  #登錄賬號與密碼為空
  def test_login_01_null(self):
    username=''
    password=''
    expected_result={"msg": "empty"}
    actual_result=login(username,password)
    self.assertTrue(expected_result == actual_result)

  #登錄賬號為空
  def test_login_02_usernull(self):
    username=''
    password='123456'
    expected_result={"msg": "empty"}
    actual_result=login(username,password)
    self.assertTrue(expected_result == actual_result)

  #登錄密碼為空
  def test_login_03_passwordnull(self):
    username='yuz'
    password=''
    expected_result={"msg": "empty"}
    actual_result=login(username,password)
    self.assertTrue(expected_result == actual_result)
  #正常登錄
  def test_login_04_correct(self):
    username = 'yuz'
    password = '123456'
    expected_result = {"msg": "success"}
    actual_result = login(username, password)
    self.assertEqual(expected_result,actual_result)

  #賬號輸入錯誤
  def test_login_05_usererro(self):
    username = 'linzai'
    password = '123456'
    expected_result = {"msg": "error"}
    actual_result = login(username, password)
    self.assertTrue(expected_result == actual_result)

  #密碼輸入錯誤
  def test_login_06_usererro(self):
    username = 'yuz'
    password = '12345698'
    expected_result = {"msg": "error"}
    actual_result = login(username, password)
    self.assertTrue(expected_result == actual_result)

  #賬號與密碼都錯誤
  def test_login_07_userpassworderror(self):
    username='linzai'
    password='laksls'
    expected_result={"msg": "error"}
    actual_result=login(username,password)
    self.assertTrue(expected_result == actual_result)

#執(zhí)行方法
if __name__ == '__main__':
  unittest.main()

6)測試用例執(zhí)行順序

采取ASCII標準按順序進行執(zhí)行

二。單元深入了解。(用例執(zhí)行、組織、收集、運行流程)

1。用例執(zhí)行

  • 1)右擊 unittest 運行(在.py文件中)
  • 2)python 運行 unittest.main()
  • 3) 運行所有的測試用例(控制臺直接執(zhí)行 : python test...py)

2.用例組織

會把測試用例的代碼放到一個統(tǒng)一的文件夾當中或者目錄當中。

如下:

3.測試用例收集

需要把每個測試用例模塊當中的測試用例收集到一起,一起執(zhí)行。

1)方法一:(創(chuàng)建一個測試用例加載器,使用discover 收集所有用例)

#初始化一個測試用例加載器
loder=unittest.TestLoader()
#先拿到該.py文件的絕對路徑
file_path=os.path.abspath(__file__)
#拿到測試模塊的路徑
case_path=os.path.join(os.path.dirname(file_path),'test')
#使用loder收集所有的測試用例
test_suit=loder.discover(case_path)
print(test_suit)

運行結(jié)果(discover收集的內(nèi)容是一個列表,如下圖):

[<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<test_login.TestLogin testMethod=test_login_01_null>, <test_login.TestLogin testMethod=test_login_02_usernull>, <test_login.TestLogin testMethod=test_login_03_passwordnull>, <test_login.TestLogin testMethod=test_login_04_correct>, <test_login.TestLogin testMethod=test_login_05_usererro>, <test_login.TestLogin testMethod=test_login_06_usererro>, <test_login.TestLogin testMethod=test_login_07_userpassworderror>]>]>, <unittest.suite.TestSuite tests=[]>, <unittest.suite.TestSuite tests=[]>]>

2)方法二(創(chuàng)建一個測試用例加載器loder,加載測試用例創(chuàng)建測試集并對用例進行添加):

from class_16_unittest單元測試集及報告.test import test_login,test_register
loder=unittest.TestLoader()
case_path=os.path.join(os.path.dirname(os.path.abspath(__file__)),'test')
#加載測試用例
suite_login=loder.loadTestsFromModule(test_login)
suite_register=loder.loadTestsFromModule(test_register)
#初始化一個測試集合 suite
suite_total=unittest.TestSuite()
#添加測試用例
suite_total.addTest(suite_login)
suite_total.addTest(test_register)

4.運行流程

1)執(zhí)行方法一,沒有測試報告(使用的是測試用例收集方法二進行的執(zhí)行):

runner = unittest.TextTestRunner()
runner.run(suite_total)

運行結(jié)果:

2)執(zhí)行方法二,有測試報告:

1.自帶的測試報告(TextTestRunner)

with open("test_result.txt",'w',encoding='utf-8') as f:
  runner = unittest.TextTestRunner(f)
  runner.run(suite_total)

運行結(jié)果:

2.HTMLTestRunner(測試報告模板)

with open("test_result.html",'wb') as f:
  runner = HTMLTestRunner(f,
              title='測試title',
              description="測試報告的描述",
              tester='測試人'
              )
  runner.run(suite_total)

運行結(jié)果:[/code]

三。openpyxl

1.安裝與使用范圍

安裝:pip install openpyxl

范圍(2003年后):xlsx

xls格式:需要用xlrd, xlwt

2.使用

導入

import openpyxl
from openpyxl.worksheet.worksheet import Worksheet
#打開文件
workbook=openpyxl.load_workbook('cases.xlsx')
# print(workbook)

#獲取表單名

1)#sheet : Worksheet 可以獲取到 對應(yīng)得表單名字
sheet : Worksheet=workbook['Sheet1']
# print(sheet)
2)#方法二,通過表單名或者排列順序獲得操作表單
sheet=workbook['Sheet1']
#根據(jù)位置獲取操作表單名稱
sheet=workbook.worksheets[0]

#獲取數(shù)據(jù)
1)#根據(jù)表單行列獲取表單對象,row:行 column:列

cell=sheet.cell(row=2,column=3)

print(cell)
#獲取表單數(shù)據(jù)
print(cell.value)
運行結(jié)果:
excel表信息:

2)#根據(jù)最大行列,進行獲取數(shù)據(jù),使用嵌套循環(huán)的方法把表單數(shù)據(jù)一行一行的化為列表返回

注意:

#獲取表單的最大行數(shù)
row_max=sheet.max_row
#獲取最大列數(shù)
cloumn_max=sheet.max_column

#使用嵌套循環(huán)的方式,精準的拿到每一個坐標的對象,然后轉(zhuǎn)化為值
row_data=[]
for row in range(1,row_max+1):
  cloumn_data=[]
  for cloumn in range(1,cloumn_max+1):
    #print(sheet.cell(row,cloumn))
    cloumn_data.append(sheet.cell(row,cloumn).value)
  row_data.append(cloumn_data)
print(row_data)
#運行結(jié)果:

3)#根據(jù)最大行列,進行獲取數(shù)據(jù),使用嵌套循環(huán)的方法把表單數(shù)據(jù)一行一行的化為dict返回

#獲取第一行表單對象
sheet[1]
#可以與切片一起獲取固定行的對象
sheet[1:]


row_data=[]
#獲取第一行的所有值
header=[c.value for c in sheet[1]]
for row in range(2,row_max+1):
  cloumn_data=[]
  for cloumn in range(1,cloumn_max+1):
    #print(sheet.cell(row,cloumn))
    cloumn_data.append(sheet.cell(row,cloumn).value)
    #print(cloumn_data)
  #使用zip函數(shù)把header與一行一行數(shù)據(jù)進行 分組并返回 tuple對象,然后使用dict轉(zhuǎn)化為字典
  dict_data=dict(zip(header,cloumn_data))
  row_data.append(dict_data)
print(row_data)

運行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python面向?qū)ο骳lass類屬性及子類用法分析

    Python面向?qū)ο骳lass類屬性及子類用法分析

    這篇文章主要介紹了Python面向?qū)ο骳lass類屬性及子類用法,結(jié)合實例形式較為詳細的分析了Python面向?qū)ο缶幊讨衏lass類屬性的訪問、修改、刪除操作及子類的相關(guān)使用技巧,需要的朋友可以參考下
    2018-02-02
  • 老生常談進程線程協(xié)程那些事兒

    老生常談進程線程協(xié)程那些事兒

    下面小編就為大家?guī)硪黄仙U勥M程線程協(xié)程那些事兒。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決

    python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決

    這篇文章主要為大家介紹了python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Python編寫nmap掃描工具

    Python編寫nmap掃描工具

    NMAP是一款開源的網(wǎng)絡(luò)探測和安全審核的工具,今天我們用python的模擬實現(xiàn)一個簡單版本的端口掃描工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • python簡單實現(xiàn)基于SSL的IRC bot實例

    python簡單實現(xiàn)基于SSL的IRC bot實例

    這篇文章主要介紹了python簡單實現(xiàn)基于SSL的IRC bot,實例分析了IRC機器人的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2015-06-06
  • Python用Try語句捕獲異常的實例方法

    Python用Try語句捕獲異常的實例方法

    在本篇文章中小編給大家整理了關(guān)于Python用Try語句如何捕獲異常的相關(guān)知識點內(nèi)容,需要的朋友們參考下。
    2019-06-06
  • python爬蟲_微信公眾號推送信息爬取的實例

    python爬蟲_微信公眾號推送信息爬取的實例

    下面小編就為大家?guī)硪黄猵ython爬蟲_微信公眾號推送信息爬取的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • python檢測遠程udp端口是否打開的方法

    python檢測遠程udp端口是否打開的方法

    這篇文章主要介紹了python檢測遠程udp端口是否打開的方法,涉及Python操作socket實現(xiàn)檢測udp端口的技巧,需要的朋友可以參考下
    2015-03-03
  • 淺談Pandas 排序之后索引的問題

    淺談Pandas 排序之后索引的問題

    今天小編就為大家分享一篇淺談Pandas 排序之后索引的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • openCV入門學習基礎(chǔ)教程第一篇

    openCV入門學習基礎(chǔ)教程第一篇

    OpenCV是計算機視覺領(lǐng)域一個圖像和視頻處理庫,用于各種圖像和視頻分析,如面部識別和檢測,車牌閱讀,照片編輯,高級機器人視覺,光學字符識別等等,下面這篇文章主要給大家介紹了關(guān)于openCV入門學習基礎(chǔ)教程第一篇的相關(guān)資料,需要的朋友可以參考下
    2022-11-11

最新評論