python之用unittest實現接口參數化示例
阿新 • • 發佈:2019-01-31
pri pass pyc user 實現接口 value workbook lin spa
示例中獲取參數的方法有三種:
1. 從文件(txt)中讀取參數
2. 從Excel中讀取參數
3. 在代碼中直接寫參數
def login(username,password): return ‘ok‘ import unittest from parameterized import parameterized import BeautifulReport as bf import xlrd,xlwt def file_to_list(file_name): #從文件中讀取參數 l = [] with open(file_name,encoding=‘utf-8‘) as fr: for line in fr: line_list = line.strip().split(‘,‘) l.append(line_list) return l def excel_to_list(file_name): #從Excel中讀取參數 l=[] book=xlrd.open_workbook(file_name) sheet=book.sheet_by_index(0) for row in range(1,sheet.nrows):#從第一行開始取值,取到最後一行l.append(sheet.row_values(row))#將每行的數據存入大列表中,每行數據都是一個list return l class TestLogin(unittest.TestCase): #在代碼中寫入參數 @parameterized.expand([ [‘admin‘,‘1244‘,‘ok‘], [‘admin‘,‘1244‘,‘ok‘], [‘admin‘,‘1244‘,‘success‘], [‘admin‘,‘1244‘,‘success‘], ])def test_login1(self,username,password,hope): ‘‘‘登錄‘‘‘ result = login(username,password) self.assertEqual(hope,result) @parameterized.expand(file_to_list(‘register_data.txt‘)) #從文件中讀取參數 def test_reg(self,username,password): ‘文件註冊‘ print(username,password) @parameterized.expand(excel_to_list(‘reg_data.xls‘)) #從Excel中讀取數據 def test_reg1(self,username,password): ‘Excel註冊‘ print(username,password) # unittest.main() runner = bf.BeautifulReport(unittest.makeSuite(TestLogin)) runner.report(description=‘登錄測試用例‘,filename=‘login.html‘)
以上代碼執行結果:
..FF...............
測試已全部完成, 可前往E:\Python學習\pycharm\python腳本\day12查詢測試報告
生成的報告如入:
python之用unittest實現接口參數化示例