Robot从excel 读取数据

2019-12-04  本文已影响0人  只知坚持_d778

test_project_excel 练习目录:

目录结构
MyKey
import xlrd

class ts_excel():
        
    def _open_excel(self, path):
        try:
            work_book = xlrd.open_workbook(path, 'rb')
        except Exception as e:
            print(e)
            raise Exception("can not open work book! Pls check it at path:%s" % path)
        return work_book


    def _open_sheet_by_index(self, work_book, index):
        try:
            sheet = work_book.sheet_by_index(index)
        except:
            raise Exception("Can not open sheet by index %s" % index)
        return sheet

    def _open_sheet_by_name(self, work_book, sheet_name):
        sheet = None
        for st_name in work_book.sheet_names():
            if str(st_name) == sheet_name:
                sheet = work_book.sheet_by_name(sheet_name)
        if sheet == None:
            raise Exception("Can not open sheet by name %s" % sheet_name)
        return sheet

    def _get_data_from_sheet(self, sheet, col_index=2):
        nrows = sheet.nrows
        print(nrows)
        ncols = sheet.ncols
        if ncols < col_index:
            raise Exception("Can not find %d cols from sheet %s" % (col_index, sheet.name))
        # colnames = sheet.row_value(0) #第一行的数据
        list1 = []
        list2 = []
        for rownum in range(1, nrows):
            row_col0 = sheet.row(rownum)[0].value
            row_col1 = sheet.row(rownum)[1].value
            list1.append(row_col0)
            list2.append(row_col1)
        dict_date = dict(zip(list1, list2))
        print(dict_date)
        return dict_date

    def _get_testdata_from_sheet(self, sheet, col_index=3):
        nrows = sheet.nrows
        print(nrows)
        ncols = sheet.ncols
        if ncols < col_index:
            raise Exception("Can not find %dth cols from sheet %s" % (col_index, sheet.name))
        list1 = []
        list2 = []
        for rownum in nrows:
            for colnum in ncols:
                list1.append(sheet.row(rownum)[colnum].value)
                list2.append(list1)
        list1 = []
        print(list2)
        return list2

    def get_testdata_from_excel_by_index(self, path, index):
        work_book = self._open_excel(path)
        sheet = self._open_sheet_by_index(work_book, index)
        dict_data = self._get_data_from_sheet(sheet)
        return dict_data

    def get_testdata_from_excel_by_name(self, path, sheet_name=u'Sheet1'):
        work_book = self._open_excel(path)
        sheet = self._open_sheet_by_name(work_book, sheet_name)
        dict_data = self._get_data_from_sheet(sheet)
        return dict_data

    def get_testdata_from_excel(self, path, sheet_name=u'Sheet1'):
        work_book = self._open_excel(path)
        sheet = self._open_sheet_by_name(work_book, sheet_name)
        list_data = self._get_data_from_sheet(sheet)
        return list_data

#MyKey().get_testdata_from_excel_by_index('F:\python学习\\test_project_excel\\test_excel01.xlsx', 0)
#MyKey().get_testdata_from_excel_by_name('F:\python学习\\test_project_excel\\test_excel01.xlsx', 'account')
#MyKey().get_testdata_from_excel('F:\python学习\\test_project_excel\\test_excel01.xlsx', 'account')

  1. 导入自定义库


    导入自定义库
  2. 创建测试
*** Settings ***
Library            MyLibrary
Library            MyKey/ts_excel.py

*** Variables ***
${path}            ./test_excel01.xlsx
${index}           ${0}

*** Test Cases ***
test_demo01
     test_a_b     1     2

test_case02
     ${data}     get_testdata_from_excel_by_index     ${path}     ${index}

test_case03
     ${data2}     get_testdata_from_excel_by_name     ${path}     account

test_case04
     ${data3}     get_testdata_from_excel     ${path}     account
  1. 运行结果

    image.png
  2. 命令运行结果

指定运行执行文件 指定运行文件并输出到指定文件夹下 指定文件中某个案例执行并输出到指定文件夹下 设置debug模式,打印调试信息 左上角可选择debug,显示debug信息

参考:https://www.jianshu.com/p/3479f3ee59adh'h

上一篇下一篇

猜你喜欢

热点阅读