python執行報錯 configparser.NoSectionError: No section: 'section_1'
阿新 • • 發佈:2018-01-05
elf import gis hid blog image shee urn opened
場景:請求獲取驗證碼模塊regVC.py讀取配置文件config.ini時,regVC.py模塊單獨執行正常,但通過run_all.py模塊批量執行時報錯,找不到section
解決辦法:配置文件路徑需寫絕對路徑
config.ini文件如下:
regVC.py模塊代碼如下:
1 import requests 2 import configparser 3 import unittest 4 from Case.readexcel import ExcelData 5 import json 6 7 class registerVerifyCode(unittest.TestCase):View Code8 def setUp(self): 9 self.Purl = "/api/register/getVerifyCode" 10 #取配置文件內數據 11 self.config = configparser.ConfigParser() 12 self.text = self.config.read("F:\\Case\\config.ini") #這裏要寫配置文件的絕對路徑 13 self.section = self.config.sections()14 self.option = self.config.options("section_1") 15 self.item = self.config.items("section_1") 16 self.url = self.config.items("section_1")[1][1]+self.Purl 17 self.headers = self.config.items("section_1")[0][1] 18 #self.headers由str類型轉化為字典類型 19 self.header = eval(self.headers)20 self.data_path = self.config.items("section_1")[2][1] 21 self.sheetname = "註冊驗證碼獲取" 22 self.data = ExcelData(self.data_path,self.sheetname).readExcel() 23 print(self.url) 24 print(self.data) 25 26 def test_reVC(self): 27 for a in self.data: 28 for b in a: 29 print(a) 30 print(b) 31 par = {"data":{ 32 b:a[b] 33 } 34 } 35 print(par) 36 par_json = json.dumps(par) 37 res = requests.post(self.url,par_json,headers=self.header) 38 print(res.text) 39 if "手機號碼已註冊" in res.text: 40 print("該手機號碼已註冊") 41 if "請求註冊驗證碼成功" in res.text: 42 print("請求註冊驗證碼成功") 43 44 if __name__ == ‘__main__‘: 45 unittest.main()
run_all.py代碼如下:
1 import unittest 2 3 def all_case(): 4 case_dir = "F:\\KEJINSUO_interface\\Case\\" 5 testCase = unittest.TestSuite() 6 discover = unittest.defaultTestLoader.discover(case_dir, pattern = "reg*.py", top_level_dir = None) 7 testCase.addTest(discover) 8 return testCase 9 10 if __name__ == ‘__main__‘: 11 runner = unittest.TextTestRunner() 12 runner.run(all_case())
python執行報錯 configparser.NoSectionError: No section: 'section_1'