configparser--configuration file analysis library

configparser--configuration file analysis library

1. Introduction
This module mainly provides a class ConfigParser, which implements operations on structured files, similar to Windows INI files. The test framework test data can be isolated through the INI file

The format of the configuration file: [] contains the section node, and there are key values ​​such as option=value under the section


Test parameter configuration file data.ini
[testcase1]
testcase1.1=('name1','password1','nick1',)
testcase1.2=('name2','password2','nick2')

[testcase2]
testcase2.1=('name1','password1','nick1')
testcase2.2=('name2','password2','nick2')

 

Second, the example
takes the tuple type parameter code

1  # coding: utf-8 
2  
3  from configparser import ConfigParser
 4  # Instantiate a ConfigParser() class 
5 data = ConfigParser()
 6  # Read file 
7 data.read( ' ./data/data.ini ' , ' utf -8 ' )
 8  # Take evaluate with section and option, return unicode type 
9 re1 = data[ ' testcase1 ' ][ ' testcase1.1 ' ]
 10  # Convert unicode to string 
11 # re2 = str(re1) 
12 re2 = re1.encode( ' utf-8 ' )
 13  #Restore to tuple 
14 re = eval(re2)

 

Third, the package function

Encapsulate the function of taking test parameters

1  def GetIniData(filepath, section, option):
 2      """Enter the ini file path, node, key, and get the corresponding test data"""    
 3      try :
 4          data = ConfigParser()
 5          data.read(filepath, ' utf- 8 ' )
 6          r = eval(data[section][option].encode( ' utf-8 ' ))
 7          return r
 8      except BaseException as msg:
 9          return msg

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324854636&siteId=291194637