欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用自定義裝飾器的示例詳解

 更新時間:2022年11月16日 11:55:07   作者:城中月  
在Python自動化測試中,可以使用自定義的裝飾器來給測試方法傳遞測試數(shù)據(jù)。本文將通過簡單的示例和大家介紹下具體的使用方法,希望對大家有所幫助

在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ū)域轉換為列表
        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 ls

my_decorator.py

def my_decorator(sequeence):
    def outer(func):
        def inner(self):
            for item in sequeence:
                try:
                    func(self,*item)
                except Exception:
                    # print()僅供調試用,后期考慮將出錯日志寫入數(shù)據(jù)庫
                    print('測試有問題')
                else:# print()僅供調試用,后期考慮將通過日志寫入數(shù)據(jù)庫
                    print('測試通過')
        return inner
    return outer

data.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)操作日志頁面的按照賬號搜索日志功能進行UI測試
        :param username: 賬號
        :param expect: 期望結果,'0‘代表搜索不到日志記錄,'1‘代表可以搜索到日志記錄
        :return:
        '''


        # 打開系統(tǒng)操作日志頁面
        self.psol.open()
        time.sleep(2)
        # 輸入賬號并點擊搜索
        self.psol.do_search(username)
        time.sleep(2)
        # 獲取搜索結果
        result = self.psol.get_search_result()

        # 進行斷言
        if expect == '0':
            self.assertEqual(0,len(result))
        else:
            self.assertNotEqual(0,len(result))
            for item in result:
                self.assertEqual(username, item.text)

到此這篇關于Python使用自定義裝飾器的示例詳解的文章就介紹到這了,更多相關Python自定義裝飾器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python中非常實用的Math模塊函數(shù)教程詳解

    Python中非常實用的Math模塊函數(shù)教程詳解

    Math模塊中,有很多基礎的數(shù)學知識,我們必須要掌握的,例如:指數(shù)、對數(shù)、三角或冪函數(shù)等。因此,特意借著這篇文章,為大家講解一些該庫
    2021-10-10
  • django filters實現(xiàn)數(shù)據(jù)過濾的示例代碼

    django filters實現(xiàn)數(shù)據(jù)過濾的示例代碼

    這篇文章主要介紹了django filters實現(xiàn)數(shù)據(jù)過濾的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • python檢測遠程服務器tcp端口的方法

    python檢測遠程服務器tcp端口的方法

    這篇文章主要介紹了python檢測遠程服務器tcp端口的方法,涉及Python操作socket檢測tcp端口的技巧,需要的朋友可以參考下
    2015-03-03
  • 最新評論