标签:相关 src ref dirname host 操作 结果 nbsp odi
python读取配置文件中的数据在前面的Python 读取配置文件常用几种方式文章中已经介绍,这里主要介绍对.ini文件的常用封装方法以及使用。
废话也不多说,画个操作,代码流。
__author__ = ‘Leo‘ from configparser import ConfigParser import os from common import Log class Config(object): """ 配置文件 """ """ :常量设置 :读取ini """ # titles: TITLE_TEST = ‘test‘ TITLE_RELEASE = ‘online_release‘ TITLE_EMAIL = ‘mail‘ # values: # [test\release] VALUE_TESTER = ‘tester‘ VALUE_ENVIRONMENT = ‘environment‘ VALUE_VERSION_CODE = ‘versionCode‘ VALUE_HOST = ‘host‘ VALUE_PATH = ‘path‘ VALUE_DATA = ‘data‘ # [mail] VALUE_SMTP_SERVER = ‘smtpserver‘ VALUE_SENDER = ‘sender‘ VALUE_RECEIVER = ‘receiver‘ VALUE_USERNAME = ‘username‘ VALUE_PASSWORD = ‘password‘ # 获取当前文件所在的根目录 path_root = str(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) def __init__(self): """ 初始化 """ self.config = ConfigParser() self.logger = Log.MyLog() self.conf_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ‘config.ini‘) if not os.path.exists(self.conf_path): raise FileNotFoundError("配置文件不存在!") # 读取配置文件 self.config.read(self.conf_path,encoding=‘utf8‘) # 测试环境配置 self.tester_test = self.get_conf(Config.TITLE_TEST,Config.VALUE_TESTER) self.environment_test = self.get_conf(Config.TITLE_TEST,Config.VALUE_ENVIRONMENT) self.versionCode_test = self.get_conf(Config.TITLE_TEST,Config.VALUE_VERSION_CODE) self.host_test = self.get_conf(Config.TITLE_TEST,Config.VALUE_HOST) self.path_test = self.get_conf(Config.TITLE_TEST,Config.VALUE_PATH) self.data_test = self.get_conf(Config.TITLE_TEST,Config.VALUE_DATA) # 邮件配置 self.smtpserver = self.get_conf(Config.TITLE_EMAIL,Config.VALUE_SMTP_SERVER) self.sender = self.get_conf(Config.TITLE_EMAIL,Config.VALUE_SENDER) self.receiver = self.get_conf(Config.TITLE_EMAIL,Config.VALUE_RECEIVER) self.username = self.get_conf(Config.TITLE_EMAIL,Config.VALUE_USERNAME) self.password = self.get_conf(Config.TITLE_EMAIL,Config.VALUE_PASSWORD) def get_conf(self, title, value): """ 配置文件读取 :param title: :param value: :return: """ return self.config.get(title, value) def set_conf(self, title, value, text): """ 配置文件修改 :param title: :param value: :param text: :return: """ self.config.set(title, value, text) with open(self.conf_path, "w+") as f: return self.config.write(f) def add_conf(self, title): """ 配置文件添加 :param title: :return: """ self.config.add_section(title) with open(self.conf_path, "w+") as f: return self.config.write(f) if __name__ == ‘__main__‘: print(Config().tester_test) print(Config().environment_test) print(Config().versionCode_test) print(Config().host_test) print(Config().data_test) print(Config().path_test)
; 接口测试框架配置文件 ; titles [test] # test配置 tester = 全栈测试开发日记 environment = test versionCode = v1.0 host = www.***.com path = / data = {‘a‘:1,‘b‘:2} [mail] #发送邮件信息 smtpserver = smtp.163.com sender = test1@163.com receiver = test2@163.com username = test1@163.com password = 12345678
文件结构:
运行结果
全栈测试开发日记 test v1.0 www.***.com {‘a‘:1,‘b‘:2} / Process finished with exit code 0
方法基本上就是这样,具体根据自己业务需要进行设计。
另外,对测试开发,自动化测试,全栈测试相关技术感兴趣的朋友,可以加入到群里学习和探索交流,进群方式,扫下方二维码。
标签:相关 src ref dirname host 操作 结果 nbsp odi
原文地址:https://www.cnblogs.com/liudinglong/p/12946974.html