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