pytest之公共类封装读取excel(pandas)

import pandas as pd
import os
import yaml

abs_path = os.path.abspath(__file__)
# 三种方式
project_path = os.path.dirname(abs_path).rstrip('\\common')
# project_path = os.path.dirname(abs_path).replace('\\common','')
# project_path = os.path.dirname(abs_path)[0:len(os.path.dirname(abs_path))-6]

def read_excel(filePath, sheet_name):
    df = pd.read_excel((project_path+filePath), sheet_name=sheet_name)
    # 获取EXCEL的行数0,列数1
    lines_count = df.shape[0]
    print('lines_count:',lines_count)
    # 获取EXCEL的列数
    cols_count = df.shape[1]

    result_data = []
    lines_data = []
    for i in range(lines_count):
        lines_data = []
        for j in range(cols_count):
            cell_data = df.iloc[i, j]
            # 把不是str类型的数据都装为int
            if not isinstance(cell_data, str):
                cell_data = int(cell_data)
            print('cell_data',cell_data)
            lines_data.append(cell_data)
        result_data.append(lines_data)
    print('result_data.append(lines_data):',result_data)
    return result_data
from common.excel_util import read_excel

@pytest.mark.parametrize('casename,title, author, content, createUser,expect_code,expect_message', read_excel(r'\data\testCases.xlsx', 'Sheet1'))
def test_buy_now_params(casename, title, author, content, createUser, expect_code, expect_message):
    print('casename,title, author, content, createUser,expect_code,expect_message:',casename, title, author, content, createUser, expect_code, expect_message)
pytest之公共类封装读取excel(pandas)_第1张图片

你可能感兴趣的:(笔记,pytest,python,excel,pandas)