Python使用自定義裝飾器的示例詳解
在Python自動化測試中,使用自定義的裝飾器來給測試方法傳遞測試數(shù)據(jù):
reader.py
import csv
import json
from openpyxl import load_workbook
from setting import DATA_DIR
from os import path
class Reader:
@classmethod
def read_excel(cls,xlname, min_row, max_row, min_col, max_col):
xlname = path.join(DATA_DIR,xlname)
data = []
wb = load_workbook(xlname)
ws = wb.active
# 將選中區(qū)域轉(zhuǎn)換為列表
ls = list(ws.iter_rows(min_row=min_row, max_row=max_row, min_col=min_col, max_col=max_col))
wb.save(xlname)
for row in ls:
data_row = []
for col in row:
data_row.append(col.value)
data.append(data_row)
wb.save(xlname)
return data
@classmethod
def read_csv(cls, filename):
file_name = path.join(DATA_DIR,filename)
ls = None
with open(file=file_name,mode='r',encoding='utf-8') as f:
ls = list(csv.reader(f))
return lsmy_decorator.py
def my_decorator(sequeence):
def outer(func):
def inner(self):
for item in sequeence:
try:
func(self,*item)
except Exception:
# print()僅供調(diào)試用,后期考慮將出錯日志寫入數(shù)據(jù)庫
print('測試有問題')
else:# print()僅供調(diào)試用,后期考慮將通過日志寫入數(shù)據(jù)庫
print('測試通過')
return inner
return outerdata.csv
admin,1
wp,1
666666,1
msramsrsa,0
find_test.py
import time
import unittest
from parameterized import parameterized
from pages.page_login import PageLogin
from pages.page_system_operate_log import PageSystemOperateLog
from utils.decorate import my_decorator
from utils.driver import WebDriver
from utils.reader import Reader
class SysOperateLogFindPageTest(unittest.TestCase):
'''
系統(tǒng)操作日志頁面測試類:執(zhí)行系統(tǒng)操作日志頁面的UI測試
作者:awake.silent@qq.com
'''
@my_decorator(Reader.read_csv('search_log_by_username.csv'))
def test_search_log_by_username(self, username, expect):
'''
對系統(tǒng)操作日志頁面的按照賬號搜索日志功能進(jìn)行UI測試
:param username: 賬號
:param expect: 期望結(jié)果,'0‘代表搜索不到日志記錄,'1‘代表可以搜索到日志記錄
:return:
'''
# 打開系統(tǒng)操作日志頁面
self.psol.open()
time.sleep(2)
# 輸入賬號并點(diǎn)擊搜索
self.psol.do_search(username)
time.sleep(2)
# 獲取搜索結(jié)果
result = self.psol.get_search_result()
# 進(jìn)行斷言
if expect == '0':
self.assertEqual(0,len(result))
else:
self.assertNotEqual(0,len(result))
for item in result:
self.assertEqual(username, item.text)到此這篇關(guān)于Python使用自定義裝飾器的示例詳解的文章就介紹到這了,更多相關(guān)Python自定義裝飾器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python點(diǎn)云地面點(diǎn)濾波(Progressive Morphological Filter)算法介紹(PCL庫)
這篇文章主要介紹了python點(diǎn)云地面點(diǎn)濾波(Progressive Morphological Filter)算法介紹(PCL庫),了解膨脹/腐蝕這兩個基礎(chǔ)操作,可以通過對其進(jìn)行簡單組合來形成開/閉操作,需要的朋友可以參考下2021-08-08
Python 的第三方調(diào)試庫 ???pysnooper?? 使用示例
這篇文章主要介紹了Python 的第三方調(diào)試庫 ???pysnooper?? 使用示例的相關(guān)資料,需要的朋友可以參考下2023-02-02
Python使用selenium + headless chrome獲取網(wǎng)頁內(nèi)容的方法示例
這篇文章主要介紹了Python使用selenium + headless chrome獲取網(wǎng)頁內(nèi)容的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python操作MySQL簡單實(shí)現(xiàn)方法
這篇文章主要介紹了Python操作MySQL簡單實(shí)現(xiàn)方法,通過一個簡單的實(shí)例講述了Python針對mysql數(shù)據(jù)庫的增刪改查技巧,需要的朋友可以參考下2015-01-01
python+pytest接口自動化之session會話保持的實(shí)現(xiàn)
在接口測試的過程中,經(jīng)常會遇到有些接口需要在登錄的狀態(tài)下才能請求,本文主要介紹了python+pytest接口自動化之session會話保持的實(shí)現(xiàn),感興趣的可以了解一下2022-06-06
python內(nèi)置函數(shù)globals()的實(shí)現(xiàn)代碼
本文主要介紹了python內(nèi)置函數(shù)globals()的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Pygame實(shí)現(xiàn)游戲最小系統(tǒng)功能詳解
這篇文章主要介紹了Pygame實(shí)現(xiàn)游戲最小系統(tǒng),Pygame是一個專門用來開發(fā)游戲的 Python 模塊,主要為開發(fā)、設(shè)計 2D 電子游戲而生,具有免費(fèi)、開源,支持多種操作系統(tǒng),具有良好的跨平臺性等優(yōu)點(diǎn)2022-11-11

