• Python3-接口自动化-7-读取Excel封装方法


    1. 需要调用第三方库xlrd

    #调用读Excel的第三方库xlrd
    from xlrd import open_workbook

    2.获取Excel的文件路径

    
    
    from config import getpathInfo
    #调用读Excel的第三方库xlrd
    from xlrd import open_workbook
    # 拿到该项目所在的绝对路径
    path = getpathInfo.get_Path()

    #
    获取用例文件路径 xlsPath = os.path.join(path, "testFile", file_name, xls_name)

    3. 打开Excel并获得Excel的sheet

    file = open_workbook(xlsPath)#  打开用例Excel
    sheet = file.sheet_by_name(sheet_name)#  获得打开Excel的sheet

    4. 获取Excel的行数

    nrows = sheet.nrows

    5. 循环并获取每行的内容,添加到列表

     cls = []
    for i in range(nrows):# 根据行数做循环
          if sheet.row_values(i)[0] != u'case_name':# 如果这个Excel的这个sheet的第i行的第一列不等于case_name那么我们把这行的数据添加到cls[]
                cls.append(sheet.row_values(i))

    具体实现

    from config import getpathInfo
    #调用读Excel的第三方库xlrd
    from xlrd import open_workbook
    # 拿到该项目所在的绝对路径
    path = getpathInfo.get_Path()
    
    class readExcel():
    
        def get_xls(self, file_name, xls_name, sheet_name):# xls_name填写用例的Excel名称 sheet_name该Excel的sheet名称
    
            cls = []
            # 获取用例文件路径
            xlsPath = os.path.join(path, "testFile", file_name, xls_name)
            file = open_workbook(xlsPath)#  打开用例Excel
            sheet = file.sheet_by_name(sheet_name)#  获得打开Excel的sheet
    
            # 获取这个sheet内容行数
            nrows = sheet.nrows
            for i in range(nrows):# 根据行数做循环
                if sheet.row_values(i)[0] != u'case_name':# 如果这个Excel的这个sheet的第i行的第一列不等于case_name那么我们把这行的数据添加到cls[]
                    cls.append(sheet.row_values(i))
    
            return cls

     6. 用例中使用

    login_xls = readExcel.readExcel().get_xls('BusinessProcess/testLendRequestProcess',"test_001_sales_customer_getCustomerInfo.xlsx", sheet_name)
    
    # paramunittest是unittest实现参数化的一个专门的模块,可以传入多组参数,自动生成多个用例
    @paramunittest.parametrized(*login_xls)
    class TestCustomerGetCustomerInfo(unittest.TestCase):
    
        def setParameters(self, case_name, url, method, param, code, msg):
    
            self.case_name = str(case_name)
            self.url = str(url)
            self.method = str(method)
            self.param = str(param)
            self.code = str(code)
            self.msg = str(msg)
            self.return_json = None
    paramunittest是unittest实现参数化的一个专门的模块,可以传入多组参数,自动生成多个用例

  • 相关阅读:
    各位相加
    模板方法设计模式
    581. Shortest Unsorted Continuous Subarray
    LeetCode
    判断质数
    整除问题
    A+B+C问题
    juery 安全加固 集合
    丁洪波 -- 不要“ 总是拿着微不足道的成就来骗自己”
    avalon源码阅读(1)
  • 原文地址:https://www.cnblogs.com/chushujin/p/13329988.html
Copyright © 2020-2023  润新知