config.ini
# this is config file, only store browser type and server URL [browserType] #browserName = Firefox browserName = Chrome #browserName = IE [testServer] URL = https://www.baidu.com #URL = http://www.google.com
test_config.py(python3) import ConfigParser(python2)
1 # coding=utf-8 2 import configparser 3 import os 4 5 6 class TestReadConfigFile(object): 7 def get_value(self): 8 root_dir = os.path.dirname(os.path.abspath('.')) # 获取项目根目录的相对路径 9 print(root_dir) 10 11 config = configparser.ConfigParser() 12 file_path = os.path.dirname(os.path.abspath('.')) + '/config/config.ini' 13 config.read(file_path) 14 15 browser = config.get("browserType", "browserName") 16 url = config.get("testServer", "URL") 17 18 return (browser, url) # 返回的是一个元组 19 20 21 trcf = TestReadConfigFile() 22 print(trcf.get_value())