文章目錄
- Python讀取配置檔案
- python執行報錯 NoSectionError: No
- run_all.py代碼如下:
Python讀取配置檔案
配置檔案: driver_data.ini
[db]
url=www.baidu.com
[bowers]
bowers_ff=firefox
bowers_cr=chrome
bowers_ie=ie
import os,sys
import ConfigParser
# class ReadConf():
cf = ConfigParser.ConfigParser()
path = “../conf/driver_data.ini”
fileopen = open(path)
cf.readfp(fileopen)
secs = cf.sections() # read all section
print (‘sections:’, secs)
print (cf.get(‘db’,’url’)) # read db section url value
kvs = cf.items(‘bowers’) # read bowers all item&key
print (‘bowers:’, kvs)
python執行報錯 NoSectionError: No
sectionconfigparser.NoSectionError: No section: ‘section_1’
場景:請求擷取驗證碼子產品regVC.py讀取配置檔案config.ini時,regVC.py子產品單獨執行正常,但通過run_all.py子產品批量執行時報錯,找不到section
解決辦法:配置檔案路徑需寫絕對路徑
headers = {"Content-Type":"application/json"}
url = http://test.invoice.html
data_path =
import requests
import configparser
import unittest
from Case.readexcel import ExcelData
import json
class registerVerifyCode(unittest.TestCase):
def setUp(self):
self.Purl = "/api/register/getVerifyCode"
#取配置檔案内資料
self.config = configparser.ConfigParser()
self.text = self.config.read("F:\\Case\\config.ini") #這裡要寫配置檔案的絕對路徑
self.section = self.config.sections()
self.option = self.config.options("section_1")
self.item = self.config.items("section_1")
self.url = self.config.items("section_1")[1][1]+self.Purl
self.headers = self.config.items("section_1")[0][1]
#self.headers由str類型轉化為字典類型
self.header = eval(self.headers)
self.data_path = self.config.items("section_1")[2][1]
self.sheetname = "注冊驗證碼擷取"
self.data = ExcelData(self.data_path,self.sheetname).readExcel()
print(self.url)
print(self.data)
def test_reVC(self):
for a in self.data:
for b in a:
print(a)
print(b)
par = {"data":{
b:a[b]
}
}
print(par)
par_json = json.dumps(par)
res = requests.post(self.url,par_json,headers=self.header)
print(res.text)
if "手機号碼已注冊" in res.text:
print("該手機号碼已注冊")
if "請求注冊驗證碼成功" in res.text:
print("請求注冊驗證碼成功")
if __name__ == '__main__':
unittest.main()
run_all.py代碼如下:
import unittest
def all_case():
case_dir = "F:\\KEJINSUO_interface\\Case\\"
testCase = unittest.TestSuite()
discover = unittest.defaultTestLoader.discover(case_dir, pattern = "reg*.py", top_level_dir = None)
testCase.addTest(discover)
return testCase
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(all_case())