场景:请求获取验证码模块regVC.py读取配置文件config.ini时,regVC.py模块单独执行正常,但通过run_all.py模块批量执行时报错,找不到section

解决办法:配置文件路径需写绝对路径

config.ini文件如下:

python执行报错 configparser.NoSectionError: No section: 'section_1'

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):
 8     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()
View Code

相关文章:

  • 2022-02-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-13
  • 2021-07-23
  • 2021-08-11
  • 2021-10-12
猜你喜欢
  • 2021-12-04
  • 2022-12-23
  • 2021-06-12
  • 2022-03-03
  • 2022-12-23
  • 2021-06-03
相关资源
相似解决方案