首先在包目录下创建文件夹: config
config 下创建 test.conf 文件
- [section1] #节点
- name = tank #k = v
- age = 28
config 下创建 readConfig.py 文件
- # 读取配置文件 readConfig.py
- import configparser
- conf = configparser.configparser()
- conf.read('c://test.conf')
- # 获取所有的节点
- ret1 = conf.sections()
- # 获取某一节点下所有的 K 值
- ret2 = conf.options()
- # 获取某一节点下某一个 K 值对应的的 V 值
- ret3 = conf.get('节点名', 'K')
- # 获取某一节点下所有的键值对
- ret4 = conf.items('节点名')
- # 增加节点
- conf.add_section('节点名')
- # 增加和更改键值对值
- conf.set('节点名','K','V')
- # 查询节点 / 键值对是否存在
- has_sec = conf.has_section('节点名')
- has_opt = conf.has_option('节点名','K')
- # 删除节点 / 键值对
- config.remove_section('节点名')
- config.remove_option('节点名','K')
- # 写入配置文件 test.conf
- conf.write(open('c://test.conf','w'))
其他目录下的. py 文件调用配置文件内容, 创建
- import requests
- import unittest
- from config import readConfig
- class GetLog(unittest.TestCase):
- '''请求 getCompanyList 接口'''
- # 从配置文件获取测试地址和参数
- host = readConfig.host
- url = readConfig.url
- payload = readConfig.payload
- def test_case(self):
- r1 = requests.post(GetLog.host+GetLog.url+"/getCompanyList", data=GetLog.payload)
- req1 = r1.json()
- print(req1)
- if __name__ == '__main__':
- unittest.main()
使用方法说明:
- # 加载需要的模块
- from config import readConfig
- # 获取 V 值给 V
- v = readConfig.v
- # 调用本文件内的方法, 并把值传入
方法名 (值 1, 值 2,...)
来源: http://www.bubuko.com/infodetail-2767424.html