python+requests+unittest 搭建接口自动化测试框架(三)

封装可以读取excel表的类    readExcel.py

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

 
class readExcel():
    def get_xls(self, xls_name, sheet_name):# xls_name填写用例的Excel名称 sheet_name该Excel的sheet名称
        cls = []
        # 获取用例文件路径
        xlsPath = os.path.join(os.getcwd()+'/testFile/case/'+ xls_name)
        file = xlrd.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
        
if __name__ == '__main__': 
    print(readExcel().get_xls('userCase.xls', 'login'))

python+requests+unittest 搭建接口自动化测试框架(三)

 封装读取.ini文件的类   readConfig.py

import os
import configparser

class ReadConfig():
    def __init__(self):
        config_path = os.path.join(os.getcwd()+'/config.ini')
        self.cf = configparser.ConfigParser()
        self.cf.read(config_path,encoding='utf-8')

    def get_http(self, name):
        value = self.cf.get('HTTP', name)
        return value
    def get_email(self, name):
        value = self.cf.get('EMAIL', name)
        return value
 
 
if __name__ == '__main__':
    print('HTTP中的baseurl值为:', ReadConfig().get_http('baseurl'))
config.ini的内容如下:
[HTTP]
scheme = http
baseurl = 127.0.0.1
port = 8888
timeout = 10.0
 
[EMAIL]
on_off = on
subject = 接口自动化测试报告
app = Outlook
addressee = songxiaobao@qq.com
cc = zhaobenshan@qq.com