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多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決
這篇文章主要為大家介紹了python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09python簡單實現(xiàn)基于SSL的IRC bot實例
這篇文章主要介紹了python簡單實現(xiàn)基于SSL的IRC bot,實例分析了IRC機器人的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2015-06-06