pycharm读取Excel文件中的值


前置条件:

先下载:xlrd库,将业务逻辑(测试用例中的备注),请求地址,请求参数写到Excel中,分行写,一个用例写一行如下图

1.先获取所要读取Excel所在的路径(进行目录的拼接)

2.打开Excel,读取所需参数所在的工作簿(sheet),sheet1的索引为0,以此类推;

3.具体定位到某行、某列;

4.返回读取到的数据;

5.利用面向对象类的继承,调用获取的数据

 

import os
import xlrd
import json


class Helper(object):
    def dir_name(self,fileName,filePath='data'):
        '''
        获取要读取的文件路径
        :parameter:filename文件名
        :parameter:filepath文件路径
        '''
        return os.path.join(os.path.dirname(os.path.dirname(__file__)),filePath,fileName)

    def getReadExcel(self,rowx,index=0):
        '''
        读取Excel中的数据
        :param rowx:行
        :param index:要读取Excel中sheet的索引
        '''
        sheet=xlrd.open_workbook(self.dir_name('0703test.xlsx'))
        table=sheet.sheet_by_index(int(index))
        return table.row_values(rowx)

    def geturl(self,rowx):
        '''
        url:读取行中索引为1的值,getgetReadExcel中的返回值为list形式,所以取索引为1的值
        最后读取到的值为字典格式
        '''
        return self.getReadExcel(rowx)[1]

    def getdata(self,rowx):
        '''获取到的数据为字符串,反序列化为字典'''
        return json.loads(self.getReadExcel(rowx)[2])

利用类的继承,进行调用

import unittest
from page.info import *
from utills.helper import *


class InfoTest(unittest.TestCase,Helper):
    @classmethod
    def setUpClass(cls):
        pass
    @classmethod
    def tearDownClass(cls):
        pass

    def test_info_001(self):
        '''测试情感状态功能是否可编辑'''
        r=post(self.geturl(2),self.getdata(2))
        str1=r.text
        self.assertTrue("单身" in str1)
        print(r.text)


if   __name__  ==   '__main__':
    unittest.main(verbosity=2)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM