Contents: Guide
-
- foreword
- 1. From entry to mastery of Python programming
- 2. Interface automation project actual combat
- 3. Actual Combat of Web Automation Project
- 4. Actual Combat of App Automation Project
- 5. Resume of first-tier manufacturers
- 6. Test and develop DevOps system
- 7. Commonly used automated testing tools
- Eight, JMeter performance test
- 9. Summary (little surprise at the end)
foreword
Introduction to configparser
The ConfigParser module has been renamed to configparser in Python 3.
This module defines the ConfigParser class. The ConfigParser class implements a basic configuration file parser language that provides a structure similar to that found in .ini files
ini file related knowledge
Key-value pairs can be separated by = or:;
section names are case-sensitive, while option names are case-insensitive;
leading and trailing blanks in key-value pairs will be removed;
values can be multi-line ;
The configuration file can contain comments, and the comments are prefixed with # or ;;
ini file example
[server]
age = 45
username = yes
# server就是section
# age、username就是option
Manipulate the ini file
Instantiate the ConfigParser class;
read the configuration file;
operate the configuration file;
base code
import configparser
filename = 'F:/Interface/config/server.ini'
# 实例化configparser
config = configparser.ConfigParser()
# 读取配置文件
config.read(filename, encoding="utf-8-sig")
# 获取某个option的值(最常见的操作)
config.get(section="server", option="username")
When the configuration file has Chinese, when calling the read() method, you need to pass the encoding="utf-8-sig" parameter
The most common operation is get(section,option,fallback="default value") to get the value of an option, of course you can also pass a fallback, when your option does not exist, the value of the fallback will be returned
configparser encapsulation class
In order to better reuse configparser, we write commonly used methods as a wrapper class
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = 操作配置文件工具类
"""
import configparser
class ConfigUtil:
# 实例化configparser
config = configparser.ConfigParser()
def read(self, filename):
"""
读取配置文件
:param filename: 配置文件路径
"""
self.config.read(filename, encoding="utf-8-sig")
def get(self, _options, _section='server'):
"""
获取某个options值
:param _options: option
:param _section: section
"""
try:
# 方式一:调用方法
value = self.config.get(section=_section, option=_options, fallback="默认值,key不存在则返回此值")
# 方式二:索引
value = self.config[_section][_options]
except Exception as e:
print("没有获取到值")
value = None
return value
def get_options_key_value(self, _section):
"""
以列表(name,value)的形式返回section中的每个值
:param _section: 某个section
:return: list[tuple(key,value)]
"""
return self.config.items(_section)
def get_all_section(self):
"""
获取所有section
"""
return self.config.sections()
def get_options_by_section(self, _section):
"""
获取section下所有可用options
"""
# 方式一
keys = []
for _options in self.config[_section]:
keys.append(_options)
# 方式二(推荐)
keys = self.config.options(_section)
return keys
def assert_section_in_config(self, _section):
"""
判断section是否存在
:param _section: 需要判断的section
"""
return _section in self.config
def assert_options_in_section(self, _section, _options):
"""
判断options是否存在某个section中
:param _section: 某个section
:param _options: 需要判断的options的key值
"""
return _options in self.config[_section]
configUtil = ConfigUtil()
if __name__ == '__main__':
filename = 'F:/imocInterface/config/server.ini'
configUtil.read(filename)
print(configUtil.get("username"))
print(configUtil.get_all_section())
print(configUtil.assert_section_in_config("server"))
print(configUtil.get_options_by_section("server"))
print(configUtil.assert_options_in_section("server", "usernsame"))
print(configUtil.get_options_key_value("server"))
The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled |
1. From entry to mastery of Python programming
2. Interface automation project actual combat
3. Actual Combat of Web Automation Project
4. Actual Combat of App Automation Project
5. Resume of first-tier manufacturers
6. Test and develop DevOps system
7. Commonly used automated testing tools
Eight, JMeter performance test
9. Summary (little surprise at the end)
On the road of life, don't be intimidated by difficulties and setbacks. Stick to your dreams and work hard for them. Every attempt is an opportunity to gain experience, and success can only come when you keep trying. Believe in yourself, not afraid of hardships, you will reap a better future!
Only those who go forward bravely can reach the pinnacle of success; only those who are constantly honed can break out of their own world. Always believe in yourself, be firm in your goals, move forward step by step, and you will find that miracles are not far away!
Only by bravely facing challenges can we welcome the joy of success; only by constantly pursuing progress can we create a better future. Let us work hard together and create our own brilliance with sweat and wisdom!