python 配置文件__ConfigParser

基础读取配置文件

  • -read(filename)               直接读取文件内容
  • -sections()                      得到所有的section,并以列表的形式返回
  • -options(section)            得到该section的所有option
  • -items(section)                得到该section的所有键值对
  • -get(section,option)        得到section中option的值,返回为string类型
  • -getint(section,option)    得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。

具体详见文章:https://www.cnblogs.com/feeland/p/4514771.html

do_config.py

# -*- conding:utr-8 -*-
#@Time  :2018/11/6 16:45
#@Author:GYP测试
#@File  :do_config.py

import configparser
class ReadConfig:
    def read_config(self,file_name,section,option):
        cf=configparser.ConfigParser()
        cf.read('case.config',encoding='utf-8')#打开文件
        return cf.get(section,option)
if __name__ == '__main__':
    res=ReadConfig().read_config('case.config','MODE','mode')
    print(res)

# #读取配置文件的数据
#
# print(cf.sections())
# print(cf.items('PYTHON'))
#
#
# res_1=cf.get('MODE','mode')
# print(res_1)
# res_2=cf['LEMON']['boss']
# print(res_2)
#
# #数据类型讨论的问题
# #--------都是字符串--------数据类型转换 eval()
# print(type(cf.get('PYTHON','num')))

case.config

---------------------------------------------------------------------------------------------------------------------

[MODE]

mode=[1,3]

[PYTHON]

num=89
name=小郭

[LEMON]

num=33
age=18
boss=华华
--------------------------------------------------------------------
gyp1101.py
 1 # -*- conding:utr-8 -*-
 2 #@Time  :2018/11/2 15:25
 3 #@Author:GYP测试
 4 #@File  :gyp1101.py
 5 from openpyxl import load_workbook
 6 from class_1103.do_config import ReadConfig
 7 class datedd:
 8     def __init__(self,file_name,sheet_name):
 9         self.file_name=file_name
10         self.sheet_name=sheet_name
11     def get_header(self):
12         wb = load_workbook(self.file_name)
13         sheet = wb[self.sheet_name]
14         header=[]
15         for i in range(1,sheet.max_column+1):
16             header.append(sheet.cell(1,i).value)
17         # print(header)
18         return header
19 
20     def get_data(self):
21         '''mode:控制是否执行所有的用例 默认值为all 为all就执行的用例
22         如果不等于all的话 就进入分值 判断
23         mode的值 只能输入 all 列表 这两种类型的参数'''
24         mode=ReadConfig().read_config('case.config','MODE','mode')
25 
26 
27         wb=load_workbook(self.file_name)
28         sheet=wb[self.sheet_name]
29         header=self.get_header()
30         test_data=[]
31         for i in range(2,sheet.max_row+1):
32             sub_data={}
33             for j in range(1,sheet.max_column+1):
34                 sub_data[header[j-1]]=sheet.cell(i,j).value
35             test_data.append(sub_data)
36         #根据mode值去进行判断
37         if mode=='all':#执行所有用例
38             final_data=test_data
39         else:
40             final_data=[]
41             for item in test_data:
42                 if item['case_id'] in eval(mode):
43                     final_data.append(item)
44         print(final_data)
45         return final_data
46 if __name__ == '__main__':
47     datedd('xg.xlsx','python1').get_data()
48     # datedd('xg.xlsx','python1').get_header()

通过配置文件可以修改具体 执行那些用例

原文地址:https://www.cnblogs.com/guoyuanping/p/9920922.html