Python unittest單元測(cè)試openpyxl實(shí)現(xiàn)過程解析
一。初識(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é)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決
這篇文章主要為大家介紹了python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
python簡(jiǎn)單實(shí)現(xiàn)基于SSL的IRC bot實(shí)例
這篇文章主要介紹了python簡(jiǎn)單實(shí)現(xiàn)基于SSL的IRC bot,實(shí)例分析了IRC機(jī)器人的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-06-06
python爬蟲_微信公眾號(hào)推送信息爬取的實(shí)例
下面小編就為大家?guī)硪黄猵ython爬蟲_微信公眾號(hào)推送信息爬取的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
python檢測(cè)遠(yuǎn)程udp端口是否打開的方法
這篇文章主要介紹了python檢測(cè)遠(yuǎn)程udp端口是否打開的方法,涉及Python操作socket實(shí)現(xiàn)檢測(cè)udp端口的技巧,需要的朋友可以參考下2015-03-03
openCV入門學(xué)習(xí)基礎(chǔ)教程第一篇
OpenCV是計(jì)算機(jī)視覺領(lǐng)域一個(gè)圖像和視頻處理庫,用于各種圖像和視頻分析,如面部識(shí)別和檢測(cè),車牌閱讀,照片編輯,高級(jí)機(jī)器人視覺,光學(xué)字符識(shí)別等等,下面這篇文章主要給大家介紹了關(guān)于openCV入門學(xué)習(xí)基礎(chǔ)教程第一篇的相關(guān)資料,需要的朋友可以參考下2022-11-11

