最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一。初識(shí)單元測(cè)試

1)定義:

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

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

2)單元測(cè)試的意義

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

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

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

測(cè)試一般會(huì)在集成、系統(tǒng)、驗(yàn)收進(jìn)行測(cè)試

4)unittest的注意事項(xiàng):

1.模塊名需要以 test_ 開頭

2.類名:以 Test 開頭

3.測(cè)試用例的方法名稱以 test_ 開頭

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

5)如何寫測(cè)試用例

#首先需要引入單元測(cè)試框架
import unittest
#login模塊校驗(yàn)規(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"}

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

  #登錄賬號(hào)為空
  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)

  #賬號(hào)輸入錯(cuò)誤
  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)

  #密碼輸入錯(cuò)誤
  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)

  #賬號(hào)與密碼都錯(cuò)誤
  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)測(cè)試用例執(zhí)行順序

采取ASCII標(biāo)準(zhǔn)按順序進(jìn)行執(zhí)行

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

1。用例執(zhí)行

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

2.用例組織

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

如下:

3.測(cè)試用例收集

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

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

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

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

[<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)建一個(gè)測(cè)試用例加載器loder,加載測(cè)試用例創(chuàng)建測(cè)試集并對(duì)用例進(jìn)行添加):

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

4.運(yùn)行流程

1)執(zhí)行方法一,沒有測(cè)試報(bào)告(使用的是測(cè)試用例收集方法二進(jìn)行的執(zhí)行):

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

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

2)執(zhí)行方法二,有測(cè)試報(bào)告:

1.自帶的測(cè)試報(bào)告(TextTestRunner)

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

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

2.HTMLTestRunner(測(cè)試報(bào)告模板)

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

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

三。openpyxl

1.安裝與使用范圍

安裝:pip install openpyxl

范圍(2003年后):xlsx

xls格式:需要用xlrd, xlwt

2.使用

導(dǎo)入

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

#獲取表單名

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

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

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

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

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

注意:

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

#使用嵌套循環(huán)的方式,精準(zhǔn)的拿到每一個(gè)坐標(biāo)的對(duì)象,然后轉(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)
#運(yùn)行結(jié)果:

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

#獲取第一行表單對(duì)象
sheet[1]
#可以與切片一起獲取固定行的對(duì)象
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ù)進(jìn)行 分組并返回 tuple對(duì)象,然后使用dict轉(zhuǎn)化為字典
  dict_data=dict(zip(header,cloumn_data))
  row_data.append(dict_data)
print(row_data)

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

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

相關(guān)文章

  • python將pandas datarame保存為txt文件的實(shí)例

    python將pandas datarame保存為txt文件的實(shí)例

    今天小編就為大家分享一篇python將pandas datarame保存為txt文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • jupyter修改文件名方式(TensorFlow)

    jupyter修改文件名方式(TensorFlow)

    這篇文章主要介紹了jupyter修改文件名方式(TensorFlow),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python中使用裝飾器來優(yōu)化尾遞歸的示例

    Python中使用裝飾器來優(yōu)化尾遞歸的示例

    這里我們用典型的斐波那契數(shù)列作為例子,來展示Python中使用裝飾器來優(yōu)化尾遞歸的示例,需要的朋友可以參考下
    2016-06-06
  • linux之父進(jìn)程使用kill函數(shù)殺死子進(jìn)程方式

    linux之父進(jìn)程使用kill函數(shù)殺死子進(jìn)程方式

    這篇文章主要介紹了linux之父進(jìn)程使用kill函數(shù)殺死子進(jìn)程方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python字符串逆序輸出的實(shí)例講解

    Python字符串逆序輸出的實(shí)例講解

    今天小編就為大家分享一篇關(guān)于Python字符串逆序輸出的實(shí)例講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Django異步任務(wù)之Celery的基本使用

    Django異步任務(wù)之Celery的基本使用

    這篇文章主要給大家介紹了關(guān)于Django異步任務(wù)之Celery使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 超詳細(xì)Python文件操作命令知識(shí)

    超詳細(xì)Python文件操作命令知識(shí)

    最近在寫的程序頻繁地與文件操作打交道,所以想著給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于Python文件操作命令的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • Python模擬鍵盤輸入自動(dòng)登錄TGP

    Python模擬鍵盤輸入自動(dòng)登錄TGP

    這篇文章主要介紹了Python模擬鍵盤輸入自動(dòng)登錄TGP的示例代碼,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11
  • python爬取B站關(guān)注列表及數(shù)據(jù)庫的設(shè)計(jì)與操作

    python爬取B站關(guān)注列表及數(shù)據(jù)庫的設(shè)計(jì)與操作

    這篇文章主要為大家介紹了python爬取B站關(guān)注列表及數(shù)據(jù)庫的設(shè)計(jì)與操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • DRF之請(qǐng)求與響應(yīng)的實(shí)現(xiàn)

    DRF之請(qǐng)求與響應(yīng)的實(shí)現(xiàn)

    本文主要介紹了DRF請(qǐng)求與響應(yīng)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07

最新評(píng)論

沁源县| 灌云县| 荆门市| 鹰潭市| 郁南县| 大埔区| 邢台市| 宾阳县| 大埔县| 广汉市| 措美县| 拉孜县| 谷城县| 永丰县| 南陵县| 卓资县| 乌审旗| 宽城| 晋中市| 松江区| 安庆市| 措勤县| 南充市| 卫辉市| 青田县| 北海市| 来安县| 嵩明县| 万源市| 微博| 同心县| 伊春市| 卢龙县| 屏东县| 南召县| 垣曲县| 莒南县| 酒泉市| 三门县| 贵溪市| 全州县|